Radio
Radio is a component used for allowing users to select one option from multiple choices.
<RadioGroup.Root>
<RadioGroup.Item value="1">織田信長</RadioGroup.Item>
<RadioGroup.Item value="2">豊臣秀吉</RadioGroup.Item>
<RadioGroup.Item value="3">徳川家康</RadioGroup.Item>
</RadioGroup.Root>
Usage
import { Radio, RadioGroup } from "@yamada-ui/react"
import { Radio, RadioGroup } from "@/components/ui"
import { Radio, RadioGroup } from "@workspaces/ui"
<RadioGroup.Root>
<RadioGroup.Item />
</RadioGroup.Root>
Use Items
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "織田信長", value: "1" },
{ label: "豊臣秀吉", value: "2" },
{ label: "徳川家康", value: "3" },
],
[],
)
return <RadioGroup.Root items={items} />
Change Variant
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "Checked", value: "1" },
{ label: "No checked", value: "2" },
],
[],
)
return (
<VStack>
<For each={["solid", "subtle", "surface", "outline"]}>
{(variant, index) => (
<RadioGroup.Root
key={variant}
variant={variant}
defaultValue="1"
items={items}
/>
)}
</For>
</VStack>
)
Change Size
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "Checked", value: "1" },
{ label: "No checked", value: "2" },
],
[],
)
return (
<VStack>
<For each={["sm", "md", "lg"]}>
{(size, index) => (
<RadioGroup.Root
key={size}
size={size}
defaultValue="1"
items={items}
/>
)}
</For>
</VStack>
)
Set Default Value
To set a default value, set defaultValue to a value.
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "織田信長", value: "1" },
{ label: "豊臣秀吉", value: "2" },
{ label: "徳川家康", value: "3" },
],
[],
)
return <RadioGroup.Root defaultValue="1" items={items} />
Default Checked
To default checked, set defaultChecked to true.
const items = useMemo<RadioGroup.Item[]>(
() => [
{ defaultChecked: true, label: "織田信長", value: "1" },
{ label: "豊臣秀吉", value: "2" },
{ label: "徳川家康", value: "3" },
],
[],
)
return <RadioGroup.Root items={items} />
Change Direction
To change the direction, set orientation to "horizontal" or "vertical". The default is "vertical".
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "織田信長", value: "1" },
{ label: "豊臣秀吉", value: "2" },
{ label: "徳川家康", value: "3" },
],
[],
)
return <RadioGroup.Root items={items} orientation="horizontal" />
Change Shape
To change the shape, set shape to "circle", "square" or "rounded". The default is "circle".
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "織田信長", value: "1" },
{ label: "豊臣秀吉", value: "2" },
{ label: "徳川家康", value: "3" },
],
[],
)
return (
<VStack>
<For each={["circle", "square", "rounded"]}>
{(shape) => (
<RadioGroup.Root
key={shape}
defaultValue="1"
shape={shape}
items={items}
/>
)}
</For>
</VStack>
)
Disable
To disable, set disabled to true.
<RadioGroup.Root>
<For each={["solid", "subtle", "surface", "outline"]}>
{(variant) => (
<RadioGroup.Item key={variant} variant={variant} disabled>
{toTitleCase(variant)}
</RadioGroup.Item>
)}
</For>
</RadioGroup.Root>
Read-Only
To read-only, set readOnly to true.
<RadioGroup.Root>
<For each={["solid", "subtle", "surface", "outline"]}>
{(variant) => (
<RadioGroup.Item key={variant} variant={variant} readOnly defaultChecked>
{toTitleCase(variant)}
</RadioGroup.Item>
)}
</For>
</RadioGroup.Root>
Invalid
To make invalid, set invalid to true.
<RadioGroup.Root>
<For each={["solid", "subtle", "surface", "outline"]}>
{(variant) => (
<RadioGroup.Item key={variant} variant={variant} invalid>
{toTitleCase(variant)}
</RadioGroup.Item>
)}
</For>
</RadioGroup.Root>
Change Border Color
To change the border color, set focusBorderColor or errorBorderColor to a color value.
<RadioGroup.Root>
<RadioGroup.Item value="1">Default border color</RadioGroup.Item>
<RadioGroup.Item focusBorderColor="green.500" value="2">
Custom border color
</RadioGroup.Item>
<RadioGroup.Item errorBorderColor="orange.500" invalid value="3">
Custom border color
</RadioGroup.Item>
</RadioGroup.Root>
Control
const [value, setValue] = useState("1")
const items = useMemo<RadioGroup.Item[]>(
() => [
{ label: "織田信長", value: "1" },
{ label: "豊臣秀吉", value: "2" },
{ label: "徳川家康", value: "3" },
],
[],
)
return <RadioGroup.Root items={items} value={value} onChange={setValue} />
"use client" to the top of the file.Use Custom Component
const { getInputProps, getLabelProps, getRootProps } = useRadioGroup({
defaultValue: "1",
})
const CustomRadio = ({ children, ...rest }) => {
return (
<Box
as="label"
css={{
"&:has(input:checked)": {
bg: "colorScheme.solid",
borderColor: "colorScheme.solid",
color: "colorScheme.contrast",
},
alignItems: "center",
borderWidth: "1px",
display: "flex",
h: "10",
px: "3",
rounded: "l2",
}}
{...getLabelProps()}
>
<Box as="input" {...getInputProps(rest)} />
{children}
</Box>
)
}
return (
<HStack {...getRootProps()}>
<CustomRadio value="1">織田信長</CustomRadio>
<CustomRadio value="2">豊臣秀吉</CustomRadio>
<CustomRadio value="3">徳川家康</CustomRadio>
</HStack>
)
"use client" to the top of the file.Props
Accessibility
The Radio follows the WAI-ARIA - Radio Pattern for accessibility.
If you are not using Field.Root, set aria-label or aria-labelledby to RadioGroup.Root.
<RadioGroup.Root aria-label="キャラクター">
<RadioGroup.Item value="孫悟空">孫悟空</RadioGroup.Item>
<RadioGroup.Item value="ベジータ">ベジータ</RadioGroup.Item>
<RadioGroup.Item value="フリーザ">フリーザ</RadioGroup.Item>
</RadioGroup.Root>
<VStack gap="sm">
<Text as="h3" id="label">
キャラクター
</Text>
<RadioGroup.Root aria-labelledby="label">
<RadioGroup.Item value="孫悟空">孫悟空</RadioGroup.Item>
<RadioGroup.Item value="ベジータ">ベジータ</RadioGroup.Item>
<RadioGroup.Item value="フリーザ">フリーザ</RadioGroup.Item>
</RadioGroup.Root>
</VStack>
Keyboard Navigation
| Key | Description | State |
|---|---|---|
Tab | Moves focus to the checked radio button within the radio group. If no radio button is checked, moves focus to the first radio button. | - |
Space | Checks the focused radio button. | - |
ArrowLeft | Checks the previous enabled radio button. | - |
ArrowRight | Checks the next enabled radio button. | - |
ArrowUp | Checks the previous enabled radio button. | - |
ArrowDown | Checks the next enabled radio button. | - |
ARIA Roles and Attributes
| Component | Roles and Attributes | Usage |
|---|---|---|
RadioGroup.Root | role="radiogroup" | Indicates that this is a radio group. |
aria-labelledby | If RadioGroup.Root is within a Field.Root and Field.Root has a label or Field.Label, sets its id. | |
aria-describedby | If RadioGroup.Root is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
Radio, RadioGroup.Item | aria-checked | Sets to "true" if the radio button is checked, and "false" if it is not checked. |
aria-describedby | If Radio or RadioGroup.Item is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
aria-disabled | Sets to "true" if disabled or readOnly is set. | |
aria-invalid | Sets to "true" if invalid is set. | |
aria-required | Sets to "true" if required is set. | |
RadioIndicator | aria-hidden | Excludes the element from the accessibility tree. |