maputnik/src/components/inputs/NumberInput.jsx

81 lines
1.8 KiB
React
Raw Normal View History

2016-09-12 19:44:28 +02:00
import React from 'react'
import PropTypes from 'prop-types'
2016-09-12 19:44:28 +02:00
class NumberInput extends React.Component {
2016-12-18 20:09:27 +01:00
static propTypes = {
value: PropTypes.number,
default: PropTypes.number,
min: PropTypes.number,
max: PropTypes.number,
onChange: PropTypes.func,
2016-09-12 19:44:28 +02:00
}
constructor(props) {
super(props)
this.state = {
value: props.value
}
}
2018-05-17 12:24:39 +02:00
UNSAFE_componentWillReceiveProps(nextProps) {
this.setState({ value: nextProps.value })
}
changeValue(newValue) {
const value = parseFloat(newValue)
const hasChanged = this.state.value !== value
if(this.isValid(value) && hasChanged) {
this.props.onChange(value)
} else {
this.setState({ value: newValue })
}
2016-12-18 20:09:27 +01:00
}
2016-09-12 20:29:53 +02:00
isValid(v) {
const value = parseFloat(v)
if(isNaN(value)) {
return false
}
if(!isNaN(this.props.min) && value < this.props.min) {
return false
}
if(!isNaN(this.props.max) && value > this.props.max) {
return false
}
return true
}
resetValue() {
// Reset explicitly to default value if value has been cleared
if(this.state.value === "") {
return this.changeValue(this.props.default)
}
// If set value is invalid fall back to the last valid value from props or at last resort the default value
if(!this.isValid(this.state.value)) {
if(this.isValid(this.props.value)) {
this.changeValue(this.props.value)
} else {
this.changeValue(this.props.default)
}
}
}
render() {
2016-12-19 16:30:48 +01:00
return <input
2018-05-17 12:43:25 +02:00
spellCheck="false"
2017-01-10 21:28:30 +01:00
className="maputnik-number"
2016-12-19 16:30:48 +01:00
placeholder={this.props.default}
value={this.state.value}
onChange={e => this.changeValue(e.target.value)}
onBlur={this.resetValue.bind(this)}
2016-12-19 16:30:48 +01:00
/>
2016-12-18 20:09:27 +01:00
}
2016-09-12 19:44:28 +02:00
}
export default NumberInput