@@ -167,34 +167,10 @@ function BaseSelectionList<TItem extends ListItem>(
167167 const { isKeyboardShown} = useKeyboardState ( ) ;
168168 const [ itemsToHighlight , setItemsToHighlight ] = useState < Set < string > | null > ( null ) ;
169169 const itemFocusTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
170- const isItemSelected = useCallback (
171- ( item : TItem ) => item . isSelected ?? ( ( isSelected ?.( item ) ?? selectedItems . includes ( item . keyForList ?? '' ) ) && canSelectMultiple ) ,
172- [ isSelected , selectedItems , canSelectMultiple ] ,
173- ) ;
174- /** Calculates on which page is selected item so we can scroll to it on first render */
175- const calculateInitialCurrentPage = useCallback ( ( ) => {
176- if ( canSelectMultiple || sections . length === 0 ) {
177- return 1 ;
178- }
179-
180- let currentIndex = 0 ;
181- for ( const section of sections ) {
182- if ( section . data ) {
183- for ( const item of section . data ) {
184- if ( isItemSelected ( item ) ) {
185- return Math . floor ( currentIndex / CONST . MAX_SELECTION_LIST_PAGE_LENGTH ) + 1 ;
186- }
187- currentIndex ++ ;
188- }
189- }
190- }
191- return 1 ;
192- } , [ canSelectMultiple , isItemSelected , sections ] ) ;
193- const [ currentPage , setCurrentPage ] = useState ( ( ) => calculateInitialCurrentPage ( ) ) ;
170+ const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
194171 const isTextInputFocusedRef = useRef < boolean > ( false ) ;
195172 const { singleExecution} = useSingleExecution ( ) ;
196173 const [ itemHeights , setItemHeights ] = useState < Record < string , number > > ( { } ) ;
197- const pendingScrollIndexRef = useRef < number | null > ( null ) ;
198174
199175 const onItemLayout = ( event : LayoutChangeEvent , itemKey : string | null | undefined ) => {
200176 if ( ! itemKey ) {
@@ -211,6 +187,11 @@ function BaseSelectionList<TItem extends ListItem>(
211187
212188 const incrementPage = ( ) => setCurrentPage ( ( prev ) => prev + 1 ) ;
213189
190+ const isItemSelected = useCallback (
191+ ( item : TItem ) => item . isSelected ?? ( ( isSelected ?.( item ) ?? selectedItems . includes ( item . keyForList ?? '' ) ) && canSelectMultiple ) ,
192+ [ isSelected , selectedItems , canSelectMultiple ] ,
193+ ) ;
194+
214195 const canShowProductTrainingTooltipMemo = useMemo ( ( ) => {
215196 return canShowProductTrainingTooltip && isFocused ;
216197 } , [ canShowProductTrainingTooltip , isFocused ] ) ;
@@ -343,17 +324,6 @@ function BaseSelectionList<TItem extends ListItem>(
343324 return ;
344325 }
345326
346- // Calculate which page is needed to show this index
347- const requiredPage = Math . ceil ( ( index + 1 ) / CONST . MAX_SELECTION_LIST_PAGE_LENGTH ) ;
348-
349- // If the required page is beyond the current page, load all pages up to it,
350- // then return early and let the scroll happen after the page update
351- if ( requiredPage > currentPage ) {
352- pendingScrollIndexRef . current = index ;
353- setCurrentPage ( requiredPage ) ;
354- return ;
355- }
356-
357327 const itemIndex = item . index ?? - 1 ;
358328 const sectionIndex = item . sectionIndex ?? - 1 ;
359329 let viewOffsetToKeepFocusedItemAtTopOfViewableArea = 0 ;
@@ -370,11 +340,10 @@ function BaseSelectionList<TItem extends ListItem>(
370340 }
371341
372342 listRef . current . scrollToLocation ( { sectionIndex, itemIndex, animated, viewOffset : variables . contentHeaderHeight - viewOffsetToKeepFocusedItemAtTopOfViewableArea } ) ;
373- pendingScrollIndexRef . current = null ;
374343 } ,
375344
376345 // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
377- [ flattenedSections . allOptions , currentPage ] ,
346+ [ flattenedSections . allOptions ] ,
378347 ) ;
379348
380349 const [ disabledArrowKeyIndexes , setDisabledArrowKeyIndexes ] = useState ( flattenedSections . disabledArrowKeyOptionsIndexes ) ;
@@ -387,21 +356,6 @@ function BaseSelectionList<TItem extends ListItem>(
387356 // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
388357 } , [ flattenedSections . disabledArrowKeyOptionsIndexes ] ) ;
389358
390- /** Check whether there is a need to scroll to an item and if all items are loaded */
391- useEffect ( ( ) => {
392- if ( pendingScrollIndexRef . current === null ) {
393- return ;
394- }
395-
396- const indexToScroll = pendingScrollIndexRef . current ;
397- const targetItem = flattenedSections . allOptions . at ( indexToScroll ) ;
398-
399- if ( targetItem && indexToScroll < CONST . MAX_SELECTION_LIST_PAGE_LENGTH * currentPage ) {
400- pendingScrollIndexRef . current = null ;
401- scrollToIndex ( indexToScroll , true ) ;
402- }
403- } , [ currentPage , scrollToIndex , flattenedSections . allOptions ] ) ;
404-
405359 const debouncedScrollToIndex = useMemo ( ( ) => lodashDebounce ( scrollToIndex , CONST . TIMING . LIST_SCROLLING_DEBOUNCE_TIME , { leading : true , trailing : true } ) , [ scrollToIndex ] ) ;
406360
407361 const setHasKeyBeenPressed = useCallback ( ( ) => {
@@ -816,28 +770,17 @@ function BaseSelectionList<TItem extends ListItem>(
816770 ) {
817771 return ;
818772 }
819- // Reset the current page to 1 when the user types something
820- if ( prevTextInputValue !== textInputValue ) {
821- setCurrentPage ( 1 ) ;
822- }
823-
824- // When clearing the search, scroll to the selected item if one exists
825- if ( prevTextInputValue !== '' && textInputValue === '' ) {
826- const foundSelectedItemIndex = flattenedSections . allOptions . findIndex ( isItemSelected ) ;
827- if ( foundSelectedItemIndex !== - 1 ) {
828- updateAndScrollToFocusedIndex ( foundSelectedItemIndex ) ;
829- return ;
830- }
831- }
832-
833773 // Remove the focus if the search input is empty and prev search input not empty or selected options length is changed (and allOptions length remains the same)
834774 // else focus on the first non disabled item
835775 const newSelectedIndex =
836- ( prevTextInputValue !== '' && textInputValue === '' ) ||
776+ ( isEmpty ( prevTextInputValue ) && textInputValue === '' ) ||
837777 ( flattenedSections . selectedOptions . length !== prevSelectedOptionsLength && prevAllOptionsLength === flattenedSections . allOptions . length )
838778 ? - 1
839779 : 0 ;
840780
781+ // Reset the current page to 1 when the user types something
782+ setCurrentPage ( 1 ) ;
783+
841784 updateAndScrollToFocusedIndex ( newSelectedIndex ) ;
842785 } , [
843786 canSelectMultiple ,
@@ -849,8 +792,6 @@ function BaseSelectionList<TItem extends ListItem>(
849792 prevSelectedOptionsLength ,
850793 prevAllOptionsLength ,
851794 shouldUpdateFocusedIndex ,
852- flattenedSections . allOptions ,
853- isItemSelected ,
854795 ] ) ;
855796
856797 useEffect (
0 commit comments