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] Support different elements under ButtonGroup #28645

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented Sep 27, 2021

Closes #17226

Use Context API instead of React.cloneElement.

I have also considered https://reactjs.org/docs/context.html#caveats by using useMemo. But I have not profiled any performance metrics.

Also let me know if we want to test this in some other ways.

CodeSandbox with the fix.

@ZeeshanTamboli ZeeshanTamboli marked this pull request as ready for review September 27, 2021 13:21
@ZeeshanTamboli ZeeshanTamboli self-assigned this Sep 27, 2021
@ZeeshanTamboli ZeeshanTamboli marked this pull request as draft September 27, 2021 13:21
@ZeeshanTamboli ZeeshanTamboli added the component: ButtonGroup The React component. label Sep 28, 2021
@mui-pr-bot
Copy link

mui-pr-bot commented Sep 28, 2021

Details of bundle changes

@material-ui/lab: parsed: +0.08% , gzip: +0.10%

Generated by 🚫 dangerJS against 1c026b8

@ZeeshanTamboli ZeeshanTamboli added the new feature New feature or request label Sep 28, 2021
@ZeeshanTamboli ZeeshanTamboli marked this pull request as ready for review September 28, 2021 05:40
@PabloLION
Copy link
Contributor

Any news on this PR? I am using a customized <IconButton> as a child of <ButtonGroup>.
I can only convert them to <Button> for now.

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Oct 6, 2021

Any news on this PR? I am using a customized <IconButton> as a child of <ButtonGroup>. I can only convert them to <Button> for now.

It would help if someone gets time to review this PR.


@Pablion Until then you can use this PR's preview package and check if it works for you.

"@mui/material": "https://pkg.csb.dev/mui-org/material-ui/commit/34759013/@mui/material",

@PabloLION
Copy link
Contributor

@ZeeshanTamboli I just checked the new preview with my localhost, and it is improved but not what expacted. I'm trying to create to create a sandbox to explain but they dont support "@mui/material": "https://pkg.csb.dev/mui-org/material-ui/commit/34759013/@mui/material" nor the "@mui/material": "^5.0.0",. Any help?

@PabloLION
Copy link
Contributor

I made a explanatory demo with Code Sandbox.

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Oct 11, 2021

I made a explanatory demo with Code Sandbox.

@Pablion Thanks for providing the codesandbox. You need to pass color="primary" prop to the IconButtons as explained in the doc. By default, the color is default.

See this updated CodeSandbox.

For how it is working on MUI 5.0.3 (the left content), I think anyway the IconButton is not supported inside ButtonGroup and it was wrong. You can check the hovering styles as well in GoogleIcon, looks a bit off.

@PabloLION
Copy link
Contributor

Hi,@ZeeshanTamboli,
Sorry for the misguiding example. My intention was to make the source code less changed, and I knew about the color="primary". I just want to point out that

  • The border style has also changed.
  • Those components wrapped by <a> and <button> are still considered as different class (shown in first section of 2nd Updated CodeSandbox here).

And I think that should be part of "Support different elements under ButtonGroup"

@ZeeshanTamboli ZeeshanTamboli requested a review from a team October 15, 2021 04:51
@siriwatknp
Copy link
Member

Should it be a demo to showcase the children that are not Button?

@PabloLION
Copy link
Contributor

Is there something new to test with?

@mnajdova mnajdova dismissed their stale review October 28, 2021 09:31

Comments addressed

@ZeeshanTamboli
Copy link
Member Author

All right! There's just two more issues to solve:

  1. Argos shows that the appearance of buttons inside a group changed. Could you make sure there are no differences in existing components?
  2. The CI complains about framer changes not being checked in. Please run yarn workspace framer build locally and commit the changes.

See #28645 (comment)

...other
} = props;

const color = colorProp || colorContext || 'primary';
const disabled = disabledProp || disabledContext || false;
const disableElevation = disableElevationProp || disableElevationContext || false;
Copy link
Member

@oliviertassinari oliviertassinari Oct 28, 2021

Choose a reason for hiding this comment

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

Using || looks wrong here. If disableElevationContext is true and disableElevationProp false, disableElevation should be false, not true.

Copy link
Member

@oliviertassinari oliviertassinari Oct 28, 2021

Choose a reason for hiding this comment

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

Otherwise, we could explore the creation of a helper. I assume that it's always the same case when a parent component uses the context to change default values on its children.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using || looks wrong here. If disableElevationContext is true and disableElevationProp false, disableElevation should be false, not true.

Nice catch. That is interesting! Need to think how to handle these button boolean props if used with context.

Copy link
Member

@oliviertassinari oliviertassinari Oct 28, 2021

Choose a reason for hiding this comment

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

I assume this would work:

Suggested change
const disableElevation = disableElevationProp || disableElevationContext || false;
const disableElevation = disableElevationProp ?? disableElevationContext ?? false;

I saw it used by Michal const ButtonRoot: React.ElementType = component ?? components.Root ?? 'button';

Copy link
Contributor

Choose a reason for hiding this comment

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

@oliviertassinari I'm afraid that his is not supposed to be used here. If i recall it correctly, ?? checks nullity (undefined/null) instead of falsy/truthy.

Example here (i just wrote it)

So if a user has disableElevationProp={false} disableElevationContext={true}, this version with ?? will return a false instead of true. (see the exception_case in that example)

component ?? components.Root ?? 'button' was correctly used or equally components?.Root ?? 'button'.

Copy link
Member

Choose a reason for hiding this comment

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

we do not support it like this without Context

It's interesting no one has filed a bug with this yet. I'd say let's keep the current behavior and fix it for the next major (and in unstyled Button when ButtonGroup is introduced there).

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd say let's keep the current behavior and fix it for the next major (and in unstyled Button when ButtonGroup is introduced there).

Okay. With that I think there is nothing else pending in this PR. Resolving this conversation.

Copy link
Member

Choose a reason for hiding this comment

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

@ZeeshanTamboli could we add a todo to change this behavior in v6?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

Copy link
Member

Choose a reason for hiding this comment

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

I was just writing tests for this and noticed that we do not support it like this without Context.

@ZeeshanTamboli it looks like more bugs.

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.

See one last comment for adding TODO for v6. Apart from that, it looks great! :)

@ZeeshanTamboli ZeeshanTamboli merged commit a0f23c0 into mui:master Nov 2, 2021
@ZeeshanTamboli ZeeshanTamboli deleted the issue/17226-support-different-elements-to-ButtonGroup branch November 2, 2021 07:23
@PabloLION
Copy link
Contributor

Hi MUI team. does this close #28899 as well? And will it on next patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: ButtonGroup The React component. new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ButtonGroup] Different elements to <Button> as a child issue warnings
8 participants