Skip to content

Commit

Permalink
alternative proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Aug 26, 2019
1 parent ae28a99 commit 8daea9c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 140 deletions.
6 changes: 3 additions & 3 deletions docs/pages/api/toggle-button-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Any other props supplied will be provided to the root element (native element).
| Rule name | Global class | Description |
|:-----|:-------------|:------------|
| <span class="prop-name">root</span> | <span class="prop-name">MuiToggleButtonGroup-root</span> | Styles applied to the root element.
| <span class="prop-name">groupedChild</span> | <span class="prop-name">MuiToggleButtonGroup-groupedChild</span> | Styles applied to the children.
| <span class="prop-name">sizeSmallChild</span> | <span class="prop-name">MuiToggleButtonGroup-sizeSmallChild</span> | Styles applied to the children if `size="small"`.
| <span class="prop-name">sizeLargeChild</span> | <span class="prop-name">MuiToggleButtonGroup-sizeLargeChild</span> | Styles applied to the children if `size="large"`.
| <span class="prop-name">grouped</span> | <span class="prop-name">MuiToggleButtonGroup-grouped</span> | Styles applied to the children.
| <span class="prop-name">groupedSizeSmall</span> | <span class="prop-name">MuiToggleButtonGroup-groupedSizeSmall</span> | Styles applied to the children if `size="small"`.
| <span class="prop-name">groupedSizeLarge</span> | <span class="prop-name">MuiToggleButtonGroup-groupedSizeLarge</span> | Styles applied to the children if `size="large"`.

You can override the style of the component thanks to one of these customization points:

Expand Down
22 changes: 22 additions & 0 deletions docs/src/pages/components/toggle-button/StandaloneToggleButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import CheckIcon from '@material-ui/icons/Check';
import Box from '@material-ui/core/Box';
import ToggleButton from '@material-ui/lab/ToggleButton';

export default function StandaloneToggleButton() {
const [selected, setSelected] = React.useState(false);

return (
<Box bgcolor="white" borderRadius={4}>
<ToggleButton
value="check"
selected={selected}
onChange={() => {
setSelected(!selected);
}}
>
<CheckIcon />
</ToggleButton>
</Box>
);
}
35 changes: 0 additions & 35 deletions docs/src/pages/components/toggle-button/ToggleButton.js

This file was deleted.

63 changes: 8 additions & 55 deletions docs/src/pages/components/toggle-button/ToggleButtonSizes.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import React from 'react';
import CheckIcon from '@material-ui/icons/Check';
import FormatAlignLeftIcon from '@material-ui/icons/FormatAlignLeft';
import FormatAlignCenterIcon from '@material-ui/icons/FormatAlignCenter';
import FormatAlignRightIcon from '@material-ui/icons/FormatAlignRight';
import FormatAlignJustifyIcon from '@material-ui/icons/FormatAlignJustify';
import Box from '@material-ui/core/Box';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';

