onKeydown event in react-select
Asked Answered
I

2

7

I am using the react-select vserion 2 component. I want to be able to trigger the OnKeyDown event when someone presses the backspace key.

Does anyone know how to do this?

Individual answered 25/9, 2018 at 6:49 Comment(2)
Possible duplicate of React: Event handler method for BackspaceStibnite
Either provide code or exact module which you're using.Fourfold
B
8

react-select provides a prop onKeyDown which you can use as the following example:

 const onKeyDown = e => {
    // catch the code of the key pressed
    if (e.keyCode === 8) {
      // do your stuff
    }
  };

In this function you can catch the keyCode of the key pressed.

Here a live example.

Balenciaga answered 25/9, 2018 at 14:27 Comment(0)
H
0

react-select no onKeyDown props available.

Consider something like this:

class App extends Component {
  onKeyDown = () => {
    console.log("your code here");
  };

  render() {
    return (
      <div onKeyDown={this.onKeyDown} className="App">
        <Select options={options} />
      </div>
    );
  }
}

codesandbox: https://codesandbox.io/s/react-select-key-event-khczpp

Headley answered 11/9, 2023 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.