Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created June 24, 2016 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/e8ce3998836452bd9b6b6cce2c225d0e to your computer and use it in GitHub Desktop.
Save adamloving/e8ce3998836452bd9b6b6cce2c225d0e to your computer and use it in GitHub Desktop.
React based HTML input for dollars that disallows decimals and inserts commas.
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