maputnik/src/fields/number.jsx

43 lines
1.1 KiB
React
Raw Normal View History

2016-09-12 19:44:28 +02:00
import React from 'react'
import inputStyle from './input.js'
2016-09-12 19:44:28 +02:00
/*** Number fields with support for min, max and units and documentation*/
class NumberField extends React.Component {
2016-12-18 20:09:27 +01:00
static propTypes = {
onChange: React.PropTypes.func.isRequired,
2016-12-18 20:09:27 +01:00
name: React.PropTypes.string.isRequired,
2016-09-12 19:47:28 +02:00
value: React.PropTypes.number,
2016-09-12 19:44:28 +02:00
default: React.PropTypes.number,
unit: React.PropTypes.string,
min: React.PropTypes.number,
max: React.PropTypes.number,
doc: React.PropTypes.string,
2016-12-19 16:21:22 +01:00
style: React.PropTypes.object,
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
*/
this.props.onChange(value)
}
2016-09-12 20:29:53 +02:00
2016-12-18 20:09:27 +01:00
render() {
2016-12-19 16:30:48 +01:00
return <input
style={{
...inputStyle.input,
...this.props.style
}}
type="number"
name={this.props.name}
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 NumberField