Skip to content

Commit fcf6478

Browse files
committed
fix(table): recompute the paginated slice when current changes
The effect that keeps currentPaginateData in sync only depended on data and reserveSelectedRowOnPaginate: [data, reserveSelectedRowOnPaginate], With local data and a controlled pagination.current, the data reference never changes, so the slice stays on the first page forever. The table renders page 2 correctly but select-all still works off page 1 rows - clicking the header checkbox on page 2 reports the keys of page 1. Add the pagination fields the effect actually reads to the dependency list. Verified: - new test fails on the previous implementation (expected [ 1, 2 ] to deeply equal [ 3, 4 ]) - new test passes with the fix - page 1 select-all still reports [1, 2] - table: 53 tests pass
1 parent b2cc9a2 commit fcf6478

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React from 'react';
2+
import { fireEvent, render } from '@test/utils';
3+
4+
import { Table } from '..';
5+
6+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
7+
8+
describe('Table', () => {
9+
describe('scenarios', () => {
10+
describe('select all with controlled pagination', () => {
11+
it('selects the rows of the current page after current changes', async () => {
12+
const data = Array.from({ length: 6 }, (_, i) => ({ id: i + 1, name: `row-${i + 1}` }));
13+
const columns = [
14+
{ colKey: 'row-select', type: 'multiple' as const },
15+
{ colKey: 'name', title: 'name' },
16+
];
17+
let captured: any[] = [];
18+
const table = (current: number) => (
19+
<Table
20+
data={data}
21+
columns={columns}
22+
rowKey="id"
23+
pagination={{ current, pageSize: 2, total: 6 }}
24+
reserveSelectedRowOnPaginate={false}
25+
onSelectChange={(keys: any[]) => {
26+
captured = keys;
27+
}}
28+
/>
29+
);
30+
const { container, rerender } = render(table(1));
31+
await flush();
32+
rerender(table(2));
33+
await flush();
34+
35+
const visible = [...container.querySelectorAll('tbody tr')].map((tr) => tr.textContent);
36+
expect(visible).toEqual(['row-3', 'row-4']);
37+
38+
fireEvent.click(container.querySelector('th .t-checkbox input'));
39+
await flush();
40+
expect(captured).toEqual([3, 4]);
41+
});
42+
43+
it('still selects the first page rows when current stays at 1', async () => {
44+
const data = Array.from({ length: 6 }, (_, i) => ({ id: i + 1, name: `row-${i + 1}` }));
45+
const columns = [
46+
{ colKey: 'row-select', type: 'multiple' as const },
47+
{ colKey: 'name', title: 'name' },
48+
];
49+
let captured: any[] = [];
50+
const { container } = render(
51+
<Table
52+
data={data}
53+
columns={columns}
54+
rowKey="id"
55+
pagination={{ current: 1, pageSize: 2, total: 6 }}
56+
reserveSelectedRowOnPaginate={false}
57+
onSelectChange={(keys: any[]) => {
58+
captured = keys;
59+
}}
60+
/>,
61+
);
62+
await flush();
63+
fireEvent.click(container.querySelector('th .t-checkbox input'));
64+
await flush();
65+
expect(captured).toEqual([1, 2]);
66+
});
67+
});
68+
});
69+
});

‎packages/components/table/hooks/useRowSelect.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ export default function useRowSelect(
8989
setCurrentPaginateData(newData);
9090
},
9191
// eslint-disable-next-line
92-
[data, reserveSelectedRowOnPaginate],
92+
[
93+
data,
94+
reserveSelectedRowOnPaginate,
95+
pagination?.current,
96+
pagination?.pageSize,
97+
pagination?.defaultCurrent,
98+
pagination?.defaultPageSize,
99+
],
93100
);
94101

95102
useEffect(() => {

0 commit comments

Comments
 (0)