@@ -12,13 +12,25 @@ export default class NumericInput extends Component {
1212 constructor ( props ) {
1313 super ( props ) ;
1414
15- this . state = { value : props . value } ;
15+ this . state = {
16+ value : props . value ,
17+ numericInputClassName : this . getNumericInputClassName ( props . value ) ,
18+ } ;
19+
1620 this . onChange = this . onChange . bind ( this ) ;
1721 this . updateValue = this . updateValue . bind ( this ) ;
1822 this . onKeyDown = this . onKeyDown . bind ( this ) ;
1923 this . onWheel = this . onWheel . bind ( this ) ;
2024 }
2125
26+ getNumericInputClassName ( value ) {
27+ return isNumeric ( 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+
2234 componentWillReceiveProps ( nextProps ) {
2335 if ( nextProps . value !== this . state . value ) {
2436 this . setState ( { value : nextProps . value } ) ;
@@ -49,17 +61,20 @@ export default class NumericInput extends Component {
4961 }
5062
5163 onChange ( value ) {
52- this . setState ( { value} ) ;
64+ this . setState ( { value, numericInputClassName : this . getNumericInputClassName ( value ) } ) ;
5365 }
5466
5567 updateValue ( newValue ) {
56- const { max, min, integerOnly, value : propsValue } = this . props ;
68+ const { max, min, integerOnly} = this . props ;
5769 let updatedValue = newValue ;
5870
5971 // When the user blurs on non-numeric data reset the component
6072 // to the last known good value (this.props.value).
6173 if ( ! isNumeric ( updatedValue ) ) {
62- this . setState ( { value : propsValue } ) ;
74+ this . setState ( {
75+ value : updatedValue ,
76+ numericInputClassName : this . getNumericInputClassName ( updatedValue ) ,
77+ } ) ;
6378 return ;
6479 }
6580
@@ -151,7 +166,7 @@ export default class NumericInput extends Component {
151166 return (
152167 < div className = "numeric-input__wrapper" >
153168 < EditableText
154- className = { `numeric-input__number ${ this . props . editableClassName } ` }
169+ className = { this . state . numericInputClassName }
155170 placeholder = { this . props . placeholder }
156171 text = { this . state . value }
157172 type = "text"
0 commit comments