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.

Enter key submitting form when menu open but showing no options

See original GitHub issue

Within 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.

image

Have created a quick demo here:

https://codesandbox.io/s/react-codesandboxer-example-i6bwn

Steps to reproduce:

  1. Type any non-existent colour in the select and ensure you are seeing the “No options” message.
  2. Hit enter, see the alert that the form has been submitted.

Is this intended behaviour?

Thanks 😃

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

4reactions
flexdineshcommented, Apr 8, 2020

Hey @joshamos! You need to prevent the default keydown behaviour. You can use the onKeyDown prop to customize it.

Eg.

const keyDownHandler = e => {
  if (e.key === "Enter") e.preventDefault();
}

<Select ...otherprops onKeydown={keyDownHandler} />

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.

3reactions
rsslldnphycommented, Aug 27, 2020

@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 the return 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!

Read more comments on GitHub >

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

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