Skip to content

Commit b42c40a

Browse files
committed
fix: Remove condition clamping pageIndex
1 parent fdb530d commit b42c40a

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/__tests__/operations/paginate.test.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,12 @@ 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 above the maximum
21-
let {
22-
items: processed,
23-
pagesCount,
24-
actualPageIndex,
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-
3120
// Page number is below the minimum
32-
({
21+
let {
3322
items: processed,
3423
pagesCount,
3524
actualPageIndex,
36-
} = processItems(items, { currentPageIndex: 0 }, { pagination: {} }));
25+
} = processItems(items, { currentPageIndex: 0 }, { pagination: {} });
3726
expect(actualPageIndex).toEqual(1);
3827
expect(pagesCount).toEqual(3);
3928
expect(processed).toHaveLength(10);

src/__tests__/stubs.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ const Table = React.forwardRef<CollectionRef, TableProps>(
8181
ref: React.Ref<CollectionRef>
8282
) => {
8383
const scrollToTop = () => {
84-
spy && spy();
84+
if (spy) {
85+
spy();
86+
}
8587
};
8688
React.useImperativeHandle(ref, () => ({
8789
scrollToTop,
@@ -245,7 +247,11 @@ function Pagination({
245247
return (
246248
<ul>
247249
<li>
248-
<button data-testid="previous-page" disabled={disabled} onClick={() => changePage(currentPageIndex - 1)}>
250+
<button
251+
data-testid="previous-page"
252+
disabled={disabled || currentPageIndex === 1}
253+
onClick={() => changePage(currentPageIndex - 1)}
254+
>
249255
&lt;
250256
</button>
251257
</li>
@@ -258,7 +264,11 @@ function Pagination({
258264
<span data-testid="pages-count">{pagesCount}</span>
259265
</li>
260266
<li>
261-
<button data-testid="next-page" disabled={disabled} onClick={() => changePage(currentPageIndex + 1)}>
267+
<button
268+
data-testid="next-page"
269+
disabled={disabled || currentPageIndex === pagesCount}
270+
onClick={() => changePage(currentPageIndex + 1)}
271+
>
262272
&gt;
263273
</button>
264274
</li>

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('1');
169-
expect(getVisibleItems()[0]).toEqual('1');
168+
expect(getCurrentPage()).toEqual('2');
169+
expect(getVisibleItems()[0]).toEqual('11');
170170
});
171171

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

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 || pageIndex > pagesCount || Number.isNaN(pageIndex)) {
19+
if (pageIndex < 1 || Number.isNaN(pageIndex)) {
2020
pageIndex = 1;
2121
}
2222
return { pageSize, pagesCount, pageIndex };

0 commit comments

Comments
 (0)