@@ -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 ) || 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,28 @@ 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
71+ if ( updatedValue === '' ) {
72+ this . setState ( {
73+ value : this . props . value ,
74+ numericInputClassName : this . getNumericInputClassName ( this . props . value ) ,
75+ } ) ;
76+ return ;
77+ }
78+
5979 // When the user blurs on non-numeric data reset the component
6080 // to the last known good value (this.props.value).
6181 if ( ! isNumeric ( updatedValue ) ) {
62- this . setState ( { value : propsValue } ) ;
82+ this . setState ( {
83+ value : updatedValue ,
84+ numericInputClassName : this . getNumericInputClassName ( updatedValue ) ,
85+ } ) ;
6386 return ;
6487 }
6588
@@ -151,7 +174,7 @@ export default class NumericInput extends Component {
151174 return (
152175 < div className = "numeric-input__wrapper" >
153176 < EditableText
154- className = { `numeric-input__number ${ this . props . editableClassName } ` }
177+ className = { this . state . numericInputClassName }
155178 placeholder = { this . props . placeholder }
156179 text = { this . state . value }
157180 type = "text"
0 commit comments