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] Merge page and pageSize props into paginationModel #7147

Merged
merged 31 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2a7fb2a
Implement paginationModel and onPaginationModelChange
MBilalShafi Dec 31, 2022
3a1e4f0
Refinement
MBilalShafi Dec 31, 2022
179c3e0
Minor fix
MBilalShafi Dec 31, 2022
ddae38b
Merge next into 'pagination-model'
MBilalShafi Dec 31, 2022
c6efaab
Resolve a few of Alex's comments
MBilalShafi Jan 4, 2023
e10e4b4
Fix incorrect comment
MBilalShafi Jan 4, 2023
e27e48b
Update docs a bit
MBilalShafi Jan 4, 2023
fb10647
Fix a few tests
MBilalShafi Jan 4, 2023
3460160
Fix a lot of tests
MBilalShafi Jan 5, 2023
49dc073
'rowsPerPageOptions' to 'pageSizeOptions'
MBilalShafi Jan 6, 2023
758205c
Remove unused reasons
MBilalShafi Jan 6, 2023
9db54aa
Variable rename
MBilalShafi Jan 6, 2023
2c2e5c7
Fix failing test
MBilalShafi Jan 6, 2023
3d396bb
Remove 'rowCount' and 'pageCount' from state
MBilalShafi Jan 6, 2023
ef02fcb
proptypes
MBilalShafi Jan 6, 2023
59ac217
Merge 'next' into 'pagination-model'
MBilalShafi Jan 6, 2023
b685d9a
Simplifies a demo
MBilalShafi Jan 6, 2023
6fb6718
Update memo dep
MBilalShafi Jan 7, 2023
255a0bc
Make state single level
MBilalShafi Jan 11, 2023
d8f159b
docs:api
MBilalShafi Jan 11, 2023
d10551c
Merge 'next' into 'pagination-model'
MBilalShafi Jan 12, 2023
c15fa56
Revert 255a0bc
MBilalShafi Jan 16, 2023
369f647
docs:api
MBilalShafi Jan 16, 2023
e6c1781
Update migration guide
MBilalShafi Jan 16, 2023
6a2287a
Merge 'next' into pagination-model
MBilalShafi Jan 16, 2023
ed8158b
Update dep for cursor demo
MBilalShafi Jan 16, 2023
370f014
Adress comments
MBilalShafi Jan 17, 2023
36532a9
Refactor selectors
MBilalShafi Jan 17, 2023
cc982d4
Some refactoring as per PR comments
MBilalShafi Jan 17, 2023
09978f9
Fix argos
MBilalShafi Jan 17, 2023
a6a7d5a
Update a demo
MBilalShafi Jan 17, 2023
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
8 changes: 7 additions & 1 deletion docs/data/data-grid/api-object/UseGridApiContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ export default function UseGridApiContext() {
maxColumns: 6,
});

const [paginationModel, setPaginationModel] = React.useState({
pageSize: 10,
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved
page: 0,
});

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
pageSize={10}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
/>
</Box>
);
Expand Down
8 changes: 7 additions & 1 deletion docs/data/data-grid/api-object/UseGridApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ export default function UseGridApiContext() {
maxColumns: 6,
});

const [paginationModel, setPaginationModel] = React.useState({
pageSize: 10,
page: 0,
});

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
components={{
Toolbar: CustomToolbar,
}}
pageSize={10}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
/>
</Box>
);
Expand Down
3 changes: 2 additions & 1 deletion docs/data/data-grid/api-object/UseGridApiContext.tsx.preview
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
components={{
Toolbar: CustomToolbar,
}}
pageSize={10}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
/>
13 changes: 12 additions & 1 deletion docs/data/data-grid/api-object/UseGridApiRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export default function UseGridApiRef() {
maxColumns: 6,
});

const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: 10,
});

const apiRef = useGridApiRef();

