Skip to content

Commit

Permalink
[docs] Add useRadioGroup section (#21910)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Tassinari <[email protected]>
  • Loading branch information
kodai3 and oliviertassinari authored Jul 27, 2020
1 parent f716030 commit 88fa20f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/src/pages/components/radio-buttons/UseRadioGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/core/styles';
import RadioGroup, { useRadioGroup } from '@material-ui/core/RadioGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Radio from '@material-ui/core/Radio';

const useStyles = makeStyles((theme) => ({
labelChecked: {
color: theme.palette.secondary.main,
},
}));

function MyFormControlLabel(props) {
const classes = useStyles();
const radioGroup = useRadioGroup();

let checked = false;

if (radioGroup) {
checked = radioGroup.value === props.value;
}

return (
<FormControlLabel
classes={{
label: checked ? classes.labelChecked : '',
}}
{...props}
/>
);
}

MyFormControlLabel.propTypes = {
/**
* The value of the component.
*/
value: PropTypes.any,
};

export default function UseRadioGroup() {
return (
<RadioGroup name="use-radio-group" defaultValue="first">
<MyFormControlLabel value="first" label="First" control={<Radio />} />
<MyFormControlLabel value="second" label="Second" control={<Radio />} />
</RadioGroup>
);
}
44 changes: 44 additions & 0 deletions docs/src/pages/components/radio-buttons/UseRadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { makeStyles, createStyles } from '@material-ui/core/styles';
import RadioGroup, { useRadioGroup } from '@material-ui/core/RadioGroup';
import FormControlLabel, {
FormControlLabelProps,
} from '@material-ui/core/FormControlLabel';
import Radio from '@material-ui/core/Radio';

const useStyles = makeStyles((theme) =>
createStyles({
labelChecked: {
color: theme.palette.secondary.main,
},
}),
);

function MyFormControlLabel(props: FormControlLabelProps) {
const classes = useStyles();
const radioGroup = useRadioGroup();

let checked = false;

if (radioGroup) {
checked = radioGroup.value === props.value;
}

return (
<FormControlLabel
classes={{
label: checked ? classes.labelChecked : '',
}}
{...props}
/>
);
}

export default function UseRadioGroup() {
return (
<RadioGroup name="use-radio-group" defaultValue="first">
<MyFormControlLabel value="first" label="First" control={<Radio />} />
<MyFormControlLabel value="second" label="Second" control={<Radio />} />
</RadioGroup>
);
}
24 changes: 24 additions & 0 deletions docs/src/pages/components/radio-buttons/radio-buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ Here is an example of customizing the component. You can learn more about this i

{{"demo": "pages/components/radio-buttons/CustomizedRadios.js"}}

## `useRadioGroup`

For advanced customization use cases, a `useRadioGroup()` hook is exposed.
It returns the context value of the parent radio group.
The Radio component uses this hook internally.

### API

```jsx
import { useRadioGroup } from '@material-ui/core/RadioGroup';
```

#### Returns

`value` (_Object_):

- `value.name` (_String_ [optional]): The name used to reference the value of the control.
- `value.onChange` (_Void_ [optional]): Callback fired when a radio button is selected.
- `value.value` (_Any_ [optional]): Value of the selected radio button.

#### Example

{{"demo": "pages/components/radio-buttons/UseRadioGroup.js"}}

## When to use

- [Checkboxes vs. Radio Buttons](https://www.nngroup.com/articles/checkboxes-vs-radio-buttons/)
Expand Down

0 comments on commit 88fa20f

Please sign in to comment.