-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[Autocomplete] Fix auto highlight when options change but not the length #46489
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
Changes from all commits
d60059d
8744a12
7586fce
63f0880
10aa947
c60e905
c6dace7
91a5b21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,15 @@ import useControlled from '@mui/utils/useControlled'; | |
import useId from '@mui/utils/useId'; | ||
import usePreviousProps from '@mui/utils/usePreviousProps'; | ||
|
||
function areArraysSame({ array1, array2, parser = (value) => value }) { | ||
ZeeshanTamboli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ( | ||
array1 && | ||
array2 && | ||
array1.length === array2.length && | ||
array1.every((prevOption, index) => parser(prevOption) === parser(array2[index])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to invert the logic using if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I ask what's the specific solution you are suggesting?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clarification |
||
); | ||
} | ||
|
||
// https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript | ||
function stripDiacritics(string) { | ||
return string.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); | ||
|
@@ -456,6 +465,12 @@ function useAutocomplete(props) { | |
} | ||
}); | ||
|
||
const filteredOptionsChanged = !areArraysSame({ | ||
array1: previousProps.filteredOptions, | ||
array2: filteredOptions, | ||
parser: getOptionLabel, | ||
}); | ||
|
||
const getPreviousHighlightedOptionIndex = () => { | ||
const isSameValue = (value1, value2) => { | ||
const label1 = value1 ? getOptionLabel(value1) : ''; | ||
|
@@ -465,8 +480,11 @@ function useAutocomplete(props) { | |
|
||
if ( | ||
highlightedIndexRef.current !== -1 && | ||
previousProps.filteredOptions && | ||
previousProps.filteredOptions.length !== filteredOptions.length && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strengthen this condition by checking if elements are the same. |
||
!areArraysSame({ | ||
array1: previousProps.filteredOptions, | ||
array2: filteredOptions, | ||
parser: getOptionLabel, | ||
}) && | ||
previousProps.inputValue === inputValue && | ||
(multiple | ||
? value.length === previousProps.value.length && | ||
|
@@ -597,8 +615,10 @@ function useAutocomplete(props) { | |
} | ||
|
||
React.useEffect(() => { | ||
syncHighlightedIndex(); | ||
}, [syncHighlightedIndex]); | ||
if (filteredOptionsChanged) { | ||
syncHighlightedIndex(); | ||
} | ||
}, [syncHighlightedIndex, filteredOptionsChanged]); | ||
|
||
const handleOpen = (event) => { | ||
if (open) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.