Skip to content
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

[DataGrid] Fix onPageChange and onPageSizeChange event trigger #1034

Merged
merged 5 commits into from
Feb 12, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,21 @@ export const usePagination = (apiRef: ApiRef): void => {
}, [apiRef, dispatch, options.paginationMode]);

React.useEffect(() => {
setPage(options.page != null ? options.page : 0);
}, [apiRef, options.page, setPage]);
const newPage = options.page != null ? options.page : 0;
dispatch(setPageActionCreator(newPage));
}, [dispatch, options.page]);

React.useEffect(() => {
if (!options.autoPageSize && options.pageSize) {
setPageSize(options.pageSize);
dispatch(setPageSizeActionCreator(options.pageSize));
}
}, [options.autoPageSize, options.pageSize, logger, setPageSize]);
}, [options.autoPageSize, options.pageSize, logger, dispatch]);

React.useEffect(() => {
if (options.autoPageSize && containerSizes && containerSizes?.viewportPageSize > 0) {
setPageSize(containerSizes?.viewportPageSize);
dispatch(setPageSizeActionCreator(containerSizes?.viewportPageSize));
}
}, [containerSizes, options.autoPageSize, setPageSize]);
}, [containerSizes, dispatch, options.autoPageSize]);

React.useEffect(() => {
dispatch(setRowCountActionCreator({ totalRowCount: visibleRowCount }));
Expand Down
50 changes: 32 additions & 18 deletions packages/grid/data-grid/src/tests/pagination.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
screen,
} from 'test/utils';
import { expect } from 'chai';
import { DataGrid, RowsProp } from '@material-ui/data-grid';
import { DataGrid, DataGridProps, RowsProp } from '@material-ui/data-grid';
import { getColumnValues } from 'test/utils/helperFn';
import { spy } from 'sinon';

Expand Down Expand Up @@ -65,17 +65,6 @@ describe('<DataGrid /> - Pagination', () => {
expect(cell).to.have.text('Addidas');
});

it('should trigger onPageChange once if page prop is set', () => {
const onPageChange = spy();

render(
<div style={{ width: 300, height: 300 }}>
<DataGrid {...baselineProps} page={1} onPageChange={onPageChange} pageSize={1} />
</div>,
);
expect(onPageChange.callCount).to.equal(1);
});

it('should trigger onPageChange when clicking on next page', () => {
const onPageChange = spy();

Expand Down Expand Up @@ -110,15 +99,40 @@ describe('<DataGrid /> - Pagination', () => {
expect(onPageChange.callCount).to.equal(2);
});

it('should not trigger onPageChange on initialisation and rendering of the first and default page', () => {
it('should not trigger onPageChange on initialisation or on page prop change', () => {
const onPageChange = spy();

render(
<div style={{ width: 300, height: 300 }}>
<DataGrid {...baselineProps} onPageChange={onPageChange} pageSize={1} />
</div>,
);
function Test(props: Partial<DataGridProps>) {
return (
<div style={{ width: 300, height: 300 }}>
<DataGrid {...baselineProps} {...props} />
</div>
);
}

const { setProps } = render(<Test page={1} pageSize={1} onPageChange={onPageChange} />);
expect(onPageChange.callCount).to.equal(0);
setProps({ page: 2 });
expect(onPageChange.callCount).to.equal(0);
});

it('should not trigger onPageSizeChange on initialisation or on pageSize prop change', () => {
const onPageSizeChange = spy();

function Test(props: Partial<DataGridProps>) {
return (
<div style={{ width: 300, height: 300 }}>
<DataGrid {...baselineProps} {...props} />
</div>
);
}

const { setProps } = render(
<Test onPageSizeChange={onPageSizeChange} pageSize={1} page={1} />,
);
expect(onPageSizeChange.callCount).to.equal(0);
setProps({ pageSize: 2 });
expect(onPageSizeChange.callCount).to.equal(0);
});

it('should support server side pagination', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/storybook/src/stories/grid-pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function PaginationApiTests() {
const [autosize, setAutoSize] = React.useState(false);

React.useEffect(() => {
return apiRef.current.onPageChange(action('pageChange'));
return apiRef.current.onPageChange(action('apiRef: onPageChange'));
}, [apiRef, data]);

const backToFirstPage = () => {
Expand Down Expand Up @@ -126,6 +126,8 @@ export function PaginationApiTests() {
pagination
pageSize={myPageSize}
autoPageSize={autosize}
onPageChange={action('prop: onPageChange')}
onPageSizeChange={action('prop: onPageSizeChange')}
components={{
Pagination: ({ state }) => (
<Pagination
Expand Down