-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Autocomplete] Input cleared unexpectedly with freeSolo, autoHighlight and autoSelect #18646
Comments
@reiv Thanks for the bug report, I can confirm two issues:
What do you think of the following diff? diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index a8fd29e1a..df72a0980 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -114,7 +114,6 @@ export default function useAutocomplete(props) {
const [focusedTag, setFocusedTag] = React.useState(-1);
const defaultHighlighted = autoHighlight ? 0 : -1;
const highlightedIndexRef = React.useRef(defaultHighlighted);
- const selectedIndexRef = React.useRef(-1);
function setHighlightedIndex(index, mouse = false) {
highlightedIndexRef.current = index;
@@ -350,7 +349,6 @@ export default function useAutocomplete(props) {
const nextIndex = validOptionIndex(getNextIndex(), direction);
setHighlightedIndex(nextIndex);
- selectedIndexRef.current = nextIndex;
if (autoComplete && diff !== 'reset') {
if (nextIndex === -1) {
@@ -429,8 +427,6 @@ export default function useAutocomplete(props) {
}
resetInputValue(event, newValue);
-
- selectedIndexRef.current = -1;
};
function validTagIndex(index, direction) {
@@ -615,8 +611,8 @@ export default function useAutocomplete(props) {
return;
}
- if (autoSelect && selectedIndexRef.current !== -1) {
- handleValue(event, filteredOptions[selectedIndexRef.current]);
+ if (autoSelect && highlightedIndexRef.current !== -1 && popupOpen) {
+ handleValue(event, filteredOptions[highlightedIndexRef.current]);
} else if (!freeSolo) {
resetInputValue(event, value);
}
@@ -673,8 +669,13 @@ export default function useAutocomplete(props) {
return;
}
- // Restore the focus to the correct option.
- setHighlightedIndex(highlightedIndexRef.current);
+ // Automatically select the first option as the listbox become visible.
+ if (highlightedIndexRef.current === -1 && autoHighlight) {
+ changeHighlightedIndex('reset', 'next');
+ } else {
+ // Restore the focus to the correct option.
+ setHighlightedIndex(highlightedIndexRef.current);
+ }
});
const handlePopupIndicator = event => { Basically:
Do you want to submit a pull request? :) |
@oliviertassinari I tried out your suggested changes and while it does fix the original issue I'm not sure if treating "highlighted" and "selected" option as the same is the way to go. For instance, an option could be inadvertently selected by simply mousing over it, which feels weird. Also, when clearing the input, the first available option is autoselected on blur, unless the listbox is dismissed first. // Automatically select the first option as the listbox becomes visible.
- if (highlightedIndexRef.current === -1 && autoHighlight) {
+ if (inputValue && highlightedIndexRef.current === -1 && autoHighlight) {
changeHighlightedIndex('reset', 'next'); |
@reiv Thanks for the feedback:
|
@reiv @oliviertassinari Can I pick it up? |
Are you still working on it @SerhiiBilyk? |
@oliviertassinari @captain-yossarian I modified the original sandbox to demonstrate this: https://codesandbox.io/s/create-react-app-forked-ff1h7?file=/src/App.js Basically it's the same bug mentioned before:
Considering v5 is currently being worked on, I'm assuming a fix for this would have to wait for the next release instead of being merged into v4? |
@alexpermiakov You can pick it up |
@theGirrafish Do you want to open a new issue for it? Regarding a fix, I believe we could do: diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index a9d8d48de3..5e47e637ca 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -430,7 +430,7 @@ export default function useAutocomplete(props) {
}
// Synchronize the value with the highlighted index
- if (!filterSelectedOptions && valueItem != null) {
+ if (valueItem != null) {
const currentOption = filteredOptions[highlightedIndexRef.current];
// Keep the current highlighted index if possible It would fail a test case @captain-yossarian you added but I believe the asserted behavior from #19923 was only one of the two options mentioned in #19637. I think that it's fair to reset the index when |
@oliviertassinari Sure, I can create another ticket for this. |
@theGirrafish please take it, I have no time ( |
what's the status on this one? |
An
Autocomplete
withfreeSolo
,autoHighlight
andautoSelect
props set totrue
clears its input when it loses focus and the input value is not an exact or partial match for any of the provided options.Current Behavior 😯
See above. This only occurs once; going back and entering the same free string causes it to persist until the input is cleared again (either manually or by clicking the clear button), which seems to put it back into the problematic state. Note that the string must contain letters that appear in any of the option labels for this to occur.
Expected Behavior 🤔
The input value should not be cleared on blur as long as
freeSolo
istrue
.Steps to Reproduce 🕹
Codesandbox: https://codesandbox.io/s/create-react-app-76xd5
Steps:
To reset, click the clear button or backspace over the input field.
Context 🔦
I am trying to build a simple autocomplete field which permits free values but optimizes entry of provided options first (hence this combination of props).
Your Environment 🌎
The text was updated successfully, but these errors were encountered: