Skip to content

Commit

Permalink
Merge pull request #44222 from Krishna2323/krishna2323/issue/36140
Browse files Browse the repository at this point in the history
fix: Workspace switcher - Selection disappears after erasing character with workspace selected.
  • Loading branch information
rlinoz authored Jun 27, 2024
2 parents 5e464ea + 0f822b6 commit 4722d06
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/SelectionList/BaseSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,17 @@ function BaseSelectionList<TItem extends ListItem>(

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

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

// reseting the currrent page to 1 when the user types something
setCurrentPage(1);
Expand All @@ -561,6 +564,7 @@ function BaseSelectionList<TItem extends ListItem>(
textInputValue,
updateAndScrollToFocusedIndex,
prevSelectedOptionsLength,
prevAllOptionsLength,
]);

useEffect(
Expand Down

0 comments on commit 4722d06

Please sign in to comment.