unpkg/client/NumberTextInput.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-04-15 04:01:23 +00:00
import React from 'react'
import PropTypes from 'prop-types'
import { parseNumber, formatNumber } from './NumberUtils'
2016-05-20 18:58:58 +00:00
class NumberTextInput extends React.Component {
static propTypes = {
value: PropTypes.number,
parseNumber: PropTypes.func,
2016-07-20 19:26:15 +00:00
formatNumber: PropTypes.func,
onChange: PropTypes.func
2016-05-20 18:58:58 +00:00
}
static defaultProps = {
value: 0,
parseNumber,
formatNumber
}
2017-04-15 04:01:23 +00:00
state = {
value: null
}
componentWillMount() {
2016-05-20 18:58:58 +00:00
this.setState({ value: this.props.value })
2017-04-15 04:01:23 +00:00
}
2016-05-20 18:58:58 +00:00
2017-11-08 16:57:15 +00:00
handleChange = event => {
2016-05-20 18:58:58 +00:00
const value = this.props.parseNumber(event.target.value)
this.setState({ value }, () => {
2017-11-08 16:57:15 +00:00
if (this.props.onChange) this.props.onChange(value)
2016-05-20 18:58:58 +00:00
})
}
2017-04-15 04:01:23 +00:00
render() {
2016-05-20 18:58:58 +00:00
const { value } = this.state
2017-03-25 00:35:38 +00:00
const { parseNumber, formatNumber, ...props } = this.props // eslint-disable-line no-unused-vars
2016-08-09 16:38:04 +00:00
const displayValue = formatNumber(value)
2016-05-20 18:58:58 +00:00
return (
2017-11-08 16:57:15 +00:00
<input
{...props}
type="text"
value={displayValue}
onChange={this.handleChange}
/>
2016-05-20 18:58:58 +00:00
)
}
}
export default NumberTextInput