Skip to content

Commit 6eaca48

Browse files
authored
Merge pull request #823 from plotly/numerics-error
add error state when !isNumber to Numeric component
2 parents 3d82b4d + 9bd9bfd commit 6eaca48

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/components/widgets/NumericInput.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

src/styles/components/widgets/_numeric-input.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@
4949
font-family: inherit;
5050
}
5151

52+
.numeric-input__number--error {
53+
@extend .numeric-input__number;
54+
border-color: var(--color-sienna);
55+
}
56+
57+
.numeric-input__number--error:focus {
58+
@extend .numeric-input__number;
59+
border-color: var(--color-sienna);
60+
outline-color: var(--color-sienna);
61+
}
62+
5263
.numeric-input__caret-box {
5364
display: inline-block;
5465
max-height: 32px;

0 commit comments

Comments
 (0)