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] Custom variant #22160

Merged
merged 5 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion docs/pages/api-docs/button-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The `MuiButtonGroup` name can be used for providing [default props](/customizati
| <span class="prop-name">fullWidth</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the buttons will take up the full width of its container. |
| <span class="prop-name">orientation</span> | <span class="prop-type">'horizontal'<br>&#124;&nbsp;'vertical'</span> | <span class="prop-default">'horizontal'</span> | The group orientation (layout flow direction). |
| <span class="prop-name">size</span> | <span class="prop-type">'large'<br>&#124;&nbsp;'medium'<br>&#124;&nbsp;'small'</span> | <span class="prop-default">'medium'</span> | The size of the button. `small` is equivalent to the dense button styling. |
| <span class="prop-name">variant</span> | <span class="prop-type">'contained'<br>&#124;&nbsp;'outlined'<br>&#124;&nbsp;'text'</span> | <span class="prop-default">'outlined'</span> | The variant to use. |
| <span class="prop-name">variant</span> | <span class="prop-type">'contained'<br>&#124;&nbsp;'outlined'<br>&#124;&nbsp;'text'<br>&#124;&nbsp;string</span> | <span class="prop-default">'outlined'</span> | The variant to use. |

The `ref` is forwarded to the root element.

Expand All @@ -51,6 +51,8 @@ Any other props supplied will be provided to the root element (native element).
|:-----|:-------------|:------------|
| <span class="prop-name">root</span> | <span class="prop-name">.MuiButtonGroup-root</span> | Styles applied to the root element.
| <span class="prop-name">contained</span> | <span class="prop-name">.MuiButtonGroup-contained</span> | Styles applied to the root element if `variant="contained"`.
| <span class="prop-name">outlined</span> | <span class="prop-name">.MuiButtonGroup-outlined</span> | Styles applied to the root element if `variant="outlined"`.
| <span class="prop-name">text</span> | <span class="prop-name">.MuiButtonGroup-text</span> | Styles applied to the root element if `variant="text"`.
| <span class="prop-name">disableElevation</span> | <span class="prop-name">.MuiButtonGroup-disableElevation</span> | Styles applied to the root element if `disableElevation={true}`.
| <span class="prop-name">disabled</span> | <span class="prop-name">.Mui-disabled</span> | Pseudo-class applied to child elements if `disabled={true}`.
| <span class="prop-name">fullWidth</span> | <span class="prop-name">.MuiButtonGroup-fullWidth</span> | Styles applied to the root element if `fullWidth={true}`.
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui-styles/src/makeStyles/makeStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export default function makeStyles(stylesOrCreator, options = {}) {
'MuiAvatar',
'MuiBadge',
'MuiButton',
'MuiButtonGroup',
'MuiChip',
'MuiTypography',
];
Expand Down
8 changes: 7 additions & 1 deletion packages/material-ui/src/ButtonGroup/ButtonGroup.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react';
import { OverridableStringUnion } from '@material-ui/types';
import { OverridableComponent, OverrideProps } from '../OverridableComponent';

export interface ButtonGroupPropsVariantOverrides {}
export type ButtonGroupVariantDefaults = Record<'text' | 'outlined' | 'contained', true>;

export interface ButtonGroupTypeMap<P = {}, D extends React.ElementType = 'div'> {
props: P & {
/**
Expand Down Expand Up @@ -43,7 +47,7 @@ export interface ButtonGroupTypeMap<P = {}, D extends React.ElementType = 'div'>
/**
* The variant to use.
*/
variant?: 'text' | 'outlined' | 'contained';
variant?: OverridableStringUnion<ButtonGroupVariantDefaults, ButtonGroupPropsVariantOverrides>;
};
defaultComponent: D;
classKey: ButtonGroupClassKey;
Expand All @@ -64,6 +68,8 @@ declare const ButtonGroup: OverridableComponent<ButtonGroupTypeMap>;
export type ButtonGroupClassKey =
| 'root'
| 'contained'
| 'outlined'
| 'text'
| 'disabled'
| 'disableElevation'
| 'fullWidth'
Expand Down
28 changes: 27 additions & 1 deletion packages/material-ui/src/ButtonGroup/ButtonGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { isFragment } from 'react-is';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { useThemeVariants } from '@material-ui/styles';
import capitalize from '../utils/capitalize';
import { fade } from '../styles/colorManipulator';
import withStyles from '../styles/withStyles';
Expand All @@ -21,6 +22,10 @@ export const styles = (theme) => ({
contained: {
boxShadow: theme.shadows[2],
},
/* Styles applied to the root element if `variant="outlined"`. */
outlined: {},
/* Styles applied to the root element if `variant="text"`. */
text: {},
/* Styles applied to the root element if `disableElevation={true}`. */
disableElevation: {
boxShadow: 'none',
Expand Down Expand Up @@ -180,6 +185,23 @@ const ButtonGroup = React.forwardRef(function ButtonGroup(props, ref) {
...other
} = props;

const themeVariantsClasses = useThemeVariants(
{
...props,
color,
component: Component,
disabled,
disableElevation,
disableFocusRipple,
disableRipple,
fullWidth,
orientation,
size,
variant,
},
'MuiButtonGroup',
);

const buttonClassName = clsx(
classes.grouped,
classes[`grouped${capitalize(orientation)}`],
Expand All @@ -202,6 +224,7 @@ const ButtonGroup = React.forwardRef(function ButtonGroup(props, ref) {
[classes.fullWidth]: fullWidth,
[classes.disableElevation]: disableElevation,
},
themeVariantsClasses,
className,
)}
ref={ref}
Expand Down Expand Up @@ -298,7 +321,10 @@ ButtonGroup.propTypes = {
/**
* The variant to use.
*/
variant: PropTypes.oneOf(['contained', 'outlined', 'text']),
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['contained', 'outlined', 'text']),
PropTypes.string,
]),
};

export default withStyles(styles, { name: 'MuiButtonGroup' })(ButtonGroup);