Skip to content

Commit

Permalink
[DataGrid] Fix incorrect computation of lastPage in GridPagination (
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi authored and thomasmoon committed Sep 6, 2024
1 parent e173130 commit a8324ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/x-data-grid/src/components/GridPagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export const GridPagination = React.forwardRef<unknown, Partial<TablePaginationP
[rootProps.rowCount, visibleTopLevelRowCount],
);

const lastPage = React.useMemo(
() => Math.floor(rowCount / (paginationModel.pageSize || 1)),
[rowCount, paginationModel.pageSize],
);
const lastPage = React.useMemo(() => {
const calculatedValue = Math.ceil(rowCount / (paginationModel.pageSize || 1)) - 1;
return Math.max(0, calculatedValue);
}, [rowCount, paginationModel.pageSize]);

const handlePageSizeChange = React.useCallback(
(event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => {
Expand Down
22 changes: 22 additions & 0 deletions packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -685,4 +685,26 @@ describe('<DataGrid /> - Pagination', () => {
expect(getColumnValues(0)).to.deep.equal(['0', '1', '2', '3', '4']);
});
});

// See https://github.com/mui/mui-x/issues/11247
it('should not throw on deleting the last row of a page > 0', () => {
const columns = [{ field: 'name' }];
const rows = [
{ id: 0, name: 'a' },
{ id: 1, name: 'b' },
{ id: 2, name: 'c' },
{ id: 3, name: 'd' },
];
expect(() => {
const { setProps } = render(
<DataGrid
columns={columns}
rows={rows}
initialState={{ pagination: { paginationModel: { pageSize: 2, page: 1 } } }}
pageSizeOptions={[2]}
/>,
);
setProps({ rows: rows.slice(0, 2) });
}).not.to.throw();
});
});

0 comments on commit a8324ce

Please sign in to comment.