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][ToggleButtonGroup] Support different elements under it #40220

Merged
merged 19 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3e27ea1
[ToggleButtonGroup] Support different elements under ToggleButtonGroup
Methuselah96 Dec 17, 2023
75b8387
Fix styling for varying children
Methuselah96 Dec 28, 2023
18f06da
Add tests
Methuselah96 Dec 28, 2023
4a7617d
Update docs example
Methuselah96 Dec 28, 2023
bc6deec
Update transpiled docs example
Methuselah96 Dec 28, 2023
faf3243
Override border when button is disabled
Methuselah96 Jan 5, 2024
abf0c38
Update CustomizedDividers
Methuselah96 Jan 5, 2024
a0b8171
Merge branch 'master' into toggle-button-group-context
ZeeshanTamboli Jan 5, 2024
6b8356c
Update packages/mui-material/src/ToggleButtonGroup/ToggleButtonGroupC…
Methuselah96 Jan 5, 2024
50318f8
Update docs/data/material/components/toggle-button/CustomizedDividers…
Methuselah96 Jan 5, 2024
5ddd8c5
Update packages/mui-material/src/ToggleButtonGroup/ToggleButtonGroupC…
Methuselah96 Jan 5, 2024
94b6178
Update packages/mui-material/src/ToggleButtonGroup/ToggleButtonGroupC…
Methuselah96 Jan 5, 2024
f5ae99f
Update packages/mui-material/src/ToggleButtonGroup/toggleButtonGroupC…
Methuselah96 Jan 5, 2024
dcb82cd
Address PR review comments
Methuselah96 Jan 5, 2024
768dc26
Add back missing import
Methuselah96 Jan 5, 2024
5422705
Update toggle-button-group.json
Methuselah96 Jan 5, 2024
e7f4399
Update other toggle-button-group.json
Methuselah96 Jan 5, 2024
e80dd97
use toggleButtonGroupClasses instead of hardcoding classNames
ZeeshanTamboli Jan 6, 2024
fe00b4f
Merge branch 'master' into toggle-button-group-context
ZeeshanTamboli Jan 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions docs/data/material/components/toggle-button/CustomizedDividers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
import Divider from '@mui/material/Divider';
import Paper from '@mui/material/Paper';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import ToggleButtonGroup, {
toggleButtonGroupClasses,
} from '@mui/material/ToggleButtonGroup';

