@@ -186,18 +186,39 @@ function BaseSelectionList<TItem extends ListItem>(
186186 const incrementPage = ( ) => setCurrentPage ( ( prev ) => prev + 1 ) ;
187187
188188 const isItemSelected = useCallback (
189- ( item : TItem ) => {
190- if ( item . isSelected !== undefined ) {
191- return item . isSelected ;
192- }
193- if ( isSelected ) {
194- return isSelected ( item ) ;
195- }
196- return selectedItems . includes ( item . keyForList ?? '' ) ;
197- } ,
198- [ isSelected , selectedItems ] ,
189+ ( item : TItem ) => item . isSelected ?? ( ( isSelected ?.( item ) ?? selectedItems . includes ( item . keyForList ?? '' ) ) && canSelectMultiple ) ,
190+ [ isSelected , selectedItems , canSelectMultiple ] ,
199191 ) ;
200192
193+ // Calculate initial page count so selected item is loaded
194+ const initialPageCount = useMemo ( ( ) => {
195+ if ( canSelectMultiple || sections . length === 0 ) {
196+ return 1 ;
197+ }
198+
199+ let currentIndex = 0 ;
200+ for ( const section of sections ) {
201+ if ( section . data ) {
202+ for ( const item of section . data ) {
203+ if ( isItemSelected ( item ) ) {
204+ return Math . floor ( currentIndex / CONST . MAX_SELECTION_LIST_PAGE_LENGTH ) + 1 ;
205+ }
206+ currentIndex ++ ;
207+ }
208+ }
209+ }
210+
211+ return 1 ;
212+ } , [ sections , canSelectMultiple , isItemSelected ] ) ;
213+
214+ useEffect ( ( ) => {
215+ if ( initialPageCount < 1 ) {
216+ return ;
217+ }
218+
219+ setCurrentPage ( initialPageCount ) ;
220+ } , [ initialPageCount ] ) ;
221+
201222 /**
202223 * Iterates through the sections and items inside each section, and builds 4 arrays along the way:
203224 * - `allOptions`: Contains all the items in the list, flattened, regardless of section
@@ -225,20 +246,12 @@ function BaseSelectionList<TItem extends ListItem>(
225246 offset += sectionHeaderHeight ;
226247
227248 section . data ?. forEach ( ( item , optionIndex ) => {
228- // Add item to the general flattened array. Selected items should be in front of the array.
229- if ( isItemSelected ( item ) && ! canSelectMultiple ) {
230- allOptions . unshift ( {
231- ...item ,
232- sectionIndex,
233- index : 0 ,
234- } ) ;
235- } else {
236- allOptions . push ( {
237- ...item ,
238- sectionIndex,
239- index : optionIndex ,
240- } ) ;
241- }
249+ // Add item to the general flattened array
250+ allOptions . push ( {
251+ ...item ,
252+ sectionIndex,
253+ index : optionIndex ,
254+ } ) ;
242255
243256 // If disabled, add to the disabled indexes array
244257 const isItemDisabled = ! ! section . isDisabled || ( item . isDisabled && ! isItemSelected ( item ) ) ;
@@ -292,34 +305,12 @@ function BaseSelectionList<TItem extends ListItem>(
292305 let remainingOptionsLimit = CONST . MAX_SELECTION_LIST_PAGE_LENGTH * currentPage ;
293306 const processedSections = getSectionsWithIndexOffset (
294307 sections . map ( ( section ) => {
295- if ( isEmpty ( section . data ) || remainingOptionsLimit <= 0 ) {
296- return {
297- ...section ,
298- data : [ ] ,
299- } ;
300- }
301-
302- let sectionData = section . data ;
303- if ( ! canSelectMultiple ) {
304- const sectionSelectedItems : TItem [ ] = [ ] ;
305- const sectionUnselectedItems : TItem [ ] = [ ] ;
306- section . data . forEach ( ( item ) => {
307- if ( isItemSelected ( item ) ) {
308- sectionSelectedItems . push ( item ) ;
309- } else {
310- sectionUnselectedItems . push ( item ) ;
311- }
312- } ) ;
313-
314- sectionData = [ ...sectionSelectedItems , ...sectionUnselectedItems ] ;
315- }
316-
317- const slicedData = sectionData . slice ( 0 , remainingOptionsLimit ) ;
318- remainingOptionsLimit -= slicedData . length ;
308+ const data = ! isEmpty ( section . data ) && remainingOptionsLimit > 0 ? section . data . slice ( 0 , remainingOptionsLimit ) : [ ] ;
309+ remainingOptionsLimit -= data . length ;
319310
320311 return {
321312 ...section ,
322- data : slicedData ,
313+ data,
323314 } ;
324315 } ) ,
325316 ) ;
0 commit comments