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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ButtonGroup] + [Badge]: Wrapping a Button in a badge doesn't work if it's inside a button group #17383

Closed
2 tasks done
buob opened this issue Sep 10, 2019 · 4 comments
Closed
2 tasks done
Labels
component: ButtonGroup The React component. new feature New feature or request

Comments

@buob
Copy link

buob commented Sep 10, 2019

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 馃槸

Because ButtonGroup passes the values assuming the direct children are Buttons, it's passing these props to the Badge Element, causing the breakage. The styles and props aren't applied to the descendant Button, and the following errors occur:

Warning: Failed prop type: Invalid prop `variant` of value `outlined` supplied to
   `ForwardRef(Badge)`, expected one of ["dot","standard"].`
`Warning: React does not recognize the fullWidth / disableRipple / disableFocusRipple
    prop on a DOM element.`

This is the same as this person was experiencing: #16156, but while that use case has a reasonable workaround, I don't know that this one does as much (and it seems like a very common use case)

Expected Behavior 馃

It would be great if there was either:

  1. A way to have non-Button children of ButtonGroup (I'd imagine this would be ideal, because it makes sense to wrap ToolTips and other things around buttons in groups?
  2. Have a way to have a Badge that doesn't involve wrapping it.

Steps to Reproduce 馃暪

https://codesandbox.io/embed/create-react-app-b6odz

<ButtonGroup>
  <Button>foo</Button>
  <Button>bar</Button>
  <Badge badgeContent={4} color="primary">
    <Button>lee</Button>
  </Badge>
</ButtonGroup>
@oliviertassinari
Copy link
Member

The closest to a working state I can get is:

import React from "react";
import Container from "@material-ui/core/Container";
import ButtonGroup from "@material-ui/core/ButtonGroup";
import Button from "@material-ui/core/Button";
import Badge from "@material-ui/core/Badge";
import Box from "@material-ui/core/Box";

const Adapt = ({ children, ...other }) => children(other);

export default function App() {
  return (
    <Container maxWidth="sm">
      <Box my={5}>
        <ButtonGroup>
          <Button>Foo</Button>
          <Button>bar</Button>
          <Adapt>
            {({ className, ...props }) => (
              <Badge badgeContent={4} color="primary" className={className}>
                <Button {...props}>lee</Button>
              </Badge>
            )}
          </Adapt>
        </ButtonGroup>
      </Box>

      <Box my={5}>
        <Badge badgeContent={5} color="primary">
          <Button variant="outlined">lee</Button>
        </Badge>
      </Box>
    </Container>
  );
}

I don't see any clear solution for supporting this case, any idea?

@oliviertassinari oliviertassinari added component: ButtonGroup The React component. new feature New feature or request labels Sep 10, 2019
@slikts
Copy link

slikts commented Oct 9, 2019

I've ran into this in the context of #11601.

@slikts
Copy link

slikts commented Oct 9, 2019

The workaround of forwarding the props when nesting buttons doesn't fully work because the .MuiButtonGroup-grouped:not(:first-child) and .MuiButtonGroup-grouped:not(:last-child) selectors aren't matched.

Edit: in case this helps someone, I've ended up opting for a workaround like this:

.button:not(:first-child) {
  :global(.MuiButtonBase-root) {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
  }
}
.button:not(:last-child) {
  :global(.MuiButtonBase-root) {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
  }
}

@Gtes
Copy link

Gtes commented Mar 3, 2023

Found the same problem recently.
My solution is to wrap the whole ToggleButtonGroup and override zIndex for it.

I hope it will help someone.

<Badge>
  <ToggleButtonGroup
    sx={{
      zIndex: 1,
    }}
  >
    <ToggleButton />
    <ToggleButton />
  </ToggleButtonGroup>
</Badge>

image

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

No branches or pull requests

4 participants