Skip to content
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

fix: Focus not removed when selection list changes. #39141

Merged
merged 11 commits into from
Apr 9, 2024
19 changes: 15 additions & 4 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,18 +401,29 @@ function BaseSelectionList<TItem extends ListItem>(
);

const prevTextInputValue = usePrevious(textInputValue);
const prevSelectedOptionsLength = usePrevious(flattenedSections.selectedOptions.length);

useEffect(() => {
// Avoid changing focus if the textInputValue remains unchanged.
if (prevTextInputValue === textInputValue || flattenedSections.allOptions.length === 0) {
if ((prevTextInputValue === textInputValue && flattenedSections.selectedOptions.length === prevSelectedOptionsLength) || flattenedSections.allOptions.length === 0) {
return;
}
// Remove the focus if the search input is empty else focus on the first non disabled item
const newSelectedIndex = textInputValue === '' ? -1 : 0;
// Remove the focus if the search input is empty or selected options length is changed else focus on the first non disabled item
const newSelectedIndex = textInputValue === '' || flattenedSections.selectedOptions.length !== prevSelectedOptionsLength ? -1 : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from this issue #36140 (comment), we also need to check all options's length here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from #42583 this was causing the first category search result to not be highlighted.


// reseting the currrent page to 1 when the user types something
setCurrentPage(1);

updateAndScrollToFocusedIndex(newSelectedIndex);
}, [canSelectMultiple, flattenedSections.allOptions.length, prevTextInputValue, textInputValue, updateAndScrollToFocusedIndex]);
}, [
canSelectMultiple,
flattenedSections.allOptions.length,
flattenedSections.selectedOptions.length,
prevTextInputValue,
textInputValue,
updateAndScrollToFocusedIndex,
prevSelectedOptionsLength,
]);

useEffect(
() => () => {
Expand Down
Loading