React onChange event doesn't work with Inputmask
See original GitHub issueThere’s the issue.
When attaching Inputmask
to event inside of react component there is no way to know when
value has been changed.
React uses input
event to implement its onChange
, so now there is now way to listen input
when Inputmask
attached. Also it does not matter when i attaching event handler (before or after Inputmask
), input
event will never fire.
And yes, there is no other way to handle input changes if i want to sync value with component state.
There is an example of how does it behave: http://codepen.io/nicholasrq/pen/kkaEoL?editors=0010
UPD: I’ve added a button to enable/disable masking to ensure that native behaviour works when no mask attached.
Also code example:
class Input extends React.Component{
constructor(props){
super(props)
this.state = {
mask: true
}
this.onChange = this.onChange.bind(this)
this.toggleMask = this.toggleMask.bind(this)
}
render(){
// attaching onChange in react-way
// no luck
return (
<div>
<input onChange={this.onChange} ref="input" placeholder="input" type="text"/>
<button onClick={this.toggleMask}>{this.state.mask ? 'disable' : 'enable'}</button>
</div>
)
}
componentDidMount(){
// this callback will be invoked right after
// the component is completely rendered
// let's take reference to input
const input = this.refs.input
// now let's try to add native event
// handler BEFORE masking. not working
input.addEventListener('input', this.onChange, false)
// attaching inputmask
this.attachMask()
// also will not work
input.addEventListener('input', this.onChange, false)
}
componentDidUpdate(prevProps, prevState){
if(prevState.mask != this.state.mask){
if(this.state.mask){
this.attachMask()
} else {
this.mask.remove(this.refs.input)
}
}
}
attachMask(){
const mask = new Inputmask({ mask: '99.99.99' })
this.mask = mask.mask(this.refs.input)
}
onChange(e){
console.log(e.target.value)
}
toggleMask(){
this.setState({mask: !this.state.mask})
}
}
console.clear();
ReactDOM.render(<Input/>, document.querySelector('#wrapper'))
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:20 (9 by maintainers)
Top Results From Across the Web
Not working onChange in react-input-mask (custom input)
I am not using any libraries as input, but for some reason onChange dont work... <InputMask onChange={(e) => console.log(e.target ...
Read more >[Input Mask Reactive] on change event not triggered when ...
When applying the mask if the number is greater than 999 ie has 4 integer value the onchange event on the input no...
Read more >react input onchange doesn't work
When attaching Inputmask to event inside of react component there is no way to know when value has been changed.
Read more >Implementing react-input-mask for web apps
Like any other typical input element, we can use the onChange event to change the component state based on user inputs.
Read more >clearfacts/react-input-mask - npm package
children : function. NOTE: To make this feature more reliable, please tell about your use case in this issue. To use another component...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nicholasrq Did you manage to get this working somehow?
@nicholasrq , @an-kh ,
I made some modification in the vanilla dependencylib which could fix the problem in react. Can you have a try.