1
1
import React from 'react' ;
2
2
import CustomPropTypes from './util/propTypes' ;
3
- import { number as numberLocalizer } from './util/localizers' ;
3
+ import { number as numberLocalizer } from './util/localizers' ;
4
4
5
5
let getFormat = props => numberLocalizer . getFormat ( 'default' , props . format )
6
6
@@ -9,21 +9,21 @@ export default React.createClass({
9
9
displayName : 'NumberPickerInput' ,
10
10
11
11
propTypes : {
12
- value : React . PropTypes . number ,
12
+ value : React . PropTypes . number ,
13
13
placeholder : React . PropTypes . string ,
14
14
15
- format : CustomPropTypes . numberFormat ,
15
+ format : CustomPropTypes . numberFormat ,
16
16
17
- parse : React . PropTypes . func ,
18
- culture : React . PropTypes . string ,
17
+ parse : React . PropTypes . func ,
18
+ culture : React . PropTypes . string ,
19
19
20
- min : React . PropTypes . number ,
20
+ min : React . PropTypes . number ,
21
21
22
- onChange : React . PropTypes . func . isRequired ,
23
- onKeyDown : React . PropTypes . func
22
+ onChange : React . PropTypes . func . isRequired ,
23
+ onKeyDown : React . PropTypes . func
24
24
} ,
25
25
26
- getDefaultProps ( ) {
26
+ getDefaultProps ( ) {
27
27
return {
28
28
value : null ,
29
29
editing : false
@@ -35,8 +35,6 @@ export default React.createClass({
35
35
, decimal = numberLocalizer . decimalChar ( null , props . culture )
36
36
, format = getFormat ( props ) ;
37
37
38
- this . _beginningWithSign = false ;
39
-
40
38
if ( value == null || isNaN ( props . value ) )
41
39
value = ''
42
40
else
@@ -67,48 +65,35 @@ export default React.createClass({
67
65
className = 'rw-input'
68
66
onChange = { this . _change }
69
67
onBlur = { this . _finish }
70
- onKeyPress = { this . _typing }
71
68
aria-disabled = { this . props . disabled }
72
69
aria-readonly = { this . props . readOnly }
73
70
disabled = { this . props . disabled }
74
71
readOnly = { this . props . readOnly }
75
72
placeholder = { this . props . placeholder }
76
- value = { value } />
73
+ value = { value }
74
+ />
77
75
)
78
76
} ,
79
77
80
- _typing ( e ) {
81
- var current = e . target . value
82
- , newVal = e . key ;
83
-
84
- this . _beginningWithSign = current . trim ( ) === '' && this . isSign ( newVal )
85
-
86
- this . props . onKeyPress
87
- && this . props . onKeyPress ( e )
88
- } ,
89
-
90
78
_change ( e ) {
91
79
var val = e . target . value
92
80
, number = this . _parse ( e . target . value )
93
- , atSign = this . isSign ( val . trim ( ) )
94
- , startingWithSign = this . _beginningWithSign ;
95
81
96
- this . _beginningWithSign = false ;
82
+ let isIntermediate = this . isIntermediateValue ( number , val ) ;
97
83
98
- if ( val == null || val . trim ( ) === '' || ( atSign && ! startingWithSign ) ) {
84
+ if ( val == null || val . trim ( ) === '' ) {
99
85
this . current ( '' )
100
86
return this . props . onChange ( null )
101
87
}
102
88
103
- if ( this . isFlushable ( number , val ) ) {
104
- if ( number !== this . props . value )
89
+ if ( ! isIntermediate ) {
90
+ if ( number !== this . props . value ) {
105
91
return this . props . onChange ( number )
106
- else
107
- this . setState ( this . getDefaultState ( ) ) // 5. -> 5
92
+ }
108
93
}
109
-
110
- if ( number < this . props . min || ( atSign && startingWithSign ) || this . isAtDelimiter ( number , val ) )
94
+ else {
111
95
this . current ( e . target . value )
96
+ }
112
97
} ,
113
98
114
99
_finish ( ) {
@@ -117,7 +102,10 @@ export default React.createClass({
117
102
118
103
// if number is below the min
119
104
// we need to flush low values and decimal stops, onBlur means i'm done inputing
120
- if ( ! isNaN ( number ) && ( number < this . props . min || this . isAtDelimiter ( number , str ) ) ) {
105
+ if ( this . isIntermediateValue ( number , str ) ) {
106
+ if ( isNaN ( number ) ) {
107
+ number = null ;
108
+ }
121
109
this . props . onChange ( number )
122
110
}
123
111
} ,
@@ -136,29 +124,42 @@ export default React.createClass({
136
124
return strVal
137
125
} ,
138
126
139
- isFlushable ( num , str ) {
140
- return (
141
- this . isValid ( num )
142
- && ! this . isAtDelimiter ( num , str )
143
- && ! this . isSign ( str )
144
- )
127
+ isIntermediateValue ( num , str ) {
128
+ return ! ! (
129
+ num < this . props . min ||
130
+ this . isSign ( str ) ||
131
+ this . isAtDelimiter ( num , str ) ||
132
+ this . isPaddedZeros ( str )
133
+ ) ;
145
134
} ,
146
135
147
136
isSign ( val ) {
148
137
return ( val || '' ) . trim ( ) === '-' ;
149
138
} ,
150
139
140
+ isPaddedZeros ( str ) {
141
+ let localeChar = numberLocalizer . decimalChar ( null , this . props . culture )
142
+ let [ _ , decimals ] = str . split ( localeChar ) ;
143
+
144
+ return ! ! (
145
+ decimals &&
146
+ decimals . match ( / 0 + $ / )
147
+ )
148
+ } ,
149
+
151
150
isAtDelimiter ( num , str , props = this . props ) {
152
151
var localeChar = numberLocalizer . decimalChar ( null , props . culture )
153
152
, lastIndex = str . length - 1
154
153
, char ;
155
154
156
- if ( str . length <= 1 ) return false
155
+ if ( str . length < 1 ) return false
157
156
158
157
char = str [ lastIndex ]
159
158
160
- return char === localeChar
161
- && str . indexOf ( char ) === lastIndex
159
+ return ! ! (
160
+ char === localeChar &&
161
+ str . indexOf ( char ) === lastIndex
162
+ )
162
163
} ,
163
164
164
165
isValid ( num ) {
@@ -168,8 +169,8 @@ export default React.createClass({
168
169
} ,
169
170
170
171
//this intermediate state is for when one runs into the decimal or are typing the number
171
- current ( val ) {
172
- this . setState ( { stringValue : val } )
172
+ current ( stringValue ) {
173
+ this . setState ( { stringValue } )
173
174
}
174
175
175
176
} ) ;
0 commit comments