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

[material-ui][TablePagination] Add ability to change icons in TablePaginationActions using slots and slotProps #33797

Merged
merged 26 commits into from
Dec 14, 2023

Conversation

pratikkarad
Copy link
Contributor

@pratikkarad pratikkarad commented Aug 4, 2022

I have tested this fix locally and also tested using a preview package on CodeSandbox.

Closes #33231

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@mui-bot
Copy link

mui-bot commented Aug 4, 2022

Netlify deploy preview

https://deploy-preview-33797--material-ui.netlify.app/

@material-ui/core: parsed: +0.10% , gzip: +0.07%

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against dd84443

@propG
Copy link

propG commented Aug 4, 2022 via email

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Please also add componentsProps for allowing people to specify additional props if needed on these slots.

@pratikkarad pratikkarad requested a review from mnajdova August 14, 2022 16:12
Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Should we also support callbacks? cc @michaldudak

@michaldudak
Copy link
Member

Should we also support callbacks? cc @michaldudak

It's not crucial here (there is no real ownerState in TablePaginationActions component), but I suppose at some point we'd like to have components/componentProps in every component in Material UI and have it consistent with Base/Joy. So it doesn't have to be in this PR, but we'll need it sometime in the future.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 25, 2022
@pratikkarad pratikkarad requested a review from mnajdova August 29, 2022 11:27
@siriwatknp
Copy link
Member

I agree that the icons should be customizable but I am not satisfied with the components prop. The components prop in this PR is inconsistent with others because it targets nested components. I put an on hold label until we have a conclusion on components override.

@siriwatknp siriwatknp added the on hold There is a blocker, we need to wait label Sep 16, 2022
@pratikkarad
Copy link
Contributor Author

Hi @siriwatknp, please update the PR with the approach once you have a conclusion on the components prop.

@siriwatknp
Copy link
Member

Hi @siriwatknp, please update the PR with the approach once you have a conclusion on the components prop.

Sure! so sorry for the delay.

@jake-collibra
Copy link
Contributor

This pattern is seen in DatePicker, and now Alert as well
https://mui.com/x/api/date-pickers/date-picker/
https://mui.com/material-ui/api/alert/

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Mar 10, 2023

Any updates on this? As far as I know, I think we finally decided to use slots and slotProps to override components now. @pratikkarad I am closing this PR. If you would like to update it, please re-open it and apply the changes.

@pratikkarad
Copy link
Contributor Author

@ZeeshanTamboli I'll fix this.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 10, 2023
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 7, 2023
@ZeeshanTamboli ZeeshanTamboli changed the title [TablePagination] Add ability to change icons used in TablePaginationActions [material-ui][TablePagination] Add ability to change icons used in TablePaginationActions Dec 8, 2023
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@pratikkarad I made the expected changes. Thank you for the PR.

@DiegoAndai Could you please review this?

@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to @mui/material label Dec 8, 2023
@DiegoAndai
Copy link
Member

