Fix for buggy string/number inputs when inputting invalid of intermediate values

This commit is contained in:
orangemug
2019-05-18 15:56:14 +01:00
parent c3c0c35d8a
commit cc8fe4e02e
2 changed files with 21 additions and 10 deletions

View File

@@ -14,13 +14,16 @@ class StringInput extends React.Component {
constructor(props) {
super(props)
this.state = {
editing: false,
value: props.value || ''
}
}
componentDidUpdate(prevProps) {
if(this.props.value !== prevProps.value) {
this.setState({value: this.props.value})
static getDerivedStateFromProps(props, state) {
if (!state.editing) {
return {
value: props.value
};
}
}
@@ -51,11 +54,15 @@ class StringInput extends React.Component {
placeholder: this.props.default,
onChange: e => {
this.setState({
editing: true,
value: e.target.value
})
},
onBlur: () => {
if(this.state.value!==this.props.value) this.props.onChange(this.state.value)
if(this.state.value!==this.props.value) {
this.setState({editing: false});
this.props.onChange(this.state.value);
}
}
});
}