Skip to content

Commit

Permalink
Merge pull request #39141 from Krishna2323/krishna2323/issue/35665
Browse files Browse the repository at this point in the history
fix: Focus not removed when selection list changes.
  • Loading branch information
bondydaa committed Apr 9, 2024
2 parents b9bd07f + 2fa4845 commit 05eb481
Showing 1 changed file with 15 additions and 4 deletions.
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;

// 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

0 comments on commit 05eb481

Please sign in to comment.