Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add CheckboxesGroup TypeScript demo (#15228)
- Loading branch information
1 parent
fde7c11
commit 130cc2e
Showing
2 changed files
with
89 additions
and
4 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
85 changes: 85 additions & 0 deletions
85
docs/src/pages/demos/selection-controls/CheckboxesGroup.tsx
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,85 @@ | ||
import React from 'react'; | ||
import { makeStyles, Theme } from '@material-ui/core/styles'; | ||
import FormLabel from '@material-ui/core/FormLabel'; | ||
import FormControl from '@material-ui/core/FormControl'; | ||
import FormGroup from '@material-ui/core/FormGroup'; | ||
import FormControlLabel from '@material-ui/core/FormControlLabel'; | ||
import FormHelperText from '@material-ui/core/FormHelperText'; | ||
import Checkbox from '@material-ui/core/Checkbox'; | ||
|
||
const useStyles = makeStyles((theme: Theme) => ({ | ||
root: { | ||
display: 'flex', | ||
}, | ||
formControl: { | ||
margin: theme.spacing(3), | ||
}, | ||
})); | ||
|
||
function CheckboxesGroup() { | ||
const classes = useStyles(); | ||
const [state, setState] = React.useState({ | ||
gilad: true, | ||
jason: false, | ||
antoine: false, | ||
}); | ||
|
||
const handleChange = (name: string) => (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setState({ ...state, [name]: event.target.checked }); | ||
}; | ||
|
||
const { gilad, jason, antoine } = state; | ||
const error = [gilad, jason, antoine].filter(v => v).length !== 2; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<FormControl component={'fieldset' as 'div'} className={classes.formControl}> | ||
<FormLabel component={'legend' as 'label'}>Assign responsibility</FormLabel> | ||
<FormGroup> | ||
<FormControlLabel | ||
control={<Checkbox checked={gilad} onChange={handleChange('gilad')} value="gilad" />} | ||
label="Gilad Gray" | ||
/> | ||
<FormControlLabel | ||
control={<Checkbox checked={jason} onChange={handleChange('jason')} value="jason" />} | ||
label="Jason Killian" | ||
/> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox checked={antoine} onChange={handleChange('antoine')} value="antoine" /> | ||
} | ||
label="Antoine Llorca" | ||
/> | ||
</FormGroup> | ||
<FormHelperText>Be careful</FormHelperText> | ||
</FormControl> | ||
<FormControl | ||
required | ||
error={error} | ||
component={'fieldset' as 'div'} | ||
className={classes.formControl} | ||
> | ||
<FormLabel component={'legend' as 'label'}>Pick two</FormLabel> | ||
<FormGroup> | ||
<FormControlLabel | ||
control={<Checkbox checked={gilad} onChange={handleChange('gilad')} value="gilad" />} | ||
label="Gilad Gray" | ||
/> | ||
<FormControlLabel | ||
control={<Checkbox checked={jason} onChange={handleChange('jason')} value="jason" />} | ||
label="Jason Killian" | ||
/> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox checked={antoine} onChange={handleChange('antoine')} value="antoine" /> | ||
} | ||
label="Antoine Llorca" | ||
/> | ||
</FormGroup> | ||
<FormHelperText>You can display an error</FormHelperText> | ||
</FormControl> | ||
</div> | ||
); | ||
} | ||
|
||
export default CheckboxesGroup; |