const handleGoToPage1 = () => apiRef.current.setPage(1);
Expand All @@ -20,7 +25,13 @@ export default function UseGridApiRef() {
<Stack spacing={1} sx={{ width: '100%' }} alignItems="flex-start">
<Button onClick={handleGoToPage1}>Go to page 1</Button>
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid {...data} apiRef={apiRef} pagination pageSize={10} />
<DataGrid
{...data}
apiRef={apiRef}
pagination
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
/>
</Box>
</Stack>
);
Expand Down
13 changes: 12 additions & 1 deletion docs/data/data-grid/api-object/UseGridApiRef.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export default function UseGridApiRef() {
maxColumns: 6,
});

const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: 10,
});

const apiRef = useGridApiRef();

const handleGoToPage1 = () => apiRef.current.setPage(1);
Expand All @@ -20,7 +25,13 @@ export default function UseGridApiRef() {
<Stack spacing={1} sx={{ width: '100%' }} alignItems="flex-start">
<Button onClick={handleGoToPage1}>Go to page 1</Button>
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid {...data} apiRef={apiRef} pagination pageSize={10} />
<DataGrid
{...data}
apiRef={apiRef}
pagination
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scop: pagination is forced to true in the MIT version. I assume it's a legacy from a demo with pro component

Suggested change
pagination

paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
/>
</Box>
</Stack>
);
Expand Down
8 changes: 7 additions & 1 deletion docs/data/data-grid/api-object/UseGridApiRef.tsx.preview
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<Button onClick={handleGoToPage1}>Go to page 1</Button>
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid {...data} apiRef={apiRef} pagination pageSize={10} />
<DataGrid
{...data}
apiRef={apiRef}
pagination
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
/>
</Box>
5 changes: 4 additions & 1 deletion docs/data/data-grid/components/CustomPaginationGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export default function CustomPaginationGrid() {
Pagination: CustomPagination,
}}
{...data}
initialState={{ ...data.initialState, pagination: { pageSize: 25 } }}
initialState={{
...data.initialState,
pagination: { paginationModel: { pageSize: 25 } },
}}
/>
</Box>
);
Expand Down
5 changes: 4 additions & 1 deletion docs/data/data-grid/components/CustomPaginationGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export default function CustomPaginationGrid() {
Pagination: CustomPagination,
}}
{...data}
initialState={{ ...data.initialState, pagination: { pageSize: 25 } }}
initialState={{
...data.initialState,
pagination: { paginationModel: { pageSize: 25 } },
}}
/>
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
Pagination: CustomPagination,
}}
{...data}
initialState={{ ...data.initialState, pagination: { pageSize: 25 } }}
initialState={{
...data.initialState,
pagination: { paginationModel: { pageSize: 25 } },
}}
/>
4 changes: 2 additions & 2 deletions docs/data/data-grid/components/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ They can be used as below:
```tsx
function CustomPagination() {
const apiRef = useGridApiContext();
const page = useGridSelector(apiRef, gridPageSelector);
const paginationModel = useGridSelector(apiRef, gridPaginationModelSelector);
const pageCount = useGridSelector(apiRef, gridPageCountSelector);

return (
<Pagination
count={pageCount}
page={page + 1}
page={paginationModel.page + 1}
onChange={(event, value) => apiRef.current.setPage(value - 1)}
/>
);
Expand Down
13 changes: 3 additions & 10 deletions docs/data/data-grid/events/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,9 @@
},
{
"projects": ["x-data-grid", "x-data-grid-pro", "x-data-grid-premium"],
"name": "pageChange",
"description": "Fired when the page changes.",
"params": "number",
"event": "MuiEvent<{}>"
},
{
"projects": ["x-data-grid", "x-data-grid-pro", "x-data-grid-premium"],
"name": "pageSizeChange",
"description": "Fired when the page size changes.",
"params": "number",
"name": "paginationModelChange",
"description": "Fired when the pagination model changes.",
"params": "GridPaginationModel",
"event": "MuiEvent<{}>"
},
{
Expand Down
10 changes: 8 additions & 2 deletions docs/data/data-grid/export/CsvGetRowsToExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,20 @@ export default function CsvGetRowsToExport() {
maxColumns: 6,
});

const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: 10,
});

return (
<div style={{ height: 300, width: '100%' }}>
<DataGrid
{...data}
loading={loading}
components={{ Toolbar: CustomToolbar }}
pageSize={10}
rowsPerPageOptions={[10]}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
pageSizeOptions={[10]}
initialState={{
...data.initialState,
filter: {
Expand Down
10 changes: 8 additions & 2 deletions docs/data/data-grid/export/CsvGetRowsToExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,20 @@ export default function CsvGetRowsToExport() {
maxColumns: 6,
});

const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: 10,
});

return (
<div style={{ height: 300, width: '100%' }}>
<DataGrid
{...data}
loading={loading}
components={{ Toolbar: CustomToolbar }}
pageSize={10}
rowsPerPageOptions={[10]}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
pageSizeOptions={[10]}
initialState={{
...data.initialState,
filter: {
Expand Down
16 changes: 0 additions & 16 deletions docs/data/data-grid/export/CsvGetRowsToExport.tsx.preview

This file was deleted.

10 changes: 8 additions & 2 deletions docs/data/data-grid/overview/DataGridDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ const rows = [
];

export default function DataGridDemo() {
const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: 5,
});

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
pageSizeOptions={[5]}
checkboxSelection
disableRowSelectionOnClick
/>
Expand Down
10 changes: 8 additions & 2 deletions docs/data/data-grid/overview/DataGridDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ const rows = [
];

export default function DataGridDemo() {
const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: 5,
});

return (
<Box sx={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
pageSizeOptions={[5]}
checkboxSelection
disableRowSelectionOnClick
/>
Expand Down
5 changes: 3 additions & 2 deletions docs/data/data-grid/overview/DataGridDemo.tsx.preview
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
rowsPerPageOptions={[5]}
paginationModel={paginationModel}
onPaginationModelChange={setPaginationModel}
pageSizeOptions={[5]}
checkboxSelection
disableRowSelectionOnClick
/>
32 changes: 18 additions & 14 deletions docs/data/data-grid/pagination/CursorPaginationGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,37 @@ const { useQuery, ...data } = createFakeServer({}, SERVER_OPTIONS);
export default function CursorPaginationGrid() {
const mapPageToNextCursor = React.useRef({});

const [page, setPage] = React.useState(0);
const [paginationModel, setPaginationModel] = React.useState({
page: 0,
pageSize: PAGE_SIZE,
});

const queryOptions = React.useMemo(
() => ({
cursor: mapPageToNextCursor.current[page - 1],
pageSize: PAGE_SIZE,
cursor: mapPageToNextCursor.current[paginationModel.page - 1],
pageSize: paginationModel.pageSize,
}),
[page],
[paginationModel],
);

const { isLoading, rows, pageInfo } = useQuery(queryOptions);

const handlePageChange = (newPage) => {
const handlePaginationModelChange = (newPaginationModel) => {
// We have the cursor, we can allow the page transition.
if (newPage === 0 || mapPageToNextCursor.current[newPage - 1]) {
setPage(newPage);
if (
newPaginationModel.page === 0 ||
mapPageToNextCursor.current[newPaginationModel.page - 1]
) {
setPaginationModel(newPaginationModel);
}
};

React.useEffect(() => {
if (!isLoading && pageInfo?.nextCursor) {
// We add nextCursor when available
mapPageToNextCursor.current[page] = pageInfo?.nextCursor;
mapPageToNextCursor.current[paginationModel.page] = pageInfo?.nextCursor;
}
}, [page, isLoading, pageInfo?.nextCursor]);
}, [paginationModel.page, isLoading, pageInfo?.nextCursor]);

// Some API clients return undefined while loading
// Following lines are here to prevent `rowCountState` from being undefined during the loading
Expand All @@ -58,13 +64,11 @@ export default function CursorPaginationGrid() {
<DataGrid
rows={rows}
{...data}
pagination
pageSize={PAGE_SIZE}
rowsPerPageOptions={[PAGE_SIZE]}
pageSizeOptions={[PAGE_SIZE]}
rowCount={rowCountState}
paginationMode="server"
onPageChange={handlePageChange}
page={page}
onPaginationModelChange={handlePaginationModelChange}
paginationModel={paginationModel}
loading={isLoading}
/>
</div>
Expand Down
Loading