-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
f42e1b5
to
2a7fb2a
Compare
These are the results for the performance tests:
|
<DataGrid | ||
{...data} | ||
apiRef={apiRef} | ||
pagination |
There was a problem hiding this comment.
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
pagination |
} | ||
}, [page, isLoading, pageInfo?.nextCursor]); | ||
}, [paginationModel, isLoading, pageInfo?.nextCursor]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure
}, [paginationModel, isLoading, pageInfo?.nextCursor]); | |
}, [paginationModel.page, isLoading, pageInfo?.nextCursor]); |
|
||
To initialize the page size without controlling it, provide the page size to the `initialState` prop. | ||
To initialize the pagination model without controlling it, provide the `paginationModel` to the `initialState` prop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To initialize the pagination model without controlling it, provide the `paginationModel` to the `initialState` prop. | |
To initialize the pagination model without controlling it, provide the `paginationModel` to the `initialState` prop. | |
It allows to specify the `pageSize` and the current `page`. |
const newPageSize = Number(event.target.value); | ||
apiRef.current.setPageSize(newPageSize); | ||
const pageSize = Number(event.target.value); | ||
apiRef.current.setPaginationModel({ pageSize, page: paginationState.paginationModel.page }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to be able to do the following: specifying only the elements you want to update
apiRef.current.setPaginationModel({ pageSize, page: paginationState.paginationModel.page }); | |
apiRef.current.setPaginationModel({ pageSize }); |
Same idea for handlePageChange
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've created setPage
and setPageSize
for those purpose ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I kept them for easing up the migration pain a bit. It's opinionated but I was thinking to deprecate them in v6 and remove them in v7 release basically to make the API simple, but they can be helpful too, like in this use-case. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion about keeping/removing those methods
packages/grid/x-data-grid/src/hooks/features/pagination/gridPaginationInterfaces.ts
Outdated
Show resolved
Hide resolved
const paginationModel = | ||
(props.paginationModel ?? props.initialState?.pagination?.paginationModel) || | ||
getDefaultGridPaginationModel(props.autoPageSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To allows partial specification in initialState
const paginationModel = | |
(props.paginationModel ?? props.initialState?.pagination?.paginationModel) || | |
getDefaultGridPaginationModel(props.autoPageSize); | |
const paginationModel = { | |
...getDefaultGridPaginationModel(props.autoPageSize), | |
...(props.paginationModel ?? props.initialState?.pagination?.paginationModel) | |
}; |
const setPaginationModel = React.useCallback<GridPaginationApi['setPaginationModel']>( | ||
(paginationModel) => { | ||
const currentModel = gridPaginationModelSelector(apiRef); | ||
if (paginationModel === currentModel) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're comparing object, I doubt the equality will be true
. Maybe better to test the values, but it woull expose to bug if we add other properties latter
if (paginationModel === currentModel) { | |
if (paginationModel.page === currentModel.page && paginationModel.pageSize === currentModel.pageSize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, I'm doing a simple equality check in the model updater and doing the smarter checks in each function.
See the filter plugin.
For the pagination, the state is simple enough so that the detailed check is doable.
But as you said, I think it's risky for the future.
I would prefer to keep exposing setPage
/ setPageSize
/ setPaginationModel
and have each method check what is relevant.
Updating only one of the two is probably the most common scenario for users, so it should not be more complicated to do it than before
packages/grid/x-data-grid/src/hooks/features/pagination/useGridPagination.ts
Outdated
Show resolved
Hide resolved
packages/grid/x-data-grid/src/hooks/features/pagination/gridPaginationInterfaces.ts
Show resolved
Hide resolved
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
@@ -317,6 +314,7 @@ export interface GridControlledStateReasonLookup { | |||
| 'deleteFilterItem' | |||
| 'changeLogicOperator' | |||
| 'restoreState'; | |||
pagination: 'setPaginationModel' | 'setPage' | 'setPageSize' | 'stateRestorePreProcessing'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only "setPaginationModel"
and "stateRestorePreProcessing"
are being effectively used. Although we have "setPageSize"
, setPageSize()
calls setPaginationModel()
that uses its own reason. I think if nobody asked for the reason we can leave it as undefined.
pagination: 'setPaginationModel' | 'setPage' | 'setPageSize' | 'stateRestorePreProcessing'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this key totally throws ts error while using apiRef.current.updateControlState
as it's typed to accept keys defined in GridControlledStateReasonLookup
.
How about keeping the valid ones?
pagination: 'setPaginationModel' | 'stateRestorePreProcessing';
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok to keep only the valid ones.
packages/grid/x-data-grid/src/hooks/features/pagination/useGridPagination.ts
Outdated
Show resolved
Hide resolved
packages/grid/x-data-grid/src/hooks/features/pagination/gridPaginationInterfaces.ts
Show resolved
Hide resolved
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
To initialize the page size without controlling it, provide the page size to the `initialState` prop. | ||
The pagination model is an object containing the current page and the size of the page. The default value is `{ page: 0, pageSize: 100 }`. To change the default value, make it controlled by `paginationModel` prop or initialize a custom value using `initialState.pagination.paginationModel`. | ||
|
||
### Initialize the pagination model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Initialize the pagination model | |
### Initializing the pagination model |
const [paginationModel, setPaginationModel] = React.useState({ | ||
pageSize: 25, | ||
page: 0, | ||
}); | ||
<DataGrid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<DataGrid | |
<DataGrid |
packages/grid/x-data-grid/src/hooks/features/pagination/gridPaginationSelector.ts
Outdated
Show resolved
Hide resolved
packages/grid/x-data-grid/src/hooks/features/pagination/useGridPagination.ts
Show resolved
Hide resolved
packages/grid/x-data-grid/src/hooks/features/pagination/useGridPagination.ts
Show resolved
Hide resolved
packages/grid/x-data-grid/src/tests/columnSpanning.DataGrid.test.tsx
Outdated
Show resolved
Hide resolved
packages/grid/x-data-grid/src/tests/pagination.DataGrid.test.tsx
Outdated
Show resolved
Hide resolved
Looks great! |
Closes #5624
Fixes #3516
Before: https://codesandbox.io/s/serverpaginationgrid-material-demo-forked-rts4d?file=/demo.tsx
After: https://codesandbox.io/s/serverpaginationgrid-material-demo-with-paginationmodel-0yhefr?file=/demo.tsx
Docs Preview: https://deploy-preview-7147--material-ui-x.netlify.app/x/react-data-grid/pagination/#pagination-model
Changelog
Breaking changes
The
page
andpageSize
props and their respective event handlersonPageChange
andonPageSizeChange
were removed. UsepaginationModel
andonPaginationModelChange
instead.The property
initialState.pagination.page
andinitialState.pagination.pageSize
were also removed. UseinitialState.pagination.paginationModel
instead.The
rowsPerPageOptions
prop was renamed topageSizeOptions
.