If you want to maintain input type='number'
(probably for mobile devices to trigger the numeric keyboard) you should use onInput
instead of onChange
to capture your event changes.
Using onInput
fixed a bug where typing text into a number input would bypass the validation I had assigned to it in onChange
. Once I fixed this function to be called in onInput
it triggered in all instances.
Here's an example of what I'm doing:
<input
type='number'
id={`player${index}Score`}
className='form-control'
pattern='[0-9]{0,5}'
onInput={(event) => this.enterScore(event, index)}
value={this.props.scoreLabel(this.state.scores[index])}
/>
I hope this helps!
EDIT - 08-03-2018:
I came up with a better solution. Use type='tel' along with a pattern regex within the input component itself.
The nuts and bolts of how I wired this up is here:
class Input extends React.Component {
state = {message: '3'};
updateNumber = (e) => {
const val = e.target.value;
// If the current value passes the validity test then apply that to state
if (e.target.validity.valid) this.setState({message: e.target.value});
// If the current val is just the negation sign, or it's been provided an empty string,
// then apply that value to state - we still have to validate this input before processing
// it to some other component or data structure, but it frees up our input the way a user
// would expect to interact with this component
else if (val === '' || val === '-') this.setState({message: val});
}
render() {
return (
<input
type='tel'
value={this.state.message}
onChange={this.updateNumber}
pattern="^-?[0-9]\d*\.?\d*$"
/>
);
}
}
ReactDOM.render(<Input />, document.getElementById('main'));
I have an example of this working on Codepen here