@ZeeshanTamboli API design might be confusing, as the slots and slotProps values differ (i.e., the firstButton slot props and firstPageIcon slot), what do you think about the following:

  • Have the slots and slotProps be firstButton, lastButton, nextButton, previousButton
  • For changing only the icon (#33231's case), use the children slot props: slotProps={{ firstButton: { children: <CustomIcon /> }}
  • For further customization, use the slot: slots={{ firstButton: ... }}

Would that work?

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Dec 8, 2023

@ZeeshanTamboli API design might be confusing, as the slots and slotProps values differ (i.e., the firstButton slot props and firstPageIcon slot), what do you think about the following:

  • Have the slots and slotProps be firstButton, lastButton, nextButton, previousButton
  • For changing only the icon (#33231's case), use the children slot props: slotProps={{ firstButton: { children: <CustomIcon /> }}
  • For further customization, use the slot: slots={{ firstButton: ... }}

Would that work?

@DiegoAndai, I considered using slots for buttons firstButton, lastButton, nextButton, and previousButton, similar to TablePaginationActions in MUI Base. However, I don't see the advantage of providing slots for buttons, as it's unlikely that someone would need a different element other than a button to be rendered there. If needed, users can use the component prop in slotProps like:

<TablePagination
  ....
  ....
  slotProps={{
    actions: {
      firstButton: {
        component: 'button', // HTML element or a React element
      },
    },
  }}
/>;

I found information about this here: #34334 (comment).

Yes, but the API design for properties differs between slots and slotProps, each having different properties for icons and buttons. However, is it necessary for slots and slotProps to be the same?

For changing only the icon (#33231 case), use the children slot props: slotProps={{ firstButton: { children: }}

(edited) If we could recommended this, we don't need this PR then unless you still suggest having slots for buttons. 👍
Oh, no, we'll still need to spread children.

@DiegoAndai
Copy link
Member

However, is it necessary for slots and slotProps to be the same?

I wouldn't say it's necessary, but IMO, it's a clearer API design. I wouldn't break it unless strictly necessary. Pure speculation here: If the slots from the slots prop and those from the slotProps prop differ, then the mental model of the component's internal structure becomes confusing.

Oh, no, we'll still need to spread children.

Yes, what do you think about doing that instead?

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Dec 9, 2023

Yes, what do you think about doing that instead?

@DiegoAndai To be honest, I don't like it. It's too nested: slotProps.actions.firstButton.children and I suppose we would need the children to be React.Node instead of React.Element.

Instead, I propose slots firstButton, lastButton, nextButton, previousButton, firstPageIcon, lastPageIcon, nextPageIcon and previousPageIcon for both slots and slotsProp props. What do you think?

@DiegoAndai
Copy link
Member

@ZeeshanTamboli I guess the "rule" we could follow is to have slots for components instantiated inside the parent. If that's the "rule", we could have slots for the icons as those are instantiated in the TablePaginationActions component. I would use "Button" instead of "Page", though: firstButtonIcon instead of firstPageIcon.

So I would be on board with: firstButton, lastButton, nextButton, previousButton, firstButtonIcon, lastButtonIcon, nextButtonIcon and previousButtonIcon for both slots and slotsProp props.

A question that arises now is how these should behave regarding right-to-left mode. Would the user expect, for example, for nextButtonIcon and previousButtonIcon to be switched on right-to-left? Or would they expect these to stay on the same position?


Sidenote:

To be honest, I don't like it. It's too nested: slotProps.actions.firstButton.children

I realized that we made a mistake in that PR. It should be slotProps.actions.slotProps.firstButton.

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Dec 13, 2023

@ZeeshanTamboli I guess the "rule" we could follow is to have slots for components instantiated inside the parent. If that's the "rule", we could have slots for the icons as those are instantiated in the TablePaginationActions component. I would use "Button" instead of "Page", though: firstButtonIcon instead of firstPageIcon.

So I would be on board with: firstButton, lastButton, nextButton, previousButton, firstButtonIcon, lastButtonIcon, nextButtonIcon and previousButtonIcon for both slots and slotsProp props.

Alright, I'll make the changes.

A question that arises now is how these should behave regarding right-to-left mode. Would the user expect, for example, for nextButtonIcon and previousButtonIcon to be switched on right-to-left? Or would they expect these to stay on the same position?

Certainly, it should switch successfully with custom slots since it already functions without them: https://codesandbox.io/p/sandbox/inspiring-hofstadter-8pzmn9?file=%2Fsrc%2Findex.tsx%3A22%2C4.

Sidenote:

To be honest, I don't like it. It's too nested: slotProps.actions.firstButton.children

I realized that we made a mistake in that PR. It should be slotProps.actions.slotProps.firstButton.

Hmm, but if this is done now it would be a breaking change.

@DiegoAndai
Copy link
Member

Hmm, but if this is done now it would be a breaking change.

Yes 😅 I was just noting it.

@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][TablePagination] Add ability to change icons used in TablePaginationActions [material-ui][TablePagination] Add ability to change icons used in TablePaginationActions using slots and slotProps Dec 13, 2023
@ZeeshanTamboli
Copy link
Member

@DiegoAndai This is ready for review. Also, here is the CodeSandbox demonstrating custom action slots with RTL mode.

Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

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

Glad to get this one done 😊 good work 🎉

@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][TablePagination] Add ability to change icons used in TablePaginationActions using slots and slotProps [material-ui][TablePagination] Add ability to change icons in TablePaginationActions using slots and slotProps Dec 14, 2023
@ZeeshanTamboli ZeeshanTamboli merged commit a5d6da7 into mui:master Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: TablePagination The React component. new feature New feature or request package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[TablePagination] Add ability to change icons used in TablePaginationActions
9 participants