Skip to content

Commit

Permalink
fix: keep in sync highlighted index when the option still exists but …
Browse files Browse the repository at this point in the history
…was moved
  • Loading branch information
Loïc Mangeonjean committed Mar 7, 2024
1 parent de10310 commit e59410a
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/mui-base/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export function useAutocomplete(props) {
},
);

const checkHighlightedOptionExists = () => {
const getPreviousHighlightedOptionIndex = () => {
const isSameValue = (value1, value2) => {
const label1 = value1 ? getOptionLabel(value1) : '';
const label2 = value2 ? getOptionLabel(value2) : '';
Expand All @@ -498,16 +498,12 @@ export function useAutocomplete(props) {
const previousHighlightedOption = previousProps.filteredOptions[highlightedIndexRef.current];

if (previousHighlightedOption) {
const previousHighlightedOptionExists = filteredOptions.some((option) => {
return filteredOptions.findIndex((option) => {
return getOptionLabel(option) === getOptionLabel(previousHighlightedOption);
});

if (previousHighlightedOptionExists) {
return true;
}
}
}
return false;
return null;
};

const syncHighlightedIndex = React.useCallback(() => {
Expand All @@ -517,7 +513,9 @@ export function useAutocomplete(props) {

// Check if the previously highlighted option still exists in the updated filtered options list and if the value and inputValue haven't changed
// If it exists and the value and the inputValue haven't changed, return, otherwise continue execution
if (checkHighlightedOptionExists()) {
const previousHighlightedOptionIndex = getPreviousHighlightedOptionIndex()
if (previousHighlightedOptionIndex != null) {
highlightedIndexRef.current = previousHighlightedOptionIndex;
return;
}

Expand Down

0 comments on commit e59410a

Please sign in to comment.