0

I am working on react and I am using Select component provided by the material. (https://material-ui.com/components/selects/). Upon opening the dropdown, if I press a key and there is an option that starts with that key, the corresponding option is automatically selected. This is true for normal HTML select element too. For Example - If available options are Mike, Robert, Julie, Casie and after opening dropdown I press key R on my keyboard, the selection jumps to Robert automatically Is there any way I can prevent this behavior? I tried to add onKeyPress event to Select component but it is not getting fired.

Steps to reproduce:

  1. go to https://material-ui.com/components/selects/
  2. Open any of the dropdowns
  3. Press key T
  4. The option Ten automatically gets selected.
3
  • Have you read How to Ask and minimal reproducible example? Commented Feb 26, 2020 at 16:02
  • @CalvinNunes Added link to reproduce. This isn't a bug but something to do with how select dropdown has been implemented. Unfortunately in my case, I can't find a way to prevent this. Commented Feb 26, 2020 at 16:05
  • I don't think you can avoid this, maybe this can be helpful: stackoverflow.com/questions/19183715/… Commented Feb 26, 2020 at 16:18

1 Answer 1

1

You can stop the event propagation from the <MenuItem> as follows

 <Select
          labelId="demo-simple-select-label"
          id="demo-simple-select"
          value={age}
          onKeyDown={e => e.stopPropagation()}
          onChange={handleChange}
        >
          <MenuItem   onKeyDown={e => e.stopPropagation()} value={10}>Ten</MenuItem>
          <MenuItem  onKeyDown={e => e.stopPropagation()} value={20}>Twenty</MenuItem>
          <MenuItem  onKeyDown={e => e.stopPropagation()} value={30}>Thirty</MenuItem>
</Select>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.