-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add useRadioGroup section (#21910)
Co-authored-by: Olivier Tassinari <[email protected]>
- Loading branch information
1 parent
f716030
commit 88fa20f
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters