forked from unpkg/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumberTextInput.js
47 lines (38 loc) · 1.03 KB
/
NumberTextInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react'
import PropTypes from 'prop-types'
import { parseNumber, formatNumber } from './NumberUtils'
class NumberTextInput extends React.Component {
static propTypes = {
value: PropTypes.number,
parseNumber: PropTypes.func,
formatNumber: PropTypes.func,
onChange: PropTypes.func
}
static defaultProps = {
value: 0,
parseNumber,
formatNumber
}
state = {
value: null
}
componentWillMount() {
this.setState({ value: this.props.value })
}
handleChange = (event) => {
const value = this.props.parseNumber(event.target.value)
this.setState({ value }, () => {
if (this.props.onChange)
this.props.onChange(value)
})
}
render() {
const { value } = this.state
const { parseNumber, formatNumber, ...props } = this.props // eslint-disable-line no-unused-vars
const displayValue = formatNumber(value)
return (
<input {...props} type="text" value={displayValue} onChange={this.handleChange}/>
)
}
}
export default NumberTextInput