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

[core] Fix storybook pagination stories #1099

Merged
merged 3 commits into from
Feb 23, 2021
Merged
Changes from 1 commit
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
48 changes: 34 additions & 14 deletions packages/storybook/src/stories/grid-pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function PaginationApiTests() {
}, [apiRef, data]);

const backToFirstPage = () => {
apiRef.current.setPage(1);
apiRef.current.setPage(0);
};
const [myPageSize, setPageSize] = React.useState(33);
const changePageSizeWithOptionProp = () => {
Expand Down Expand Up @@ -186,7 +186,7 @@ function loadServerRows(params: GridPageChangeParams): Promise<GridData> {
const data = getData(params.pageSize * 5, 10);

setTimeout(() => {
const minId = (params.page - 1) * params.pageSize;
const minId = params.page * params.pageSize;
data.rows.forEach((row) => {
row.id = (Number(row.id) + minId).toString();
});
Expand All @@ -200,22 +200,29 @@ export function ServerPaginationWithApi() {
const data = useData(1000, 10);
const [rows, setRows] = React.useState<GridRowsProp>([]);
const [loading, setLoading] = React.useState<boolean>(false);
const defaultPageSize = 50;

const loadRows = React.useCallback((params: GridPageChangeParams) => {
setLoading(true);
loadServerRows(params).then((newData) => {
setRows(newData.rows);
setLoading(false);
});
}, []);

React.useEffect(() => {
const unsubscribe = apiRef.current.onPageChange((params) => {
action('onPageChange')(params);
setLoading(true);
loadServerRows(params).then((newData) => {
setRows(newData.rows);
setLoading(false);
});
loadRows(params);
});
loadRows({ page: 0, pageSize: defaultPageSize });

return () => {
if (unsubscribe) {
unsubscribe();
}
};
}, [apiRef, data]);
}, [apiRef, data, loadRows]);

return (
<div className="grid-container">
Expand All @@ -224,7 +231,7 @@ export function ServerPaginationWithApi() {
columns={data.columns}
apiRef={apiRef}
pagination
pageSize={50}
pageSize={defaultPageSize}
rowCount={1000}
paginationMode={'server'}
loading={loading}
Expand All @@ -238,24 +245,36 @@ export function ServerPaginationWithEventHandler() {
const data = useData(100, 10);
const [rows, setRows] = React.useState<GridRowsProp>([]);
const [loading, setLoading] = React.useState<boolean>(false);
const defaultPageSize = 50;

const onPageChange = React.useCallback((params) => {
action('onPageChange')(params);
const loadRows = React.useCallback((params: GridPageChangeParams) => {
setLoading(true);
loadServerRows(params).then((newData) => {
setRows(newData.rows);
setLoading(false);
});
}, []);

const onPageChange = React.useCallback(
(params) => {
action('onPageChange')(params);
loadRows(params);
},
[loadRows],
);

React.useEffect(() => {
loadRows({ page: 0, pageSize: defaultPageSize });
}, [apiRef, data, loadRows]);

return (
<div className="grid-container">
<XGrid
rows={rows}
columns={data.columns}
apiRef={apiRef}
pagination
pageSize={50}
pageSize={defaultPageSize}
rowCount={552}
paginationMode={'server'}
onPageChange={onPageChange}
Expand All @@ -274,6 +293,7 @@ export function Page1Prop() {
columns={data.columns}
pagination
pageSize={50}
page={0}
onPageChange={(p) => action('pageChange')(p)}
/>
</div>
Expand All @@ -289,7 +309,7 @@ export function Page2Prop() {
columns={data.columns}
pagination
pageSize={50}
page={2}
page={1}
onPageChange={(p) => action('pageChange')(p)}
/>
</div>
Expand All @@ -300,7 +320,7 @@ export function Page2Api() {
const apiRef = useGridApiRef();

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

return (
Expand Down