insin / react-maskedinput

Masked <input/> React component
http://insin.github.io/react-maskedinput/
MIT License
730 stars 197 forks source link

onKeyPress and onKeyDown not working #109

Open WilliamCSA04 opened 7 years ago

WilliamCSA04 commented 7 years ago

Hello, Im having a problem with onKeyPress and onKeyDown. I did create a component called Input, and have this line:

<MaskedInput id="message" mask={this.props.mask} type={this.props.type} className="inpt inpt-default" placeholder={this.state.text} onBlur={this.onBlur} onFocus={this.onFocus} name={this.props.name} onChange={this.onChange} onKeyPress={this.props.onKeyPress} value={this.state.message}/>

And Im calling the Input component like this:

<Input ref="input" type="phone" name="phone" mask="(11) 11111 1111" onClick={this.inputOnClick} onKeyPress={this.handleKeyPress}/>

handleKeyPress(event){
     if(event.key === "Enter"){
      this.processUserInput(event.target.name);
     }

}

I know that my handleKeyPress function is working for two reasons, first, is working at other regular inputs, second, I had replaced use onKeyPress={this.props.onKeyPress} for onKeyUp={this.props.onKeyPress} and worked.

javierggi commented 6 years ago

Did you get this to work? I am stuck with the same problem.

WilliamCSA04 commented 6 years ago

Not really, I had to use KeyUp instead.

m-salamon commented 6 years ago

Im having the same issue, can anyone help please

cooperjones commented 6 years ago

Similar issue here. onKeyDown and onKeyUp both work, but onKeyPress in the same context doesn't function. Would love to have a fix for this!

Byron2016 commented 6 years ago

You can find the solution here:

https://gist.github.com/krambertech/76afec49d7508e89e028fce14894724c

handleKeyPress(event){ if(event.key === "13"){ this.processUserInput(event.target.name); } }

<Input ref="input" type="phone" name="phone" mask="(11) 11111 1111" onClick={this.inputOnClick} onKeyPress={(e) => this.handleKeyPress(e)}/>