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 setPage not working #284

Merged
merged 6 commits into from
Sep 15, 2020
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
24 changes: 24 additions & 0 deletions packages/grid/data-grid/src/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@ describe('<DataGrid />', () => {
container.firstChild.firstChild.firstChild,
);
});

it('should apply the page prop correctly', () => {
const rows = [
{
id: 0,
brand: 'Nike',
},
{
id: 1,
brand: 'Addidas',
},
{
id: 2,
brand: 'Puma',
},
];
render(
<div style={{ width: 300, height: 300 }}>
<DataGrid rows={rows} columns={defaultProps.columns} page={2} pageSize={1} />
</div>,
);
const cell = document.querySelector('[role="cell"][aria-colindex="0"]')!;
expect(cell).to.have.text('Addidas');
});
});
});

Expand Down
61 changes: 33 additions & 28 deletions packages/grid/x-grid-modules/src/hooks/features/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const usePagination = (
),
};
const stateRef = React.useRef(initialState);
const prevPageRef = React.useRef<number>(1);
const [state, dispatch] = React.useReducer(paginationReducer, initialState);

const updateState = React.useCallback(
Expand All @@ -74,16 +75,22 @@ export const usePagination = (

const setPage = React.useCallback(
(page: number) => {
page = stateRef.current.pageCount >= page ? page : stateRef.current.pageCount;
apiRef.current.renderPage(
stateRef.current.paginationMode === FeatureModeConstant.client ? page : 1,
);

let hasPageChanged = false;
if (stateRef.current.rowCount > 0) {
page = stateRef.current.pageCount >= page ? page : stateRef.current.pageCount;
apiRef.current.renderPage(
stateRef.current.paginationMode === FeatureModeConstant.client ? page : 1,
);
hasPageChanged = true;
}
const params: PageChangeParams = {
...stateRef.current,
page,
};
apiRef.current.publishEvent(PAGE_CHANGED, params);
if (hasPageChanged && prevPageRef.current !== page) {
apiRef.current.publishEvent(PAGE_CHANGED, params);
prevPageRef.current = page;
}
if (stateRef.current.page !== page) {
updateState({ page });
}
Expand Down Expand Up @@ -147,6 +154,17 @@ export const usePagination = (
}
}, [setPageSize, logger, getAutoPageSize]);

useApiEventHandler(apiRef, PAGE_CHANGED, options.onPageChange);
useApiEventHandler(apiRef, PAGESIZE_CHANGED, options.onPageSizeChange);

const onResize = React.useCallback(() => {
if (options.autoPageSize) {
resetAutopageSize();
}
}, [options.autoPageSize, resetAutopageSize]);

useApiEventHandler(apiRef, RESIZE, onResize);

React.useEffect(() => {
stateRef.current = state;
}, [state]);
Expand All @@ -157,24 +175,14 @@ export const usePagination = (
}
}, [apiRef, stateRef, apiRef.current?.isInitialised]);

React.useEffect(() => {
updateState({ paginationMode: options.paginationMode! });
}, [options.paginationMode, updateState]);

React.useEffect(() => {
setPage(options.page != null ? options.page : 1);
}, [options.page, setPage]);

React.useEffect(() => {
const rowCount = options.rowCount == null ? rows.length : options.rowCount;
if (rowCount !== state.rowCount) {
logger.info(`Options or rows change, recalculating pageCount and rowCount`);
const newPageCount = getPageCount(state.pageSize, rowCount);

updateState({ pageCount: newPageCount, rowCount });
if (state.page > newPageCount) {
setPage(newPageCount);
}
setPage(state.page);
}
}, [
rows.length,
Expand All @@ -187,6 +195,14 @@ export const usePagination = (
state.page,
]);

React.useEffect(() => {
updateState({ paginationMode: options.paginationMode! });
}, [options.paginationMode, updateState]);

React.useEffect(() => {
setPage(options.page != null ? options.page : 1);
}, [options.page, setPage]);

React.useEffect(() => {
if (
!options.autoPageSize &&
Expand All @@ -203,17 +219,6 @@ export const usePagination = (
}
}, [options.autoPageSize, resetAutopageSize, columns.visible.length]);

useApiEventHandler(apiRef, PAGE_CHANGED, options.onPageChange);
useApiEventHandler(apiRef, PAGESIZE_CHANGED, options.onPageSizeChange);

const onResize = React.useCallback(() => {
if (options.autoPageSize) {
resetAutopageSize();
}
}, [options.autoPageSize, resetAutopageSize]);

useApiEventHandler(apiRef, RESIZE, onResize);

const paginationApi: PaginationApi = {
setPageSize,
setPage,
Expand Down
38 changes: 37 additions & 1 deletion packages/grid/x-grid/src/XGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { screen, createClientRender, act, fireEvent } from 'test/utils';
import { expect } from 'chai';
import { XGrid } from '@material-ui/x-grid';
import { XGrid, useApiRef } from '@material-ui/x-grid';
import { useData } from 'packages/storybook/src/hooks/useData';

function getActiveCell() {
Expand Down Expand Up @@ -212,6 +212,42 @@ describe('<XGrid />', () => {
expect(row).to.not.have.class('Mui-selected');
expect(checkbox).to.have.property('checked', false);
});
it('should apply setPage correctly', () => {
const rows = [
{
id: 0,
brand: 'Nike',
},
{
id: 1,
brand: 'Addidas',
},
{
id: 2,
brand: 'Puma',
},
];
const GridTest = () => {
const apiRef = useApiRef();
React.useEffect(() => {
apiRef.current.setPage(2);
});
return (
<div style={{ width: 300, height: 300 }}>
<XGrid
rows={rows}
apiRef={apiRef}
columns={defaultProps.columns}
pagination
pageSize={1}
/>
</div>
);
};
render(<GridTest />);
const cell = document.querySelector('[role="cell"][aria-colindex="0"]')!;
expect(cell).to.have.text('Addidas');
});
});

describe('sorting', () => {
Expand Down
52 changes: 52 additions & 0 deletions packages/storybook/src/stories/grid-pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,55 @@ export function ServerPaginationWithEventHandler() {
</div>
);
}
export function Page1Prop() {
const data = useData(2000, 200);

return (
<div className="grid-container">
<XGrid
rows={data.rows}
columns={data.columns}
pagination
pageSize={50}
onPageChange={(p) => action('pageChange')(p)}
/>
</div>
);
}
export function Page2Prop() {
const data = useData(2000, 200);

return (
<div className="grid-container">
<XGrid
rows={data.rows}
columns={data.columns}
pagination
pageSize={50}
page={2}
onPageChange={(p) => action('pageChange')(p)}
/>
</div>
);
}
export function Page2Api() {
const data = useData(2000, 200);
const apiRef = useApiRef();

React.useEffect(() => {
apiRef.current.setPage(2);
}, [apiRef]);

return (
<div className="grid-container">
<XGrid
apiRef={apiRef}
rows={data.rows}
columns={data.columns}
pagination
pageSize={50}
onPageChange={(p) => action('pageChange')(p)}
/>
</div>
);
}