@@ -400,14 +400,16 @@ function BaseSelectionList<TItem extends ListItem>({
400400 const currentSearchValue = textInputOptions ?. value ;
401401 const searchChanged = prevSearchValue !== currentSearchValue ;
402402 const selectedOptionsChanged = dataDetails . selectedOptions . length !== prevSelectedOptionsLength ;
403- // Focus shouldn't be changed if: input value is the same or data length is 0
404- // shouldUpdateFocusedIndex is true => other function handles the focus
403+ // Do not change focus if:
404+ // 1. Input value is the same or
405+ // 2. Data length is 0 or
406+ // 3. shouldUpdateFocusedIndex is true => other function handles the focus
405407 if ( ( ! searchChanged && ! selectedOptionsChanged ) || data . length === 0 || shouldUpdateFocusedIndex ) {
406408 return ;
407409 }
408410
409- // Clearing search
410- if ( prevSearchValue && ! currentSearchValue ) {
411+ const hasSearchBeenCleared = prevSearchValue && ! currentSearchValue ;
412+ if ( hasSearchBeenCleared ) {
411413 const foundSelectedItemIndex = data . findIndex ( isItemSelected ) ;
412414
413415 if ( foundSelectedItemIndex !== - 1 && ! canSelectMultiple ) {
@@ -417,7 +419,9 @@ function BaseSelectionList<TItem extends ListItem>({
417419 }
418420 }
419421
420- // Remove focus (-1) if the search is idle or if the user is just toggling options without changing the list content
422+ // Remove focus (set focused index to -1) if:
423+ // 1. If the search is idle or
424+ // 2. If the user is just toggling options without changing the list content
421425 // Otherwise (e.g. when filtering/typing), focus on the first item (0)
422426 const isSearchIdle = ! prevSearchValue && ! currentSearchValue ;
423427 const newSelectedIndex = isSearchIdle || ( selectedOptionsChanged && prevAllOptionsLength === data . length ) ? - 1 : 0 ;
0 commit comments