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

[ButtonGroup] Fix rendering with conditional elements #38989

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented Sep 15, 2023

Fixes #38978

Before: https://stackblitz.com/edit/stackblitz-starters-nykpki
After: https://codesandbox.io/s/material-ui-cra-ts-forked-4xh5j7

The issue arises for conditional elements because React.Children.count(childrenParam) also counts empty nodes, causing problems with applying the wrong positioning classes and thus resulting in incorrect styling. To avoid this, we can use React.Children.toArray, which excludes empty nodes.

I've also created a utility method to get valid React elements which is directly copied from Chakra UI source code (let me know if this is fine) so that it can be reused in other MUI packages.

(Note: This bug is also present in Joy UI ButtonGroup component.)

@ZeeshanTamboli ZeeshanTamboli added component: button This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material component: ButtonGroup The React component. regression A bug, but worse labels Sep 15, 2023
@mui-bot
Copy link

mui-bot commented Sep 15, 2023

Netlify deploy preview

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

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against d06db05

@ZeeshanTamboli ZeeshanTamboli marked this pull request as ready for review September 15, 2023 07:35
DiegoAndai

This comment was marked as outdated.

@DiegoAndai DiegoAndai self-requested a review September 15, 2023 14:42
@DiegoAndai
Copy link
Member

I've also created a utility method to get valid React elements which is directly copied from Chakra UI source code (let me know if this is fine) so that it can be reused in other MUI packages.

Not sure about this one, @mnajdova what do you think?

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.

Let's figure out the copy from Chakra first.

@mnajdova
Copy link
Member

The code looks good, we don't need to include the source of where we copied it, it's not likely that we will need to backport changes in the future. The util itself looks good

@ZeeshanTamboli ZeeshanTamboli merged commit 58504fa into mui:master Oct 4, 2023
19 checks passed
@ZeeshanTamboli ZeeshanTamboli deleted the issue-38978-buttongroup-conditional-elements branch October 4, 2023 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: button This is the name of the generic UI component, not the React module! component: ButtonGroup The React component. package: material-ui Specific to @mui/material regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ButtonGroup][material-ui] Renders incorrectly with conditional elements in 5.14.9
4 participants