Skip to content

Commit 9d5844e

Browse files
committed
fix: Hide new pageIndex logic behind flag
1 parent b6007dd commit 9d5844e

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

src/__tests__/operations/paginate.test.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ test('default pagination', () => {
1717
test('should reset the currentPageIndex to 1 when out of range', () => {
1818
const items = generateItems(25);
1919

20-
// Page number is below the minimum
20+
// Page number is above the maximum
2121
let {
2222
items: processed,
2323
pagesCount,
2424
actualPageIndex,
25-
} = processItems(items, { currentPageIndex: 0 }, { pagination: {} });
25+
} = processItems(items, { currentPageIndex: 4 }, { pagination: {} });
26+
expect(actualPageIndex).toEqual(1);
27+
expect(pagesCount).toEqual(3);
28+
expect(processed).toHaveLength(10);
29+
expect(processed[0]).toEqual(items[0]);
30+
31+
// Page number is below the minimum
32+
({
33+
items: processed,
34+
pagesCount,
35+
actualPageIndex,
36+
} = processItems(items, { currentPageIndex: 0 }, { pagination: {} }));
2637
expect(actualPageIndex).toEqual(1);
2738
expect(pagesCount).toEqual(3);
2839
expect(processed).toHaveLength(10);
@@ -40,6 +51,19 @@ test('should reset the currentPageIndex to 1 when out of range', () => {
4051
expect(processed[0]).toEqual(items[0]);
4152
});
4253

54+
test('should not reset the currentPageIndex if `allowPageOutOfRange=true`', () => {
55+
const items = generateItems(25);
56+
57+
const {
58+
items: processed,
59+
pagesCount,
60+
actualPageIndex,
61+
} = processItems(items, { currentPageIndex: 4 }, { pagination: { allowPageOutOfRange: true } });
62+
expect(actualPageIndex).toEqual(4);
63+
expect(pagesCount).toEqual(3);
64+
expect(processed).toHaveLength(0);
65+
});
66+
4367
test('displays all items of it is less than the page size', () => {
4468
const items = generateItems(7);
4569
const { items: processed, pagesCount } = processItems(items, {}, { pagination: {} });
@@ -87,18 +111,6 @@ test('displays the last page when the number of items is divisible by page size'
87111
expect(processed[9]).toEqual(items[19]);
88112
});
89113

90-
test('displays an out of range page', () => {
91-
const items = generateItems(20);
92-
const {
93-
items: processed,
94-
pagesCount,
95-
actualPageIndex,
96-
} = processItems(items, { currentPageIndex: 3 }, { pagination: {} });
97-
expect(actualPageIndex).toEqual(3);
98-
expect(pagesCount).toEqual(2);
99-
expect(processed).toHaveLength(0);
100-
});
101-
102114
test('supports custom page size', () => {
103115
const items = generateItems(35);
104116
const { items: processed, pagesCount } = processItems(

src/__tests__/use-collection.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ test('should prevent pagination from going out of bounds', () => {
165165
expect(getCurrentPage()).toEqual('2');
166166
expect(getVisibleItems()[0]).toEqual('11');
167167
fireEvent.click(findNextPage());
168-
expect(getCurrentPage()).toEqual('2');
169-
expect(getVisibleItems()[0]).toEqual('11');
168+
expect(getCurrentPage()).toEqual('1');
169+
expect(getVisibleItems()[0]).toEqual('1');
170170
});
171171

172172
test('should evoke scrollToTop from the ref on pagination, filtering, property filtering and sorting', () => {

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface UseCollectionOptions<T> {
4444
freeTextFiltering?: PropertyFilterFreeTextFiltering;
4545
};
4646
sorting?: { defaultState?: SortingState<T> };
47-
pagination?: { defaultPage?: number; pageSize?: number };
47+
pagination?: { defaultPage?: number; pageSize?: number; allowPageOutOfRange?: boolean };
4848
selection?: {
4949
defaultSelectedItems?: ReadonlyArray<T>;
5050
keepSelection?: boolean;

src/operations/pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function createPageProps<T>(
1616
const pageSize = pagination.pageSize ?? DEFAULT_PAGE_SIZE;
1717
const pagesCount = Math.ceil(items.length / pageSize);
1818
let pageIndex = currentPageIndex ?? 1;
19-
if (pageIndex < 1 || Number.isNaN(pageIndex)) {
19+
if (pageIndex < 1 || (pageIndex > pagesCount && !pagination.allowPageOutOfRange) || Number.isNaN(pageIndex)) {
2020
pageIndex = 1;
2121
}
2222
return { pageSize, pagesCount, pageIndex };

0 commit comments

Comments
 (0)