export default function ToggleButtonSizes() {
const [selected, setSelected] = React.useState(false);
const [alignment, setAlignment] = React.useState('left');

const toggleSelected = () => {
setSelected(!selected);
};

const handleChangeAlignment = (event, newAlignment) => {
const handleChange = (event, newAlignment) => {
setAlignment(newAlignment);
};

const groupChildren = [
const children = [
<ToggleButton key={1} value="left">
<FormatAlignLeftIcon />
</ToggleButton>,
Expand All @@ -40,57 +32,18 @@ export default function ToggleButtonSizes() {
return (
<Grid container spacing={2} direction="column" alignItems="center">
<Grid item>
<Typography gutterBottom>Size a standalone toggle button:</Typography>
</Grid>
<Grid item>
<ToggleButton size="small" selected={selected} onChange={toggleSelected}>
<CheckIcon />
<Box ml={1}>Toggle</Box>
</ToggleButton>
</Grid>
<Grid item>
<ToggleButton size="medium" selected={selected} onChange={toggleSelected}>
<CheckIcon />
<Box ml={1}>Toggle</Box>
</ToggleButton>
</Grid>
<Grid item>
<ToggleButton size="large" selected={selected} onChange={toggleSelected}>
<CheckIcon />
<Box ml={1}>Toggle</Box>
</ToggleButton>
</Grid>
<Grid item>
<Typography gutterBottom>Size an entire group of toggle buttons:</Typography>
</Grid>
<Grid item>
<ToggleButtonGroup
size="small"
value={alignment}
exclusive
onChange={handleChangeAlignment}
>
{groupChildren}
<ToggleButtonGroup size="small" value={alignment} exclusive onChange={handleChange}>
{children}
</ToggleButtonGroup>
</Grid>
<Grid item>
<ToggleButtonGroup
size="medium"
value={alignment}
exclusive
onChange={handleChangeAlignment}
>
{groupChildren}
<ToggleButtonGroup size="medium" value={alignment} exclusive onChange={handleChange}>
{children}
</ToggleButtonGroup>
</Grid>
<Grid item>
<ToggleButtonGroup
size="large"
value={alignment}
exclusive
onChange={handleChangeAlignment}
>
{groupChildren}
<ToggleButtonGroup size="large" value={alignment} exclusive onChange={handleChange}>
{children}
</ToggleButtonGroup>
</Grid>
</Grid>
Expand Down
14 changes: 5 additions & 9 deletions docs/src/pages/components/toggle-button/toggle-button.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ components: ToggleButton, ToggleButtonGroup

# Toggle Buttons

<p class="description">Toggle buttons can be used standalone or as a group.</p>

## Standalone Toggle Button

To implement a standalone Toggle button:

{{"demo": "pages/components/toggle-button/ToggleButton.js"}}

## Toggle Button Group
<p class="description">Toggle buttons can be used to group related options.</p>

To emphasize groups of related [Toggle buttons](https://material.io/design/components/buttons.html#toggle-button),
a group should share a common container.
Expand All @@ -28,3 +20,7 @@ given its own `value` prop.
Fancy larger or smaller buttons? Use the `size` property.

{{"demo": "pages/components/toggle-button/ToggleButtonSizes.js"}}

## Standalone toggle button

{{"demo": "pages/components/toggle-button/StandaloneToggleButton.js"}}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export const styles = theme => ({
/* Styles applied to the root element. */
root: {
backgroundColor: theme.palette.background.paper,
borderRadius: 2,
borderRadius: theme.shape.borderRadius,
display: 'inline-flex',
},
/* Styles applied to the children. */
groupedChild: {
grouped: {
padding: '0px 11px 0px 12px',
'&:not(:first-child)': {
marginLeft: -1,
Expand All @@ -28,20 +28,20 @@ export const styles = theme => ({
},
},
/* Styles applied to the children if `size="small"`. */
sizeSmallChild: {
groupedSizeSmall: {
padding: '0px 7px 0px 8px',
},
/* Styles applied to the children if `size="large"`. */
sizeLargeChild: {
groupedSizeLarge: {
padding: '0px 15px 0px 16px',
},
});

const ToggleButtonGroup = React.forwardRef(function ToggleButton(props, ref) {
const {
children,
className,
classes,
className,
exclusive = false,
onChange,
size = 'medium',
Expand Down Expand Up @@ -96,9 +96,9 @@ const ToggleButtonGroup = React.forwardRef(function ToggleButton(props, ref) {

return React.cloneElement(child, {
className: clsx(
classes.groupedChild,
classes.grouped,
{
[classes[`size${capitalize(size)}Child`]]: size !== 'medium',
[classes[`groupedSize${capitalize(size)}`]]: size !== 'medium',
},
child.props.className,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,6 @@ describe('<ToggleButtonGroup />', () => {
assert.strictEqual(root.hasClass(classes.root), true);
});

it('should apply the groupedChild className to children', () => {
const wrapper = mount(
<ToggleButtonGroup>
<ToggleButton value="hello">hello</ToggleButton>
</ToggleButtonGroup>,
);
assert.strictEqual(findToggleButton(wrapper, 'hello').hasClass(classes.groupedChild), true);
});

it('should render small children', () => {
const wrapper = mount(
<ToggleButtonGroup size="small">
<ToggleButton value="hello">hello</ToggleButton>
</ToggleButtonGroup>,
);
const child = findToggleButton(wrapper, 'hello');
assert.strictEqual(child.hasClass(classes.sizeSmallChild), true);
assert.strictEqual(child.hasClass(classes.sizeLargeChild), false);
});

it('should render large children', () => {
const wrapper = mount(
<ToggleButtonGroup size="large">
<ToggleButton value="hello">hello</ToggleButton>
</ToggleButtonGroup>,
);
const child = findToggleButton(wrapper, 'hello');
assert.strictEqual(child.hasClass(classes.sizeSmallChild), false);
assert.strictEqual(child.hasClass(classes.sizeLargeChild), true);
});

describe('exclusive', () => {
it('should render a selected ToggleButton if value is selected', () => {
const wrapper = mount(
Expand Down

0 comments on commit 8daea9c

Please sign in to comment.