forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UseRadioGroup.tsx
40 lines (33 loc) · 1.19 KB
/
UseRadioGroup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as React from 'react';
import { experimentalStyled as styled } 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';
interface StyledFormControlLabelProps extends FormControlLabelProps {
checked: boolean;
}
const StyledFormControlLabel = styled((props: StyledFormControlLabelProps) => (
<FormControlLabel {...props} />
))(({ theme, checked }) => ({
'.MuiFormControlLabel-label': checked && {
color: theme.palette.secondary.main,
},
}));
function MyFormControlLabel(props: FormControlLabelProps) {
const radioGroup = useRadioGroup();
let checked = false;
if (radioGroup) {
checked = radioGroup.value === props.value;
}
return <StyledFormControlLabel checked={checked} {...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>
);
}