Enter key submitting form when menu open but showing no options
See original GitHub issueWithin a <form onSubmit={}>
form that uses an onSubmit handler and a regular old <button type="submit">
, the enter key is submitting the form despite the react-select menu being open. This seems to happen when there are no options to display in the list.
Have created a quick demo here:
https://codesandbox.io/s/react-codesandboxer-example-i6bwn
Steps to reproduce:
- Type any non-existent colour in the select and ensure you are seeing the “No options” message.
- Hit enter, see the alert that the form has been submitted.
Is this intended behaviour?
Thanks 😃
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:6
Top Results From Across the Web
why doesn't hitting enter when a SELECT is focused submit ...
It's simply the nature of the control. The Enter key (or a mouse click) is what makes the selection. To submit the form...
Read more >The Enter Key should Submit Forms, Stop Suppressing it
When enter is clicked in the number input, the keypress event handler determines which submit button is appropriate and clicks it. While this ......
Read more >Forms in HTML documents - W3C
An HTML form is a section of a document containing normal content, markup, special elements called controls (checkboxes, radio buttons, menus, etc.), and...
Read more ><input type="submit"> - HTML: HyperText Markup Language
You must use this encoding type if your form includes any <input> ... Try entering some text into the text field, and then...
Read more >Banner Basics and Navigation - Staff
To display and use the Direct Access Field: From any form, press the F5 key on the keyboard. This opens the Direct Access....
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
Hey @joshamos! You need to prevent the default keydown behaviour. You can use the
onKeyDown
prop to customize it.Eg.
Eg. codesandbox here — https://codesandbox.io/s/react-select-form-submit-jdme8
I’m closing this issue now. If you need to discuss it further pl feel free to reopen it.
@flexdinesh I’m facing the same thing and I believe this is a real issue:
your proposed solution does prevent form submit when hitting enter with no options to choose from, but it also prevents selecting an option with the enter key when there are options to choose from, and it also prevents submission of the form by pressing the enter key when the menu is not open.
i believe the correct behaviour should be as described in this issue: #2798 - “If the input has focus, with the menu closed, pressing enter should submit a containing form.” - but if the menu is open, it shouldn’t.
i think this should be able to be fixed by adding
e.preventDefault()
in this block before thereturn
statements: https://github.com/JedWatson/react-select/blob/397e95337f54074cf0705cb5b06ce8951be72f2a/packages/react-select/src/Select.js#L1231-L1235 so that we never propagate the enter key if the menu is open. happy to submit a PR if you would like!