Skip to content

Commit

Permalink
add demo & return interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kodai3 committed Jul 24, 2020
1 parent d843b22 commit 4ae55d8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 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 { 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';
import PropTypes from 'prop-types';

const useStyles = makeStyles((theme) => ({
labelChecked: {
color: theme.palette.secondary.main,
fontWeight: 'bold',
textDecoration: 'underline',
},
}));

const 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 = {
value: PropTypes.string.isRequired,
};

export default function UseRadioGroup() {
return (
<RadioGroup defaultValue="first">
<MyFormControlLabel value="first" label="First" control={<Radio />} />
<MyFormControlLabel value="second" label="Second" control={<Radio />} />
</RadioGroup>
);
}
46 changes: 46 additions & 0 deletions docs/src/pages/components/radio-buttons/UseRadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { makeStyles } 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) => ({
labelChecked: {
color: theme.palette.secondary.main,
fontWeight: 'bold',
textDecoration: 'underline',
},
}));

const 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 defaultValue="first">
<MyFormControlLabel value="first" label="First" control={<Radio />} />

<MyFormControlLabel value="second" label="Second" control={<Radio />} />
</RadioGroup>
);
}
12 changes: 9 additions & 3 deletions docs/src/pages/components/radio-buttons/radio-buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ For advanced customization use cases, we expose a `useRadioGroup()` hook.
It returns the context value of RadioGroupContext.
The Radio component uses this hook internally.

```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.

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

## When to use

Expand Down

0 comments on commit 4ae55d8

Please sign in to comment.