2016-09-12 19:44:28 +02:00
|
|
|
import React from 'react'
|
2017-11-06 16:32:04 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2016-09-12 19:44:28 +02:00
|
|
|
|
2016-12-26 11:48:52 +01:00
|
|
|
class NumberInput extends React.Component {
|
2016-12-18 20:09:27 +01:00
|
|
|
static propTypes = {
|
2017-11-06 16:32:04 +01:00
|
|
|
value: PropTypes.number,
|
|
|
|
default: PropTypes.number,
|
|
|
|
min: PropTypes.number,
|
|
|
|
max: PropTypes.number,
|
|
|
|
onChange: PropTypes.func,
|
2016-09-12 19:44:28 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 16:18:53 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
value: props.value
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 11:48:52 +01:00
|
|
|
|
2018-05-17 12:24:39 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2016-12-30 16:18:53 +01:00
|
|
|
this.setState({ value: nextProps.value })
|
|
|
|
}
|
|
|
|
|
|
|
|
changeValue(newValue) {
|
|
|
|
const value = parseFloat(newValue)
|
|
|
|
|
|
|
|
const hasChanged = this.state.value !== value
|
|
|
|
if(this.isValid(value) && hasChanged) {
|
2016-12-26 11:48:52 +01:00
|
|
|
this.props.onChange(value)
|
2016-12-30 16:18:53 +01:00
|
|
|
} else {
|
|
|
|
this.setState({ value: newValue })
|
2016-12-26 11:48:52 +01:00
|
|
|
}
|
2016-12-18 20:09:27 +01:00
|
|
|
}
|
2016-09-12 20:29:53 +02:00
|
|
|
|
2016-12-30 16:18:53 +01: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
|
2016-12-21 10:31:38 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 16:18:53 +01:00
|
|
|
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}
|
2016-12-30 16:18:53 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-26 11:48:52 +01:00
|
|
|
export default NumberInput
|