mirror of
https://github.com/maputnik/editor.git
synced 2026-01-05 13:00:04 +00:00
Fix for buggy string/number inputs when inputting invalid of intermediate values
This commit is contained in:
@@ -13,25 +13,28 @@ class NumberInput extends React.Component {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.state = {
|
this.state = {
|
||||||
value: props.value
|
editing: false,
|
||||||
|
value: props.value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static getDerivedStateFromProps(props, state) {
|
static getDerivedStateFromProps(props, state) {
|
||||||
return {
|
if (!state.editing) {
|
||||||
value: props.value
|
return {
|
||||||
};
|
value: props.value
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
changeValue(newValue) {
|
changeValue(newValue) {
|
||||||
|
this.setState({editing: true});
|
||||||
const value = parseFloat(newValue)
|
const value = parseFloat(newValue)
|
||||||
|
|
||||||
const hasChanged = this.state.value !== value
|
const hasChanged = this.state.value !== value
|
||||||
if(this.isValid(value) && hasChanged) {
|
if(this.isValid(value) && hasChanged) {
|
||||||
this.props.onChange(value)
|
this.props.onChange(value)
|
||||||
} else {
|
|
||||||
this.setState({ value: newValue })
|
|
||||||
}
|
}
|
||||||
|
this.setState({ value: newValue })
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid(v) {
|
isValid(v) {
|
||||||
@@ -52,6 +55,7 @@ class NumberInput extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resetValue = () => {
|
resetValue = () => {
|
||||||
|
this.setState({editing: false});
|
||||||
// Reset explicitly to default value if value has been cleared
|
// Reset explicitly to default value if value has been cleared
|
||||||
if(this.state.value === "") {
|
if(this.state.value === "") {
|
||||||
return this.changeValue(this.props.default)
|
return this.changeValue(this.props.default)
|
||||||
|
|||||||
@@ -14,13 +14,16 @@ class StringInput extends React.Component {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.state = {
|
this.state = {
|
||||||
|
editing: false,
|
||||||
value: props.value || ''
|
value: props.value || ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps) {
|
static getDerivedStateFromProps(props, state) {
|
||||||
if(this.props.value !== prevProps.value) {
|
if (!state.editing) {
|
||||||
this.setState({value: this.props.value})
|
return {
|
||||||
|
value: props.value
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,11 +54,15 @@ class StringInput extends React.Component {
|
|||||||
placeholder: this.props.default,
|
placeholder: this.props.default,
|
||||||
onChange: e => {
|
onChange: e => {
|
||||||
this.setState({
|
this.setState({
|
||||||
|
editing: true,
|
||||||
value: e.target.value
|
value: e.target.value
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
onBlur: () => {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user