4
4
Input ,
5
5
ElementRef ,
6
6
Renderer2 ,
7
- HostListener ,
8
7
ViewChild ,
9
8
HostBinding ,
10
9
forwardRef
@@ -19,29 +18,29 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
19
18
<div class="ant-input-number-handler-wrap">
20
19
<a class="ant-input-number-handler ant-input-number-handler-up"
21
20
[ngClass]="{'ant-input-number-handler-up-disabled':_disabledUp}"
22
- (mousedown )="_numberUp($event)">
21
+ (click )="_numberUp($event)">
23
22
<span
24
23
class="ant-input-number-handler-up-inner"
25
24
(click)="$event.preventDefault();"></span>
26
25
</a>
27
26
<a
28
27
class="ant-input-number-handler ant-input-number-handler-down"
29
28
[ngClass]="{'ant-input-number-handler-down-disabled':_disabledDown}"
30
- (mousedown )="_numberDown($event)">
29
+ (click )="_numberDown($event)">
31
30
<span
32
31
class="ant-input-number-handler-down-inner"
33
32
(click)="$event.preventDefault();">
34
33
</span>
35
34
</a>
36
35
</div>
37
36
<div
38
- #inputWrapper
39
37
class="ant-input-number-input-wrap">
40
38
<input class="ant-input-number-input"
41
39
#inputNumber
40
+ (blur)="_checkValue()"
42
41
[placeholder]="nzPlaceHolder"
43
42
[disabled]="nzDisabled"
44
- [(ngModel)]="nzValue "
43
+ [(ngModel)]="_displayValue "
45
44
(ngModelChange)="_userInputChange()"
46
45
[attr.min]="nzMin"
47
46
[attr.max]="nzMax"
@@ -64,16 +63,16 @@ export class NzInputNumberComponent implements ControlValueAccessor {
64
63
_value : number ;
65
64
_size = 'default' ;
66
65
_prefixCls = 'ant-input-number' ;
67
- _disabledUp = false ;
68
- _disabledDown = false ;
69
66
_step = 1 ;
70
67
_precisionStep = 0 ;
71
68
_precisionFactor = 1 ;
69
+ _displayValue ;
70
+ _disabledUp = false ;
71
+ _disabledDown = false ;
72
72
// ngModel Access
73
73
onChange : any = Function . prototype ;
74
74
onTouched : any = Function . prototype ;
75
75
@ViewChild ( 'inputNumber' ) _inputNumber : ElementRef ;
76
- @ViewChild ( 'inputWrapper' ) _inputWrapper : ElementRef ;
77
76
78
77
@Input ( ) nzPlaceHolder = '' ;
79
78
@Input ( ) nzMin : number = - Infinity ;
@@ -109,30 +108,15 @@ export class NzInputNumberComponent implements ControlValueAccessor {
109
108
this . _precisionFactor = Math . pow ( 10 , this . _precisionStep ) ;
110
109
}
111
110
112
- @HostListener ( 'document:click' , [ '$event.target' ] )
113
- onClick ( target ) {
114
- if ( target && ! this . _inputWrapper . nativeElement . contains ( target ) ) {
115
- this . _offClick ( ) ;
116
- }
117
- }
118
-
119
- _checkDisabled = ( ) => {
120
- this . _disabledUp = ! ( ( this . nzValue + this . nzStep ) <= this . nzMax ) ;
121
- this . _disabledDown = ! ( ( this . nzValue - this . nzStep ) >= this . nzMin ) ;
122
- }
123
-
124
111
_numberUp ( $event ) {
125
112
$event . preventDefault ( ) ;
126
113
$event . stopPropagation ( ) ;
127
114
if ( this . nzValue === undefined ) {
128
115
this . nzValue = this . nzMin || 0 ;
129
116
}
130
- this . _checkDisabled ( ) ;
131
117
if ( ! this . _disabledUp ) {
132
118
this . nzValue = this . toPrecisionAsStep ( ( this . _precisionFactor * this . nzValue + this . _precisionFactor * this . nzStep ) / this . _precisionFactor ) ;
133
119
}
134
- this . _checkDisabled ( ) ;
135
- this . _userInputChange ( ) ;
136
120
}
137
121
138
122
_numberDown ( $event ) {
@@ -141,12 +125,9 @@ export class NzInputNumberComponent implements ControlValueAccessor {
141
125
if ( this . nzValue === undefined ) {
142
126
this . nzValue = this . nzMin || 0 ;
143
127
}
144
- this . _checkDisabled ( ) ;
145
128
if ( ! this . _disabledDown ) {
146
129
this . nzValue = this . toPrecisionAsStep ( ( this . _precisionFactor * this . nzValue - this . _precisionFactor * this . nzStep ) / this . _precisionFactor ) ;
147
130
}
148
- this . _checkDisabled ( ) ;
149
- this . _userInputChange ( ) ;
150
131
}
151
132
152
133
get nzValue ( ) : number {
@@ -157,35 +138,39 @@ export class NzInputNumberComponent implements ControlValueAccessor {
157
138
if ( this . _value === value ) {
158
139
return ;
159
140
}
160
- if ( value > this . nzMax ) {
161
- this . _value = this . nzMax ;
162
- this . onChange ( this . nzMax ) ;
163
- } else if ( value < this . nzMin ) {
164
- this . _value = this . nzMin ;
165
- this . onChange ( this . nzMin ) ;
166
- } else {
167
- this . _value = value ;
168
- this . _checkDisabled ( ) ;
169
- }
141
+ this . _value = this . _getBoundValue ( value ) ;
142
+ this . _displayValue = this . _value ;
143
+ this . _inputNumber . nativeElement . value = this . _value ;
144
+ this . onChange ( this . _value ) ;
145
+ this . _disabledUp = ( this . nzValue !== undefined ) && ! ( ( this . nzValue + this . nzStep ) <= this . nzMax ) ;
146
+ this . _disabledDown = ( this . nzValue !== undefined ) && ! ( ( this . nzValue - this . nzStep ) >= this . nzMin ) ;
170
147
}
171
148
172
149
_userInputChange ( ) {
173
- this . onChange ( this . nzValue ) ;
150
+ const numberValue = + this . _displayValue ;
151
+ if ( this . _isNumber ( numberValue ) && ( numberValue <= this . nzMax ) && ( numberValue >= this . nzMin ) ) {
152
+ this . nzValue = numberValue ;
153
+ }
174
154
}
175
155
176
- _offClick ( ) {
177
- if ( this . _value === undefined ) {
178
- return ;
179
- }
180
- if ( this . _inputNumber . nativeElement . value > this . nzMax ) {
181
- this . _inputNumber . nativeElement . value = this . nzMax ;
182
- this . onChange ( this . nzMax ) ;
183
- } else if ( this . _inputNumber . nativeElement . value < this . nzMin ) {
184
- this . _inputNumber . nativeElement . value = this . nzMin ;
185
- this . onChange ( this . nzMin ) ;
156
+ _checkValue ( ) {
157
+ this . _displayValue = this . nzValue ;
158
+ }
159
+
160
+ _getBoundValue ( value ) {
161
+ if ( value > this . nzMax ) {
162
+ return this . nzMax ;
163
+ } else if ( value < this . nzMin ) {
164
+ return this . nzMin ;
165
+ } else {
166
+ return value ;
186
167
}
187
168
}
188
169
170
+ _isNumber ( value ) {
171
+ return ! isNaN ( value ) && isFinite ( value )
172
+ }
173
+
189
174
toPrecisionAsStep ( num ) {
190
175
if ( isNaN ( num ) || num === '' ) {
191
176
return num ;
0 commit comments