Skip to content

Commit 80eab3b

Browse files
authored
feat(module:input-number): support clear input-number with nzAllowClear option (#918)
close #861
1 parent 082555a commit 80eab3b

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class NzInputNumberComponent implements ControlValueAccessor {
7979
_disabledDown = false;
8080
_focused = false;
8181
_mouseInside = false;
82+
_allowClear = false;
8283
// ngModel Access
8384
onChange: (value: number) => void = () => null;
8485
onTouched: () => void = () => null;
@@ -100,6 +101,15 @@ export class NzInputNumberComponent implements ControlValueAccessor {
100101
return this._disabled;
101102
}
102103

104+
@Input()
105+
set nzAllowClear(value: boolean) {
106+
this._allowClear = toBoolean(value);
107+
}
108+
109+
get nzAllowClear(): boolean {
110+
return this._allowClear;
111+
}
112+
103113
@Input()
104114
set nzSize(value: string) {
105115
this._renderer.removeClass(this._el, `${this._prefixCls}-${this._size}`);
@@ -190,7 +200,18 @@ export class NzInputNumberComponent implements ControlValueAccessor {
190200
}
191201
}
192202

203+
_isEmpty(value?: number | string | undefined | null): boolean {
204+
return value === undefined
205+
|| value === null
206+
|| (typeof value === 'string' && this._displayValue.trim() === '')
207+
|| (typeof value === 'string' && this.nzFormatter('') === value);
208+
}
209+
193210
_checkValue(): void {
211+
if (this.nzAllowClear && this._isEmpty(this._displayValue)) {
212+
this.nzValue = undefined;
213+
return;
214+
}
194215
const numberValue = +this.nzParser(this._displayValue);
195216
if (this._isNumber(numberValue)) {
196217
this.nzValue = numberValue;
@@ -201,7 +222,9 @@ export class NzInputNumberComponent implements ControlValueAccessor {
201222
}
202223

203224
_getBoundValue(value: number): number {
204-
if (value > this.nzMax) {
225+
if (this.nzAllowClear && (value === undefined)) {
226+
return value;
227+
} else if (value > this.nzMax) {
205228
return this.nzMax;
206229
} else if (value < this.nzMin) {
207230
return this.nzMin;
@@ -247,8 +270,8 @@ export class NzInputNumberComponent implements ControlValueAccessor {
247270
private _updateValue(value: number, emitChange: boolean = true): void {
248271
const cacheValue = this._value;
249272
this._value = this._getBoundValue(value);
250-
this._displayValue = this.nzFormatter(this._value);
251-
this._inputNumber.nativeElement.value = this.nzFormatter(this._value);
273+
this._displayValue = this.nzFormatter(this._value || '');
274+
this._inputNumber.nativeElement.value = this.nzFormatter(this._value || '');
252275
if (emitChange && (value !== cacheValue)) {
253276
this.onChange(this._value);
254277
}

src/showcase/nz-demo-input-number/nz-demo-input-number-digit.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ import { Component } from '@angular/core';
88
styles: []
99
})
1010
export class NzDemoInputNumberDigitComponent {
11-
demoValue: number;
11+
demoValue = 1;
1212
}

src/showcase/nz-demo-input-number/nz-demo-input-number-formatter.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export class NzDemoInputNumberFormatterComponent {
1616
parserPercent = value => value.replace('%', '');
1717
formatterDollar = value => `$${value}`;
1818
parserDollar = value => value.replace('$', '');
19-
formatterInt = value => parseInt(value, 10);
19+
formatterInt = value => value ? parseInt(value , 10) : '';
2020
parserInt = value => parseInt(value, 10);
2121
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ <h2 id="API"><span>API</span>
125125
<td>function( string): number</td>
126126
<td>-</td>
127127
</tr>
128+
<tr>
129+
<td>nzAllowClear</td>
130+
<td>是否允许清空input number中的数值</td>
131+
<td>boolean</td>
132+
<td>false</td>
133+
</tr>
128134
</tbody>
129135
</table>
130136
</section>

0 commit comments

Comments
 (0)