@@ -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 }
0 commit comments