forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add radio error demo (mui#19599)
- Loading branch information
1 parent
eaafc6d
commit d8bb42e
Showing
5 changed files
with
138 additions
and
30 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,62 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import Radio from '@material-ui/core/Radio'; | ||
import RadioGroup from '@material-ui/core/RadioGroup'; | ||
import FormControlLabel from '@material-ui/core/FormControlLabel'; | ||
import FormControl from '@material-ui/core/FormControl'; | ||
import FormHelperText from '@material-ui/core/FormHelperText'; | ||
import FormLabel from '@material-ui/core/FormLabel'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
formControl: { | ||
margin: theme.spacing(3), | ||
}, | ||
button: { | ||
margin: theme.spacing(1, 1, 0, 0), | ||
}, | ||
})); | ||
|
||
export default function ErrorRadios() { | ||
const classes = useStyles(); | ||
const [value, setValue] = React.useState(''); | ||
const [error, setError] = React.useState(false); | ||
const [helperText, setHelperText] = React.useState('Choose wisely'); | ||
|
||
const handleRadioChange = event => { | ||
setValue(event.target.value); | ||
setHelperText(' '); | ||
setError(false); | ||
}; | ||
|
||
const handleSubmit = event => { | ||
event.preventDefault(); | ||
|
||
if (value === 'best') { | ||
setHelperText('You got it!'); | ||
setError(false); | ||
} else if (value === 'worst') { | ||
setHelperText('Sorry, wrong answer!'); | ||
setError(true); | ||
} else { | ||
setHelperText('Please select an option.'); | ||
setError(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<FormControl component="fieldset" error={error} className={classes.formControl}> | ||
<FormLabel component="legend">Pop quiz: Material-UI is...</FormLabel> | ||
<RadioGroup aria-label="quiz" name="quiz" value={value} onChange={handleRadioChange}> | ||
<FormControlLabel value="best" control={<Radio />} label="The best!" /> | ||
<FormControlLabel value="worst" control={<Radio />} label="The worst." /> | ||
</RadioGroup> | ||
<FormHelperText>{helperText}</FormHelperText> | ||
<Button type="submit" variant="outlined" color="primary" className={classes.button}> | ||
Check Answer | ||
</Button> | ||
</FormControl> | ||
</form> | ||
); | ||
} |
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,62 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import Radio from '@material-ui/core/Radio'; | ||
import RadioGroup from '@material-ui/core/RadioGroup'; | ||
import FormControlLabel from '@material-ui/core/FormControlLabel'; | ||
import FormControl from '@material-ui/core/FormControl'; | ||
import FormHelperText from '@material-ui/core/FormHelperText'; | ||
import FormLabel from '@material-ui/core/FormLabel'; | ||
import Button from '@material-ui/core/Button'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
formControl: { | ||
margin: theme.spacing(3), | ||
}, | ||
button: { | ||
margin: theme.spacing(1, 1, 0, 0), | ||
}, | ||
})); | ||
|
||
export default function ErrorRadios() { | ||
const classes = useStyles(); | ||
const [value, setValue] = React.useState(''); | ||
const [error, setError] = React.useState(false); | ||
const [helperText, setHelperText] = React.useState('Choose wisely'); | ||
|
||
const handleRadioChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setValue((event.target as HTMLInputElement).value); | ||
setHelperText(' '); | ||
setError(false); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (value === 'best') { | ||
setHelperText('You got it!'); | ||
setError(false); | ||
} else if (value === 'worst') { | ||
setHelperText('Sorry, wrong answer!'); | ||
setError(true); | ||
} else { | ||
setHelperText('Please select an option.'); | ||
setError(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<FormControl component="fieldset" error={error} className={classes.formControl}> | ||
<FormLabel component="legend">Pop quiz: Material-UI is...</FormLabel> | ||
<RadioGroup aria-label="quiz" name="quiz" value={value} onChange={handleRadioChange}> | ||
<FormControlLabel value="best" control={<Radio />} label="The best!" /> | ||
<FormControlLabel value="worst" control={<Radio />} label="The worst." /> | ||
</RadioGroup> | ||
<FormHelperText>{helperText}</FormHelperText> | ||
<Button type="submit" variant="outlined" color="primary" className={classes.button}> | ||
Check Answer | ||
</Button> | ||
</FormControl> | ||
</form> | ||
); | ||
} |
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
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
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