Skip to content

Commit

Permalink
extract code to function
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 committed Jan 29, 2023
1 parent fb77c56 commit c75f72e
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,7 @@ export default function useAutocomplete(props) {
},
);

const syncHighlightedIndex = React.useCallback(() => {
if (!popupOpen) {
return;
}

const checkHighlightedOptionExists = () => {
if (
highlightedIndexRef.current !== -1 &&
previousProps.filteredOptions &&
Expand All @@ -485,10 +481,24 @@ export default function useAutocomplete(props) {
});

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

const syncHighlightedIndex = React.useCallback(() => {
if (!popupOpen) {
return;
}

// Check if the previously highlighted option still exists in the updated filtered options list and if the value hasn't changed
// If it exists and the value hasn't changed, return, otherwise continue execution
if (checkHighlightedOptionExists()) {
return;
}

const valueItem = multiple ? value[0] : value;

// The popup is empty, reset
Expand Down

0 comments on commit c75f72e

Please sign in to comment.