Skip to content

Commit d639c6f

Browse files
authored
Merge pull request Expensify#66780 from Krishna2323/krishna2323/issue/65977
fix: Tags - List jumps to “Show More” when selecting tag after 500.
2 parents b9c09eb + 181f146 commit d639c6f

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

src/components/SelectionList/BaseSelectionList.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,14 @@ function BaseSelectionList<TItem extends ListItem>(
809809
const prevSelectedOptionsLength = usePrevious(flattenedSections.selectedOptions.length);
810810
const prevAllOptionsLength = usePrevious(flattenedSections.allOptions.length);
811811

812+
useEffect(() => {
813+
if (prevTextInputValue === textInputValue) {
814+
return;
815+
}
816+
// Reset the current page to 1 when the user types something
817+
setCurrentPage(1);
818+
}, [textInputValue, prevTextInputValue]);
819+
812820
useEffect(() => {
813821
// Avoid changing focus if the textInputValue remains unchanged.
814822
if (
@@ -831,11 +839,6 @@ function BaseSelectionList<TItem extends ListItem>(
831839
}
832840
}
833841

834-
// Reset the current page to 1 when the user types something
835-
if (prevTextInputValue !== textInputValue) {
836-
setCurrentPage(1);
837-
}
838-
839842
// 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)
840843
// else focus on the first non disabled item
841844
const newSelectedIndex =

tests/unit/BaseSelectionListTest.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,62 @@ describe('BaseSelectionList', () => {
211211
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}70`)).toBeTruthy();
212212
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}70`)).toBeSelected();
213213
});
214+
215+
it('does not reset page when only selectedOptions changes', () => {
216+
const {rerender} = render(
217+
<BaseListItemRenderer
218+
sections={[{data: largeMockSections}]}
219+
canSelectMultiple={false}
220+
initialNumToRender={110}
221+
/>,
222+
);
223+
224+
// Click Show More button
225+
fireEvent.press(screen.getByText('common.showMore'));
226+
227+
rerender(
228+
<BaseListItemRenderer
229+
sections={[{data: largeMockSections.map((item, index) => ({...item, isSelected: index === 3}))}]}
230+
canSelectMultiple={false}
231+
initialNumToRender={110}
232+
/>,
233+
);
234+
235+
// Should now show items from second page
236+
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}51`)).toBeTruthy();
237+
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}62`)).toBeTruthy();
238+
239+
// Should not show, "Show more" button as we rendered whole list and search text was not changed
240+
expect(screen.queryByText('common.showMore')).toBeFalsy();
241+
});
242+
243+
it('should reset current page when text input changes', () => {
244+
const {rerender} = render(
245+
<BaseListItemRenderer
246+
sections={[{data: largeMockSections}]}
247+
canSelectMultiple={false}
248+
initialNumToRender={110}
249+
/>,
250+
);
251+
252+
// Click Show More button
253+
fireEvent.press(screen.getByText('common.showMore'));
254+
expect(screen.getByTestId(`${CONST.BASE_LIST_ITEM_TEST_ID}99`)).toBeTruthy();
255+
256+
// Rerender with changed `searchText` to trigger `setCurrentPage(1)`
257+
rerender(
258+
<BaseListItemRenderer
259+
sections={[{data: largeMockSections.map((item, index) => ({...item, isSelected: index === 3}))}]}
260+
canSelectMultiple={false}
261+
searchText="Item"
262+
initialNumToRender={110}
263+
/>,
264+
);
265+
266+
// Should not show the items from second page
267+
expect(screen.queryByText(`${CONST.BASE_LIST_ITEM_TEST_ID}52`)).toBeFalsy();
268+
269+
// Should show, "Show more" button as current page is reset
270+
expect(screen.getByText('common.showMore')).toBeOnTheScreen();
271+
});
214272
});

0 commit comments

Comments
 (0)