Skip to content

Commit 5212514

Browse files
authored
feat: add controlled paginationPage prop and update API documentation (jbetancur#1316)
1 parent 41735f7 commit 5212514

6 files changed

Lines changed: 84 additions & 2 deletions

File tree

apps/docs/src/pages/docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ Complete reference for every prop, type, and export in `react-data-table-compone
8686
| `paginationPerPage` | `number` | `10` | Rows shown per page. |
8787
| `paginationRowsPerPageOptions` | `number[]` | `[10,15,20,25,30]` | Options in the rows-per-page dropdown. |
8888
| `paginationDefaultPage` | `number` | `1` | Initial active page. |
89-
| `paginationResetDefaultPage` | `boolean` | `false` | Toggle to reset to page 1 (e.g. after a filter change). |
89+
| `paginationPage` | `number` | - | Controlled active page. When provided, the table navigates to this page whenever the value changes. Use together with `onChangePage` to keep them in sync. |
90+
| `paginationResetDefaultPage` | `boolean` | `false` | Toggle to reset to page 1 (e.g. after a filter change). Prefer `paginationPage` for new code. |
9091
| `paginationTotalRows` | `number` | - | Total row count for server-side pagination. |
9192
| `paginationServer` | `boolean` | `false` | Delegate page changes to `onChangePage` / `onChangeRowsPerPage`. |
9293
| `paginationServerOptions` | `PaginationServerOptions` | - | Selection-persistence options for server-side mode. |

apps/docs/src/pages/docs/changelog.astro

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ import CodeBlock from '../../components/CodeBlock.astro';
1111
<a href="https://github.com/jbetancur/react-data-table-component/commits/master" target="_blank" rel="noreferrer">repository on GitHub</a>.
1212
</p>
1313

14-
<h2>8.1.0 (current)</h2>
14+
<h2>8.2.0 (unreleased)</h2>
15+
16+
<h3>New features</h3>
17+
<ul>
18+
<li>
19+
<code>paginationPage</code> — controlled active-page prop. Set it to navigate the table
20+
programmatically (e.g. reset to page 1 after a filter change). Use together with
21+
<code>onChangePage</code> to keep them in sync.
22+
→ <a href="/docs/api#pagination">API reference</a>
23+
</li>
24+
</ul>
25+
26+
<h2>8.1.0</h2>
1527

1628
<p>
1729
Additive feature release. No breaking changes. See

src/__tests__/DataTable.test.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,63 @@ describe('DataTable::Pagination', () => {
19211921
expect(getByText('Todos')).not.toBeNull();
19221922
});
19231923

1924+
test('should navigate to the specified page when paginationPage changes', () => {
1925+
const mock = dataMock();
1926+
const { container, rerender } = render(
1927+
<DataTable
1928+
data={mock.data}
1929+
columns={mock.columns}
1930+
paginationPerPage={1}
1931+
paginationRowsPerPageOptions={[1, 2]}
1932+
pagination
1933+
paginationPage={1}
1934+
/>,
1935+
);
1936+
1937+
rerender(
1938+
<DataTable
1939+
data={mock.data}
1940+
columns={mock.columns}
1941+
paginationPerPage={1}
1942+
paginationRowsPerPageOptions={[1, 2]}
1943+
pagination
1944+
paginationPage={2}
1945+
/>,
1946+
);
1947+
1948+
expect(container.querySelector('div[id="row-2"]')).not.toBeNull();
1949+
});
1950+
1951+
test('should call onChangePage when paginationPage changes', () => {
1952+
const mock = dataMock();
1953+
const onChangePage = vi.fn();
1954+
const { rerender } = render(
1955+
<DataTable
1956+
data={mock.data}
1957+
columns={mock.columns}
1958+
paginationPerPage={1}
1959+
paginationRowsPerPageOptions={[1, 2]}
1960+
pagination
1961+
paginationPage={1}
1962+
onChangePage={onChangePage}
1963+
/>,
1964+
);
1965+
1966+
rerender(
1967+
<DataTable
1968+
data={mock.data}
1969+
columns={mock.columns}
1970+
paginationPerPage={1}
1971+
paginationRowsPerPageOptions={[1, 2]}
1972+
pagination
1973+
paginationPage={2}
1974+
onChangePage={onChangePage}
1975+
/>,
1976+
);
1977+
1978+
expect(onChangePage).toHaveBeenCalledWith(2, mock.data.length);
1979+
});
1980+
19241981
test('should render correctly when paginationResetDefaultPage is toggled', () => {
19251982
const mock = dataMock();
19261983
const { container, rerender } = render(

src/components/DataTable.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function DataTableInner<T>(props: TableProps<T>, ref: React.ForwardedRef<DataTab
5757
paginationServerOptions = defaultProps.paginationServerOptions,
5858
paginationTotalRows = defaultProps.paginationTotalRows,
5959
paginationDefaultPage = defaultProps.paginationDefaultPage,
60+
paginationPage,
6061
paginationResetDefaultPage = defaultProps.paginationResetDefaultPage,
6162
paginationPerPage = defaultProps.paginationPerPage,
6263
paginationRowsPerPageOptions = defaultProps.paginationRowsPerPageOptions,
@@ -243,6 +244,7 @@ function DataTableInner<T>(props: TableProps<T>, ref: React.ForwardedRef<DataTab
243244
selectableRowsVisibleOnly,
244245
selectableRowSelected,
245246
clearSelectedRows,
247+
paginationPage,
246248
paginationResetDefaultPage,
247249
controlledSelectedRows,
248250
onSelectedRowsChange,

src/hooks/useTableState.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface UseTableStateProps<T> {
3131
selectableRowsVisibleOnly: boolean;
3232
selectableRowSelected: ((row: T) => boolean) | null;
3333
clearSelectedRows: boolean;
34+
paginationPage?: number;
3435
paginationResetDefaultPage: boolean;
3536
/** Controlled selection. When provided, internal selection state is overridden. */
3637
controlledSelectedRows?: T[];
@@ -72,6 +73,7 @@ export default function useTableState<T>(props: UseTableStateProps<T>): UseTable
7273
selectableRowsVisibleOnly,
7374
selectableRowSelected,
7475
clearSelectedRows,
76+
paginationPage,
7577
paginationResetDefaultPage,
7678
controlledSelectedRows,
7779
onSelectedRowsChange,
@@ -173,6 +175,13 @@ export default function useTableState<T>(props: UseTableStateProps<T>): UseTable
173175
handleChangePage(paginationDefaultPage);
174176
}, [paginationDefaultPage, paginationResetDefaultPage]);
175177

178+
// Effect: Handle controlled page prop
179+
useDidUpdateEffect(() => {
180+
if (paginationPage !== undefined) {
181+
handleChangePage(paginationPage);
182+
}
183+
}, [paginationPage]);
184+
176185
// Effect: Recalculate page when total rows change (server pagination)
177186
useDidUpdateEffect(() => {
178187
if (pagination && paginationServer && paginationTotalRows > 0) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ type PaginationProps = {
122122
*/
123123
paginationIcons?: PaginationIcons;
124124
paginationPerPage?: number;
125+
paginationPage?: number;
125126
paginationResetDefaultPage?: boolean;
126127
paginationRowsPerPageOptions?: number[];
127128
paginationServer?: boolean;

0 commit comments

Comments
 (0)