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

Conversation

MBilalShafi
Copy link
Member

@MBilalShafi MBilalShafi commented Dec 8, 2022

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 and pageSize props and their respective event handlers onPageChange and onPageSizeChange were removed. Use paginationModel and onPaginationModelChange instead.

    -<DataGrid page={page} pageSize={pageSize} onPageChange={handlePageChange} onPageSizeChange={handlePageSizeChange} />
    +<DataGrid paginationModel={{ page, pageSize }} onPaginationModelChange={handlePaginationModelChange} />
  • The property initialState.pagination.page and initialState.pagination.pageSize were also removed. Use initialState.pagination.paginationModel instead.

    -initialState={{ pagination: { page: 1, pageSize: 10 } }}
    +initialState={{ pagination: { paginationModel: { page: 1, pageSize: 10 } } }}
  • The rowsPerPageOptions prop was renamed to pageSizeOptions.

    -<DataGrid rowsPerPageOptions={[10, 20, 50]} />
    +<DataGrid pageSizeOptions={[10, 20, 50]} />

@MBilalShafi MBilalShafi added breaking change component: data grid This is the name of the generic UI component, not the React module! labels Dec 8, 2022
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 14, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@mui-bot
Copy link

mui-bot commented Dec 31, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-7147--material-ui-x.netlify.app/

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 624.8 1,192.4 631.9 841.42 211.091
Sort 100k rows ms 618.6 1,204.3 618.6 945.32 190.029
Select 100k rows ms 169.6 294.6 226 231.84 40.172
Deselect 100k rows ms 174.2 218 192.4 193.58 15.915

Generated by 🚫 dangerJS against a6a7d5a

@MBilalShafi MBilalShafi added the codemod candidate Suitable for writing a codemod for label Dec 31, 2022
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 31, 2022
@MBilalShafi MBilalShafi marked this pull request as ready for review December 31, 2022 09:34
<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

}
}, [page, isLoading, pageInfo?.nextCursor]);
}, [paginationModel, isLoading, pageInfo?.nextCursor]);
Copy link
Member

Choose a reason for hiding this comment

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

Not sure

Suggested change
}, [paginationModel, isLoading, pageInfo?.nextCursor]);
}, [paginationModel.page, isLoading, pageInfo?.nextCursor]);

docs/data/data-grid/pagination/PageSizeInitialState.tsx Outdated Show resolved Hide resolved

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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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`.

docs/data/data-grid/pagination/pagination.md Outdated Show resolved Hide resolved
const newPageSize = Number(event.target.value);
apiRef.current.setPageSize(newPageSize);
const pageSize = Number(event.target.value);
apiRef.current.setPaginationModel({ pageSize, page: paginationState.paginationModel.page });
Copy link
Member

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

Suggested change
apiRef.current.setPaginationModel({ pageSize, page: paginationState.paginationModel.page });
apiRef.current.setPaginationModel({ pageSize });

Same idea for handlePageChange

Copy link
Member

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 ?

Copy link
Member Author

@MBilalShafi MBilalShafi Jan 4, 2023

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. 🤔

Copy link
Member

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

Comment on lines 34 to 36
const paginationModel =
(props.paginationModel ?? props.initialState?.pagination?.paginationModel) ||
getDefaultGridPaginationModel(props.autoPageSize);
Copy link
Member

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

Suggested change
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) {
Copy link
Member

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

Suggested change
if (paginationModel === currentModel) {
if (paginationModel.page === currentModel.page && paginationModel.pageSize === currentModel.pageSize) {

Copy link
Member

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

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 5, 2023
@github-actions
Copy link

github-actions bot commented Jan 5, 2023

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';
Copy link
Member

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.

Suggested change
pagination: 'setPaginationModel' | 'setPage' | 'setPageSize' | 'stateRestorePreProcessing';

Copy link
Member Author

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';

Copy link
Member

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.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 11, 2023
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 12, 2023
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 16, 2023
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@MBilalShafi MBilalShafi self-assigned this Jan 16, 2023
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 16, 2023
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
Copy link
Member

@m4theushw m4theushw Jan 16, 2023

Choose a reason for hiding this comment

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

Suggested change
### Initialize the pagination model
### Initializing the pagination model

const [paginationModel, setPaginationModel] = React.useState({
pageSize: 25,
page: 0,
});
<DataGrid
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<DataGrid
<DataGrid

docs/data/data-grid/pagination/pagination.md Outdated Show resolved Hide resolved
@DanailH
Copy link
Member

DanailH commented Jan 17, 2023

Looks great!

@MBilalShafi MBilalShafi merged commit afb47b2 into mui:next Jan 17, 2023
@MBilalShafi MBilalShafi deleted the pagination-model branch January 17, 2023 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change codemod candidate Suitable for writing a codemod for component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
7 participants