maputnik/src/components/inputs/NumberInput.jsx

51 lines
1.2 KiB
React
Raw Normal View History

2016-09-12 19:44:28 +02:00
import React from 'react'
2016-12-20 11:44:22 +01:00
import input from '../../config/input.js'
2016-09-12 19:44:28 +02:00
class NumberInput extends React.Component {
2016-12-18 20:09:27 +01:00
static propTypes = {
2016-09-12 19:47:28 +02:00
value: React.PropTypes.number,
style: React.PropTypes.object,
2016-09-12 19:44:28 +02:00
default: React.PropTypes.number,
min: React.PropTypes.number,
max: React.PropTypes.number,
onChange: React.PropTypes.func,
2016-09-12 19:44:28 +02:00
}
2016-12-18 20:09:27 +01:00
onChange(e) {
const value = parseFloat(e.target.value)
/*TODO: we can do range validation already here?
if(this.props.min && value < this.props.min) return
if(this.props.max && value > this.props.max) return
*/
if(isNaN(value)) {
this.props.onChange(this.props.default)
} else {
this.props.onChange(value)
}
2016-12-18 20:09:27 +01:00
}
2016-09-12 20:29:53 +02:00
2016-12-18 20:09:27 +01:00
render() {
let stepSize = null
if(this.props.max && this.props.min) {
stepSize = (this.props.max - this.props.min) / 10
}
2016-12-19 16:30:48 +01:00
return <input
style={{
2016-12-20 11:44:22 +01:00
...input.input,
2016-12-19 16:30:48 +01:00
...this.props.style
}}
type={"number"}
min={this.props.min}
max={this.props.max}
step={stepSize}
2016-12-19 16:30:48 +01:00
placeholder={this.props.default}
value={this.props.value}
onChange={this.onChange.bind(this)}
/>
2016-12-18 20:09:27 +01:00
}
2016-09-12 19:44:28 +02:00
}
export default NumberInput