Skip to content

[ButtonGroup] Fix rendering with conditional elements#38989

Merged
ZeeshanTamboli merged 9 commits intomui:masterfrom
ZeeshanTamboli:issue-38978-buttongroup-conditional-elements
Oct 4, 2023
Merged

[ButtonGroup] Fix rendering with conditional elements#38989
ZeeshanTamboli merged 9 commits intomui:masterfrom
ZeeshanTamboli:issue-38978-buttongroup-conditional-elements

Conversation

@ZeeshanTamboli
Copy link
Copy Markdown
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 scope: button Changes related to the button. package: material-ui component: ButtonGroup The React component. type: regression A bug, but worse, it used to behave as expected. labels Sep 15, 2023
@mui-bot
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@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: ButtonGroup The React component. scope: button Changes related to the button. type: regression A bug, but worse, it used to behave as expected.

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