Skip to content
Merged
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: 6 additions & 3 deletions packages/material-react-table/src/hooks/useMRT_Effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ export const useMRT_Effects = <TData extends MRT_RowData>(
useEffect(() => {
if (!enablePagination || isLoading || showSkeletons) return;
const { pageIndex, pageSize } = pagination;
const firstVisibleRowIndex = pageIndex * pageSize;
if (firstVisibleRowIndex >= totalRowCount) {
table.setPageIndex(Math.ceil(totalRowCount / pageSize) - 1);
const totalPages: number =
totalRowCount > 0 ? Math.ceil(totalRowCount / pageSize) : 1;
const isOutOfBounds: boolean = pageIndex < 0 || pageIndex >= totalPages;

if (isOutOfBounds) {
table.setPageIndex(totalPages - 1);
}
}, [totalRowCount, enablePagination, isLoading, showSkeletons]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import MenuItem from '@mui/material/MenuItem';
import {
type MRT_ColumnDef,
type MRT_ColumnFiltersState,
MRT_PaginationState,
MaterialReactTable,
} from '../../src';
import { faker } from '@faker-js/faker';
import { type Meta } from '@storybook/react';
import { Updater } from '@tanstack/react-table';

const meta: Meta = {
title: 'Fixed Bugs/useEffects',
Expand Down Expand Up @@ -323,3 +325,42 @@ export const DelayedFacetedValues = () => {
/>
);
};

export const PreventUnnecessaryPaginationChangeByOutOfBoundsCheck = () => {
const [pagination, setPagination] = useState({
pageIndex: 0,
pageSize: 5,
});

const columns: MRT_ColumnDef<Person>[] = [
{
accessorKey: 'firstName',
header: 'First Name',
},
{
accessorKey: 'lastName',
header: 'Last Name',
},
{
accessorKey: 'address',
header: 'Address',
},
];

const handlePaginationChange = (updater: Updater<MRT_PaginationState>) => {
console.log('Pagination change should not be triggered');
setPagination(updater);
};

return (
<MaterialReactTable
columns={columns}
data={[]}
manualPagination={true}
rowCount={0}
enablePagination
onPaginationChange={handlePaginationChange}
state={{ pagination }}
/>
);
};
Loading