After selecting an option, if you press the "enter" (return) keyboard key, the value is cleared.
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!
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.
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.
@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? :)
Most helpful comment
@chapmanio Thank you for the bug report! Yeah, it's not great!
What do you think of this fix? Do you want to submit a pull request? :)