import { HStack } from "@chakra-ui/react"
import { Radio, RadioGroup } from "@/components/ui/radio"
const Demo = () => {
return (
<RadioGroup defaultValue="1">
<HStack gap="6">
<Radio value="1">Option 1</Radio>
<Radio value="2">Option 2</Radio>
<Radio value="3">Option 3</Radio>
</HStack>
</RadioGroup>
)
}
Setup
If you don't already have the snippet, run the following command to add the
radio
snippet
npx @chakra-ui/cli snippet add radio
The snippet includes a closed component composition for the RadioGroup
Usage
import { Radio, RadioGroup } from "@/components/ui/radio"
<RadioGroup>
<Radio />
</RadioGroup>
Examples
Controlled
Use the value
and onValueChange
prop to control the selected radio button
"use client"
import { HStack } from "@chakra-ui/react"
import { Radio, RadioGroup } from "@/components/ui/radio"
import { useState } from "react"
const Demo = () => {
const [value, setValue] = useState("1")
return (
<RadioGroup value={value} onValueChange={(e) => setValue(e.value)}>
<HStack gap="6">
<Radio value="1">Option 1</Radio>
<Radio value="2">Option 2</Radio>
<Radio value="3">Option 3</Radio>
</HStack>
</RadioGroup>
)
}
Colors
Use the colorPalette
prop to change the color scheme of the component.
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
import { HStack, Stack, Text } from "@chakra-ui/react"
import { colorPalettes } from "compositions/lib/color-palettes"
import { Radio, RadioGroup } from "@/components/ui/radio"
const Demo = () => {
return (
<Stack gap="2" align="flex-start">
{colorPalettes.map((colorPalette) => (
<HStack key={colorPalette} gap="10" px="4">
<Text minW="8ch">{colorPalette}</Text>
<RadioGroup
colorPalette={colorPalette}
defaultValue="react"
spaceX="8"
>
<Radio value="react">React</Radio>
<Radio value="vue">Vue</Radio>
<Radio value="solid">Solid</Radio>
</RadioGroup>
</HStack>
))}
</Stack>
)
}
Sizes
Use the size
prop to change the size of the radio component.
import { HStack } from "@chakra-ui/react"
import { Radio, RadioGroup } from "@/components/ui/radio"
const Demo = () => {
return (
<HStack gap="4">
<RadioGroup size="sm">
<Radio value="react">Radio (sm)</Radio>
</RadioGroup>
<RadioGroup size="md">
<Radio value="react">Radio (md)</Radio>
</RadioGroup>
<RadioGroup size="lg">
<Radio value="react">Radio (lg)</Radio>
</RadioGroup>
</HStack>
)
}
Variants
Use the variant
prop to change the appearance of the radio component.
import { For, Stack } from "@chakra-ui/react"
import { Radio, RadioGroup } from "@/components/ui/radio"
const Demo = () => {
return (
<Stack gap="4">
<For each={["solid", "outline", "subtle"]}>
{(variant) => (
<RadioGroup
key={variant}
variant={variant}
defaultValue="react"
spaceX="4"
colorPalette="teal"
>
<Radio value="react" minW="120px">
Radio ({variant})
</Radio>
<Radio value="vue">Vue ({variant})</Radio>
</RadioGroup>
)}
</For>
</Stack>
)
}
Disabled
Use the disabled
prop to make the radio disabled.
import { HStack } from "@chakra-ui/react"
import { Radio, RadioGroup } from "@/components/ui/radio"
const Demo = () => {
return (
<RadioGroup defaultValue="2">
<HStack gap="6">
<Radio value="1" disabled>
Option 1
</Radio>
<Radio value="2">Option 2</Radio>
<Radio value="3">Option 3</Radio>
</HStack>
</RadioGroup>
)
}
Hook Form
Use the Controller
component from react-hook-form
to control the radio group
withing a form
"use client"
import { Button, Fieldset, HStack } from "@chakra-ui/react"
import { zodResolver } from "@hookform/resolvers/zod"
import { Radio, RadioGroup } from "@/components/ui/radio"
import { Controller, useForm } from "react-hook-form"
import { z } from "zod"
const items = [
{ value: "1", label: "Option 1" },
{ value: "2", label: "Option 2" },
{ value: "3", label: "Option 3" },
]
const formSchema = z.object({
value: z.string({ message: "Value is required" }),
})
type FormValues = z.infer<typeof formSchema>
const Demo = () => {
const {
control,
handleSubmit,
formState: { errors },
} = useForm<FormValues>({
resolver: zodResolver(formSchema),
})
const onSubmit = handleSubmit((data) => console.log(data))
return (
<form onSubmit={onSubmit}>
<Fieldset.Root invalid={!!errors.value}>
<Fieldset.Legend>Select value</Fieldset.Legend>
<Controller
name="value"
control={control}
render={({ field }) => (
<RadioGroup
name={field.name}
value={field.value}
onValueChange={({ value }) => {
field.onChange(value)
}}
>
<HStack gap="6">
{items.map((item) => (
<Radio
key={item.value}
value={item.value}
inputProps={{ onBlur: field.onBlur }}
>
{item.label}
</Radio>
))}
</HStack>
</RadioGroup>
)}
/>
{errors.value && (
<Fieldset.ErrorText>{errors.value?.message}</Fieldset.ErrorText>
)}
<Button size="sm" type="submit" alignSelf="flex-start">
Submit
</Button>
</Fieldset.Root>
</form>
)
}
Props
Root
Prop | Default | Type |
---|---|---|
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent' The color palette of the component |
variant | 'outline' | 'outline' | 'subtle' | 'classic' The variant of the component |
size | 'md' | 'sm' | 'md' | 'lg' The size of the component |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultValue | string The initial value of the radio group when it is first rendered. Use when you do not need to control the state of the radio group. | |
disabled | boolean If `true`, the radio group will be disabled | |
form | string The associate form of the underlying input. | |
id | string The unique identifier of the machine. | |
ids | Partial<{
root: string
label: string
indicator: string
item(value: string): string
itemLabel(value: string): string
itemControl(value: string): string
itemHiddenInput(value: string): string
}> The ids of the elements in the radio. Useful for composition. | |
name | string The name of the input fields in the radio (Useful for form submission). | |
onValueChange | (details: ValueChangeDetails) => void Function called once a radio is checked | |
orientation | 'horizontal' | 'vertical' Orientation of the radio group | |
readOnly | boolean Whether the checkbox is read-only | |
value | string The value of the checked radio |