Skip to content

Commit c061e81

Browse files
authored
Merge pull request Expensify#65846 from callstack-internal/feat/65634-reduce-amount-of-items-per-page-of-selection-list
feat: Reduce amount of items displayed per page of SelectionList
2 parents 94abb71 + bb3b316 commit c061e81

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/CONST/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5197,7 +5197,7 @@ const CONST = {
51975197
* The maximum count of items per page for SelectionList.
51985198
* When paginate, it multiplies by page number.
51995199
*/
5200-
MAX_SELECTION_LIST_PAGE_LENGTH: 500,
5200+
MAX_SELECTION_LIST_PAGE_LENGTH: 50,
52015201

52025202
/**
52035203
* Bank account names

src/components/SelectionList/BaseSelectionList.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,35 @@ function BaseSelectionList<TItem extends ListItem>(
196196
return canShowProductTrainingTooltip && isFocused;
197197
}, [canShowProductTrainingTooltip, isFocused]);
198198

199+
// Calculate initial page count so selected item is loaded
200+
const initialPageCount = useMemo(() => {
201+
if (canSelectMultiple || sections.length === 0) {
202+
return 1;
203+
}
204+
205+
let currentIndex = 0;
206+
for (const section of sections) {
207+
if (section.data) {
208+
for (const item of section.data) {
209+
if (isItemSelected(item)) {
210+
return Math.floor(currentIndex / CONST.MAX_SELECTION_LIST_PAGE_LENGTH) + 1;
211+
}
212+
currentIndex++;
213+
}
214+
}
215+
}
216+
217+
return 1;
218+
}, [sections, canSelectMultiple, isItemSelected]);
219+
220+
useEffect(() => {
221+
if (initialPageCount < 1) {
222+
return;
223+
}
224+
225+
setCurrentPage(initialPageCount);
226+
}, [initialPageCount]);
227+
199228
/**
200229
* Iterates through the sections and items inside each section, and builds 4 arrays along the way:
201230
* - `allOptions`: Contains all the items in the list, flattened, regardless of section
@@ -779,7 +808,9 @@ function BaseSelectionList<TItem extends ListItem>(
779808
: 0;
780809

781810
// Reset the current page to 1 when the user types something
782-
setCurrentPage(1);
811+
if (prevTextInputValue !== textInputValue) {
812+
setCurrentPage(1);
813+
}
783814

784815
updateAndScrollToFocusedIndex(newSelectedIndex);
785816
}, [

0 commit comments

Comments
 (0)