Material-ui: [Autocomplete] Return key clears currently selected option

Created on 5 Nov 2019  路  1Comment  路  Source: mui-org/material-ui

  • [x] The issue is present in the latest release.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 馃槸

After selecting an option, if you press the "enter" (return) keyboard key, the value is cleared.

Expected Behavior 馃

If wrapped in a form element, the return key should trigger a submit of that form.

Possibly, if the disableClearable prop is set to false then the return key _could_ be seen as clicking the "X" button to clear the field. If it is set to true then it shouldn't clear anything!

Steps to Reproduce 馃暪

It's present on all of the demos on https://material-ui.com/components/autocomplete/ if you select an option for any of the demos and then press the "enter" key, the value is removed/cleared.

Context 馃敠

I have a form with a single autocomplete field, the only other action is a button to submit the form.

After interacting with the autocomplete field and selecting a value, I wanted to trigger the form submit by using the "enter" key, rather than tabbing to the submit button and then pressing "enter".

Using the "enter" key to submit a form is expected behaviour (for me at least!), and having it clear the current field is the opposite of what I was expecting.

bug 馃悰 Autocomplete good first issue

Most helpful comment

@chapmanio Thank you for the bug report! Yeah, it's not great!

diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 72d7c84c3..2a447c1a1 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -527,7 +527,7 @@ export default function useAutocomplete(props) {
         handleFocusTag(event, 'next');
         break;
       case 'Enter':
-        if (highlightedIndexRef.current !== -1) {
+        if (highlightedIndexRef.current !== -1 && popupOpen) {
           // We don't want to validate the form.
           event.preventDefault();
           selectNewValue(event, filteredOptions[highlightedIndexRef.current]);

What do you think of this fix? Do you want to submit a pull request? :)

>All comments

@chapmanio Thank you for the bug report! Yeah, it's not great!

diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 72d7c84c3..2a447c1a1 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -527,7 +527,7 @@ export default function useAutocomplete(props) {
         handleFocusTag(event, 'next');
         break;
       case 'Enter':
-        if (highlightedIndexRef.current !== -1) {
+        if (highlightedIndexRef.current !== -1 && popupOpen) {
           // We don't want to validate the form.
           event.preventDefault();
           selectNewValue(event, filteredOptions[highlightedIndexRef.current]);

What do you think of this fix? Do you want to submit a pull request? :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbozan picture rbozan  路  3Comments

mb-copart picture mb-copart  路  3Comments

pola88 picture pola88  路  3Comments

finaiized picture finaiized  路  3Comments

sys13 picture sys13  路  3Comments