Radio
Botones de radio permiten al usuario seleccionar una opción de un conjunto.
Usa botones de radio cuando el usuario necesita ver todas las opciones disponibles. Si las opciones disponibles se pueden colapsar, considere usar un menú desplegable porque utiliza menos espacio.
Radio buttons should have the most commonly used option selected by default.
RadioGroup
RadioGroup
is a helpful wrapper used to group Radio
components that provides an easier API, and proper keyboard accessibility to the group.
<FormControl component="fieldset">
<FormLabel component="legend">Gender</FormLabel>
<RadioGroup aria-label="gender" name="gender1" value={value} onChange={handleChange}>
<FormControlLabel value="female" control={<Radio />} label="Female" />
<FormControlLabel value="male" control={<Radio />} label="Male" />
<FormControlLabel value="other" control={<Radio />} label="Other" />
<FormControlLabel value="disabled" disabled control={<Radio />} label="(Disabled option)" />
</RadioGroup>
</FormControl>
Ubicación de Etiqueta
You can change the placement of the label with the FormControlLabel
component's labelPlacement
prop:
Show error
In general, radio buttons should have a value selected by default. If this is not the case, you can display an error if no value is selected when the form is submitted:
<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>
Customized radios
Here is an example of customizing the component. You can learn more about this in the overrides documentation page.
<FormControl component="fieldset">
<FormLabel component="legend">Gender</FormLabel>
<RadioGroup defaultValue="female" aria-label="gender" name="customized-radios">
<FormControlLabel value="female" control={<StyledRadio />} label="Female" />
<FormControlLabel value="male" control={<StyledRadio />} label="Male" />
<FormControlLabel value="other" control={<StyledRadio />} label="Other" />
<FormControlLabel
value="disabled"
disabled
control={<StyledRadio />}
label="(Disabled option)"
/>
</RadioGroup>
</FormControl>
When to use
Accesibilidad
(WAI-ARIA: https://www.w3.org/TR/wai-aria-practices/#radiobutton)
- Todos los controles de formulario deben tener etiquetas, y esto incluye radio buttons, checkboxes, and switches. En la mayoría de los casos, esto se hace usando el elemento
<label>
(FormControlLabel). - Cuando no se puede usar una etiqueta, es necesario agregar un atributo directamente al componente de entrada. En este caso, puede aplicar el atributo adicional (por ejemplo,
aria-label
,aria-labelledby
,title
) a través de la propiedadinputProps
.
<RadioButton
value="radioA"
inputProps={{ 'aria-label': 'Radio A' }}
/>