Skip to content

Commit cffac94

Browse files
hsuanxyzvthinkxie
authored andcommitted
feat(module:input): add nzOnSearch property (#876)
close #813
1 parent 80eab3b commit cffac94

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/components/input/nz-input.component.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface AutoSizeType {
3939
<input
4040
(blur)="_emitBlur($event)"
4141
(focus)="_emitFocus($event)"
42+
(keyup.enter)="_onPressEnter()"
4243
[attr.id]="nzId"
4344
[disabled]="nzDisabled"
4445
[readonly]="nzReadonly"
@@ -67,7 +68,7 @@ export interface AutoSizeType {
6768
[(ngModel)]="nzValue"></textarea>
6869
</ng-template>
6970
<span class="ant-input-suffix" *ngIf="(nzType==='search')||(_suffixContent)">
70-
<i class="anticon anticon-search ant-input-search-icon" *ngIf="nzType==='search'"></i>
71+
<i class="anticon anticon-search ant-input-search-icon" *ngIf="nzType==='search'" (click)="_emitSearch()"></i>
7172
<ng-template [ngTemplateOutlet]="_suffixContent">
7273
</ng-template>
7374
</span>
@@ -156,6 +157,7 @@ export class NzInputComponent implements AfterContentInit, ControlValueAccessor,
156157

157158
@Output() nzBlur: EventEmitter<FocusEvent> = new EventEmitter();
158159
@Output() nzFocus: EventEmitter<FocusEvent> = new EventEmitter();
160+
@Output() nzOnSearch: EventEmitter<string> = new EventEmitter();
159161
@ViewChild('inputTextarea') textAreaRef: ElementRef;
160162
@ContentChild('addOnBefore') _addOnContentBefore: TemplateRef<void>;
161163
@ContentChild('addOnAfter') _addOnContentAfter: TemplateRef<void>;
@@ -197,6 +199,16 @@ export class NzInputComponent implements AfterContentInit, ControlValueAccessor,
197199
this.nzFocus.emit($event);
198200
}
199201

202+
_onPressEnter(): void {
203+
if (this.nzType === 'search') {
204+
this._emitSearch();
205+
}
206+
}
207+
208+
_emitSearch(): void {
209+
this.nzOnSearch.emit(this._value);
210+
}
211+
200212
setClassMap(): void {
201213
this._classMap = {
202214
[`${this._prefixCls}-${this._size}`]: true,

src/showcase/nz-demo-input/nz-demo-input-search.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ import { Component } from '@angular/core';
33
@Component({
44
selector: 'nz-demo-input-search',
55
template: `
6-
<nz-input [nzType]="'search'" [nzPlaceHolder]="'input search text'" [(ngModel)]="_value" style="width: 200px;"></nz-input>
6+
<nz-input [nzType]="'search'" [nzPlaceHolder]="'input search text'" [(ngModel)]="_value" style="width: 200px;" (nzOnSearch)="onSearch($event)"></nz-input>
77
`,
88

99
styles: []
1010
})
1111
export class NzDemoInputSearchComponent {
12-
_value: string;
12+
_value = '';
13+
14+
onSearch(event: string): void {
15+
console.log(event);
16+
}
1317
}

src/showcase/nz-demo-input/nz-demo-input.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ <h4 id="Input.TextArea"><span>nz-input[type=textarea]</span>
186186
</tr>
187187
</tbody>
188188
</table>
189+
<h4 id="Input.Search"><span>nz-input[type=search]</span>
190+
<!-- <a class="anchor">#</a> -->
191+
</h4>
192+
<p><code>nzType="search"</code> 时,特有的API</p>
193+
<table>
194+
<thead>
195+
<tr>
196+
<th>参数</th>
197+
<th>说明</th>
198+
<th>类型</th>
199+
<th>默认值</th>
200+
</tr>
201+
</thead>
202+
<tbody>
203+
<tr>
204+
<td>nzOnSearch</td>
205+
<td>回车或点击搜索按钮时的回调</td>
206+
<td>EventEmitter&lt;string&gt;</td>
207+
<td>-</td>
208+
</tr>
209+
</tbody>
210+
</table>
189211
<h4 id="Input.Group"><span>nz-input-group</span>
190212
<!-- <a class="anchor">#</a> -->
191213
</h4>

0 commit comments

Comments
 (0)