Skip to content

Make paginationPage an optional, controllable property #1037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
paginationResetDefaultPage = defaultProps.paginationResetDefaultPage,
paginationPerPage = defaultProps.paginationPerPage,
paginationRowsPerPageOptions = defaultProps.paginationRowsPerPageOptions,
paginationPage = defaultProps.paginationPage,
paginationIconLastPage = defaultProps.paginationIconLastPage,
paginationIconFirstPage = defaultProps.paginationIconFirstPage,
paginationIconNext = defaultProps.paginationIconNext,
Expand Down Expand Up @@ -148,7 +149,7 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
selectedColumn: defaultSortColumn,
toggleOnSelectedRowsChange: false,
sortDirection: defaultSortDirection,
currentPage: paginationDefaultPage,
currentPage: paginationPage !== null ? paginationPage : paginationDefaultPage,
rowsPerPage: paginationPerPage,
selectedRowsFlag: false,
contextMessage: defaultProps.contextMessage,
Expand Down Expand Up @@ -296,6 +297,12 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
handleChangePage(paginationDefaultPage);
}, [paginationDefaultPage, paginationResetDefaultPage]);

useDidUpdateEffect(() => {
if (paginationPage !== null && paginationPage !== currentPage) {
handleChangePage(paginationPage);
}
}, [paginationPage]);

useDidUpdateEffect(() => {
if (pagination && paginationServer && paginationTotalRows > 0) {
const updatedPage = getNumberOfPages(paginationTotalRows, rowsPerPage);
Expand Down
67 changes: 67 additions & 0 deletions src/DataTable/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,73 @@ describe('DataTable::Pagination', () => {

expect(container.firstChild).toMatchSnapshot();
});

test('should call onChangePage when paginationPage is used to control the current page', () => {
const onChangePageMock = jest.fn();
const mock = dataMock();
const { container } = render(
<DataTable
data={mock.data}
columns={mock.columns}
paginationPage={1} // force page 1
paginationPerPage={1} // force 2 pages
paginationRowsPerPageOptions={[1, 2]} // force 2 pages
pagination
onChangePage={onChangePageMock}
/>,
);

fireEvent.click(container.querySelector('button#pagination-next-page') as HTMLButtonElement);
expect(onChangePageMock).toBeCalledWith(2, 2);
});

test('should render correctly when paginationPage is used to control the current page', () => {
const mock = dataMock();
const { container, rerender } = render(
<DataTable
data={mock.data}
columns={mock.columns}
paginationPage={1} // force page 1
paginationPerPage={1} // force 2 pages
paginationRowsPerPageOptions={[1, 2]} // force 2 pages
pagination
/>,
);

rerender(
<DataTable
data={mock.data}
columns={mock.columns}
paginationPage={2} // force page 2
paginationPerPage={1} // force 2 pages
paginationRowsPerPageOptions={[1, 2]} // force 2 pages
pagination
/>,
);

// expect to be on page 2
expect(container.firstChild).toMatchSnapshot();
expect(container.querySelector('div[id="row-2"]')).not.toBeNull();
});

test('should ignore paginationDefaultPage if paginationPage is set on first render', () => {
const mock = dataMock();
const { container } = render(
<DataTable
data={mock.data}
columns={mock.columns}
paginationPage={2} // force page 2
paginationDefaultPage={1}
paginationPerPage={1} // force 2 pages
paginationRowsPerPageOptions={[1, 2]} // force 2 pages
pagination
/>,
);

// expect to be on page 2
expect(container.firstChild).toMatchSnapshot();
expect(container.querySelector('div[id="row-2"]')).not.toBeNull();
});
});

describe('DataTable::subHeader', () => {
Expand Down
Loading