shadcn.io is not affiliated with official shadcn/ui
Shadcn Radio Group for React
Allows single selection from multiple options.
Installation
bunx --bun shadcn@latest add https://kit.dev/r/aria/radio-group.jsonnpx shadcn@latest add https://kit.dev/r/aria/radio-group.jsonpnpm dlx shadcn@latest add https://kit.dev/r/aria/radio-group.jsonyarn shadcn@latest add https://kit.dev/r/aria/radio-group.jsonThis component depends on Field. Install it first if you haven't already.
Install the following dependencies:
bun add react-aria-componentsnpm install react-aria-componentspnpm add react-aria-componentsyarn add react-aria-componentsCopy and paste the following code into your project.
"use client";
import type React from "react";
import {
composeRenderProps,
RadioGroup as RadioGroupPrimitive,
type RadioGroupProps,
Radio as RadioPrimitive,
type RadioProps,
} from "react-aria-components";
import { cn } from "@/lib/utils";
import { FieldLabel } from "@/components/ui/field";
export const RadioGroup = (props: RadioGroupProps) => {
const { className, ...rest } = props;
return (
<RadioGroupPrimitiveUpdate the import paths to match your project setup.
Anatomy
RadioGroup
├── RadioGroupLabel
├── RadioGroupItem
│ ├── RadioGroupText
│ └── RadioGroupItemControl
└── RadioGroupIndicatorUsage
import {
RadioGroup,
RadioGroupItem,
} from "@/components/ui/radio-group";<RadioGroup className="w-fit" defaultValue="comfortable">
<RadioGroupItem value="default">Default</RadioGroupItem>
<RadioGroupItem value="comfortable">Comfortable</RadioGroupItem>
<RadioGroupItem value="compact">Compact</RadioGroupItem>
</RadioGroup>Accessibility
| Key | Description |
|---|---|
Space | Activate the focused control. |
End | Go to the end. |
Demos
Description
Radio group items with a description using the Field component.
Choice Card
Use FieldLabel to wrap the entire Field for a clickable card-style selection.
Fieldset
Use FieldSet and FieldLegend to group radio items with a label and description.
Disabled
Use the disabled prop on RadioGroup to disable all items.
Invalid
Use aria-invalid on RadioGroupItem and data-invalid on Field to show validation errors.
Rtl
Right-to-left radio group. See the RTL configuration guide for document direction.
API Reference
RadioGroup
PropType
AttributeType
RadioGroupText
PropType
AttributeType
RadioGroupLabel
PropType
AttributeType
RadioGroupItem
PropType
AttributeType