question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React onChange event doesn't work with Inputmask

See original GitHub issue

There’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:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
matheusbrascommented, Oct 17, 2016

@nicholasrq Did you manage to get this working somehow?

1reaction
RobinHerbotscommented, Jun 2, 2017

@nicholasrq , @an-kh ,

I made some modification in the vanilla dependencylib which could fix the problem in react. Can you have a try.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found