@@ -12,13 +12,25 @@ export default class NumericInput extends Component {
12
12
constructor ( props ) {
13
13
super ( props ) ;
14
14
15
- this . state = { value : props . value } ;
15
+ this . state = {
16
+ value : props . value ,
17
+ numericInputClassName : this . getNumericInputClassName ( props . value ) ,
18
+ } ;
19
+
16
20
this . onChange = this . onChange . bind ( this ) ;
17
21
this . updateValue = this . updateValue . bind ( this ) ;
18
22
this . onKeyDown = this . onKeyDown . bind ( this ) ;
19
23
this . onWheel = this . onWheel . bind ( this ) ;
20
24
}
21
25
26
+ getNumericInputClassName ( value ) {
27
+ return isNumeric ( value ) || value === ''
28
+ ? `numeric-input__number ${ this . props . editableClassName ? this . props . editableClassName : '' } `
29
+ : `numeric-input__number--error ${
30
+ this . props . editableClassName ? this . props . editableClassName : ''
31
+ } `;
32
+ }
33
+
22
34
componentWillReceiveProps ( nextProps ) {
23
35
if ( nextProps . value !== this . state . value ) {
24
36
this . setState ( { value : nextProps . value } ) ;
@@ -49,17 +61,28 @@ export default class NumericInput extends Component {
49
61
}
50
62
51
63
onChange ( value ) {
52
- this . setState ( { value} ) ;
64
+ this . setState ( { value, numericInputClassName : this . getNumericInputClassName ( value ) } ) ;
53
65
}
54
66
55
67
updateValue ( newValue ) {
56
- const { max, min, integerOnly, value : propsValue } = this . props ;
68
+ const { max, min, integerOnly} = this . props ;
57
69
let updatedValue = newValue ;
58
70
71
+ if ( updatedValue === '' ) {
72
+ this . setState ( {
73
+ value : this . props . value ,
74
+ numericInputClassName : this . getNumericInputClassName ( this . props . value ) ,
75
+ } ) ;
76
+ return ;
77
+ }
78
+
59
79
// When the user blurs on non-numeric data reset the component
60
80
// to the last known good value (this.props.value).
61
81
if ( ! isNumeric ( updatedValue ) ) {
62
- this . setState ( { value : propsValue } ) ;
82
+ this . setState ( {
83
+ value : updatedValue ,
84
+ numericInputClassName : this . getNumericInputClassName ( updatedValue ) ,
85
+ } ) ;
63
86
return ;
64
87
}
65
88
@@ -151,7 +174,7 @@ export default class NumericInput extends Component {
151
174
return (
152
175
< div className = "numeric-input__wrapper" >
153
176
< EditableText
154
- className = { `numeric-input__number ${ this . props . editableClassName } ` }
177
+ className = { this . state . numericInputClassName }
155
178
placeholder = { this . props . placeholder }
156
179
text = { this . state . value }
157
180
type = "text"
0 commit comments