Radio
Radios are used when only one choice may be selected in a series of options.
Native HTML radios are 100% accessible by default, so we used a very common CSS technique to style the radio.
Import#
import { Radio, RadioGroup } from "@chakra-ui/react"
Usage#
Editable Example
Radio with custom color#
You can override the colorScheme
of the Radio to any color key specified in
theme.colors
.
Editable Example
Radio sizes#
The checkbox comes with 3 sizes.
Editable Example
Disabled radios#
Editable Example
Horizontal alignment#
Editable Example
Invalid Radio#
Editable Example
Custom Radio Buttons#
In some cases, you might need to create components that work like radios but
don't look like radios. Chakra exports useRadio
, and useRadioGroup
hooks to
help with this scenario. Here's what you need to do:
- Create a component that consumes the
useRadio
hook. - Use the
useRadioGroup
hook to control a group of custom radios.
Editable Example
Note about name
prop#
We recommend passing the name
prop to the RadioGroup
component, instead of
passing it to each Radio
component. By default, the name
prop of the
RadioGroup takes precedence.
// Do this ✅<RadioGroup name="form-name"><Radio>Radio 1</Radio><Radio>Radio 2</Radio></RadioGroup>// Don't do this ❌<RadioGroup ><Radio name="form-name">Radio 1</Radio><Radio name="form-name">Radio 2</Radio></RadioGroup>
Props#
Name | Type | Description | Default |
---|---|---|---|
colorScheme | "blue" | "cyan" | "gray" | "green" | "orange" | "pink" | "purple" | "red" | "teal" | "yellow" | "whiteAlpha" | "blackAlpha" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram" | "blue" | |
defaultChecked | boolean | If `true`, the radio will be initially checked. | - |
defaultIsChecked | boolean | If `true`, the radio will be initially checked. @deprecated Please use `defaultChecked` which mirrors the default prop name for radio elements. | - |
id | string | id assigned to input | - |
isChecked | boolean | If `true`, the radio will be checked. You'll need to pass `onChange` to update its value (since it is now controlled) | - |
isDisabled | boolean | If `true`, the radio will be disabled | - |
isFocusable | boolean | If `true` and `isDisabled` is true, the radio will remain focusable but not interactive. | - |
isFullWidth | boolean | If `true`, the radio will occupy the full width of its parent container @deprecated This component defaults to 100% width, please use the props `maxWidth` or `width` to configure | - |
isInvalid | boolean | If `true`, the radio button will be invalid. This also sets `aria-invalid` to `true`. | - |
isReadOnly | boolean | If `true`, the radio will be read-only | - |
isRequired | boolean | If `true`, the radio button will be required. This also sets `aria-required` to `true`. | - |
name | string | The name of the input field in a radio (Useful for form submission). | - |
onChange | ((event: ChangeEvent<HTMLInputElement>) => void) | Function called when checked state of the `input` changes | - |
size | "sm" | "md" | "lg" | "md" | |
spacing | SystemProps["marginLeft"] | The spacing between the checkbox and its label text | 0.5rem |
value | string | number | The value to be used in the radio button. This is the value that will be returned on form submission. | - |
variant | string | Variants for Radio are not implemented in the default theme. You can extend the theme to implement them. | - |