const StyledToggleButtonGroup = styled(ToggleButtonGroup)(({ theme }) => ({
'& .MuiToggleButtonGroup-grouped': {
[`& .${toggleButtonGroupClasses.grouped}`]: {
margin: theme.spacing(0.5),
border: 0,
'&.Mui-disabled': {
borderRadius: theme.shape.borderRadius,
[`&.${toggleButtonGroupClasses.disabled}`]: {
border: 0,
},
'&:not(:first-of-type)': {
borderRadius: theme.shape.borderRadius,
},
'&:first-of-type': {
borderRadius: theme.shape.borderRadius,
},
},
[`& .${toggleButtonGroupClasses.middleButton},& .${toggleButtonGroupClasses.lastButton}`]:
{
marginLeft: -1,
borderLeft: '1px solid transparent',
},
}));

export default function CustomizedDividers() {
Expand Down
20 changes: 11 additions & 9 deletions docs/data/material/components/toggle-button/CustomizedDividers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
import Divider from '@mui/material/Divider';
import Paper from '@mui/material/Paper';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import ToggleButtonGroup, {
toggleButtonGroupClasses,
} from '@mui/material/ToggleButtonGroup';

const StyledToggleButtonGroup = styled(ToggleButtonGroup)(({ theme }) => ({
'& .MuiToggleButtonGroup-grouped': {
[`& .${toggleButtonGroupClasses.grouped}`]: {
margin: theme.spacing(0.5),
border: 0,
'&.Mui-disabled': {
borderRadius: theme.shape.borderRadius,
[`&.${toggleButtonGroupClasses.disabled}`]: {
border: 0,
},
'&:not(:first-of-type)': {
borderRadius: theme.shape.borderRadius,
},
'&:first-of-type': {
borderRadius: theme.shape.borderRadius,
},
},
[`& .${toggleButtonGroupClasses.middleButton},& .${toggleButtonGroupClasses.lastButton}`]:
{
marginLeft: -1,
borderLeft: '1px solid transparent',
},
}));

export default function CustomizedDividers() {
Expand Down
18 changes: 18 additions & 0 deletions docs/pages/material-ui/api/toggle-button-group.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
"description": "State class applied to the root element if `disabled={true}`.",
"isGlobal": true
},
{
"key": "firstButton",
"className": "MuiToggleButtonGroup-firstButton",
"description": "Styles applied to the first button in the toggle button group.",
"isGlobal": false
},
{
"key": "fullWidth",
"className": "MuiToggleButtonGroup-fullWidth",
Expand All @@ -75,6 +81,18 @@
"description": "Styles applied to the children if `orientation=\"vertical\"`.",
"isGlobal": false
},
{
"key": "lastButton",
"className": "MuiToggleButtonGroup-lastButton",
"description": "Styles applied to the last button in the toggle button group.",
"isGlobal": false
},
{
"key": "middleButton",
"className": "MuiToggleButtonGroup-middleButton",
"description": "Styles applied to buttons in the middle of the toggle button group.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiToggleButtonGroup-root",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"nodeName": "the root element",
"conditions": "<code>disabled={true}</code>"
},
"firstButton": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the first button in the toggle button group"
},
"fullWidth": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
Expand All @@ -53,6 +57,14 @@
"nodeName": "the children",
"conditions": "<code>orientation=\"vertical\"</code>"
},
"lastButton": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the last button in the toggle button group"
},
"middleButton": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "buttons in the middle of the toggle button group"
},
"root": { "description": "Styles applied to the root element." },
"vertical": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/ButtonGroup/ButtonGroupContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import type { ButtonGroupProps } from './ButtonGroup';

interface IButtonGroupContext {
interface ButtonGroupContextType {
className?: string;
color?: ButtonGroupProps['color'];
disabled?: boolean;
Expand All @@ -16,7 +16,7 @@ interface IButtonGroupContext {
/**
* @ignore - internal component.
*/
const ButtonGroupContext = React.createContext<IButtonGroupContext>({});
const ButtonGroupContext = React.createContext<ButtonGroupContextType>({});

if (process.env.NODE_ENV !== 'production') {
ButtonGroupContext.displayName = 'ButtonGroupContext';
Expand Down
19 changes: 17 additions & 2 deletions packages/mui-material/src/ToggleButton/ToggleButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { internal_resolveProps as resolveProps } from '@mui/utils';
import { unstable_composeClasses as composeClasses } from '@mui/base/composeClasses';
import { alpha } from '../styles';
import ButtonBase from '../ButtonBase';
import capitalize from '../utils/capitalize';
import useThemeProps from '../styles/useThemeProps';
import styled from '../styles/styled';
import toggleButtonClasses, { getToggleButtonUtilityClass } from './toggleButtonClasses';
import ToggleButtonGroupContext from '../ToggleButtonGroup/ToggleButtonGroupContext';
import ToggleButtonGroupButtonContext from '../ToggleButtonGroup/ToggleButtonGroupButtonContext';
import isValueSelected from '../ToggleButtonGroup/isValueSelected';

const useUtilityClasses = (ownerState) => {
const { classes, fullWidth, selected, disabled, size, color } = ownerState;
Expand Down Expand Up @@ -108,7 +112,16 @@ const ToggleButtonRoot = styled(ButtonBase, {
});

const ToggleButton = React.forwardRef(function ToggleButton(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiToggleButton' });
// props priority: `inProps` > `contextProps` > `themeDefaultProps`
const { value: contextValue, ...contextProps } = React.useContext(ToggleButtonGroupContext);
const toggleButtonGroupButtonContextPositionClassName = React.useContext(
ToggleButtonGroupButtonContext,
);
const resolvedProps = resolveProps(
{ ...contextProps, selected: isValueSelected(inProps.value, contextValue) },
inProps,
);
const props = useThemeProps({ props: resolvedProps, name: 'MuiToggleButton' });
const {
children,
className,
Expand Down Expand Up @@ -148,9 +161,11 @@ const ToggleButton = React.forwardRef(function ToggleButton(inProps, ref) {
}
};

const positionClassName = toggleButtonGroupButtonContextPositionClassName || '';

return (
<ToggleButtonRoot
className={clsx(classes.root, className)}
className={clsx(contextProps.className, classes.root, className, positionClassName)}
disabled={disabled}
focusRipple={!disableFocusRipple}
ref={ref}
Expand Down
Loading