Created
June 24, 2016 23:53
-
-
Save adamloving/e8ce3998836452bd9b6b6cce2c225d0e to your computer and use it in GitHub Desktop.
React based HTML input for dollars that disallows decimals and inserts commas.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumericInput extends React.Component { | |
constructor(props = {}) { | |
super(props) | |
this.state = { | |
value: numeral(props.value).format('0,0') | |
} | |
} | |
onChange(e) { | |
var number = numeral().unformat(e.target.value) | |
var string = numeral(number).format('0,0') | |
this.setState({ value: string }) | |
this.props.onChange(number) | |
} | |
render() { | |
return ( | |
<input | |
className="form-control" | |
type="text" | |
placeholder={this.props.placeholder} | |
value={this.state.value} | |
onChange={this.onChange.bind(this)}/>) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment