Radio Group

Web/Mobile

A set of checkable buttons where only one can be checked at a time. Compose each option with Label via id and htmlFor.

Installation

pnpm dlx cubix-ui@latest add radio-group --base aria

Usage

Import
import { Label } from "@/components/cubix/label"
import { RadioGroup, RadioGroupItem } from "@/components/cubix/radio-group"
Example
<RadioGroup defaultValue="comfortable" aria-label="تراکم نمایش" className="w-fit">
  <div className="flex items-center gap-2">
    <RadioGroupItem id="radio-demo-default" value="default" />
    <Label htmlFor="radio-demo-default">پیش‌فرض</Label>
  </div>
  <div className="flex items-center gap-2">
    <RadioGroupItem id="radio-demo-comfortable" value="comfortable" />
    <Label htmlFor="radio-demo-comfortable">راحت</Label>
  </div>
  <div className="flex items-center gap-2">
    <RadioGroupItem id="radio-demo-compact" value="compact" />
    <Label htmlFor="radio-demo-compact">فشرده</Label>
  </div>
</RadioGroup>

Composition

RadioGroupItem is the control only. Pair each one with a Label using matching id and htmlFor. The group starts right-to-left and follows the closest dir on the page.

Structure
RadioGroup
└── div
    ├── RadioGroupItem (id, value)
    └── Label (htmlFor)

Accessibility

The group renders role="radiogroup"; name it with aria-label or point aria-labelledby at a visible heading. The group is a single tab stop, arrow keys move and select, and horizontal arrows follow the reading direction.

KeyAction
TabMoves focus into the group onto the selected radio, or the first one when none is selected.
SpaceSelects the focused radio when it is not selected yet.
ArrowDown / ArrowUpMoves to and selects the next or previous enabled radio.
ArrowLeft / ArrowRightMoves to and selects the next or previous enabled radio in reading direction, so the keys are mirrored in RTL.

Examples

With description

Wrap each option in a bordered row and add a helper line under the label.

Horizontal

Lay the options out in a row with className="flex". In RTL the first option sits on the right and ArrowLeft moves to the next one.

Disabled

Set disabled on one radio to lock it, or on the group to lock every option. Arrow keys skip disabled radios.

Invalid

Set aria-invalid on the group and its items when a required choice is missing. The radios get a destructive border and ring; show the error copy below.

یک دوره پرداخت انتخاب کنید.

Form

Give the group a name so the selected value is submitted with the form.

روش ارسال

زمان تحویل سفارش را انتخاب کنید.

API Reference

Note: Parts render data-slot attributes (radio-group, radio-group-item, radio-group-indicator) for targeting in tests and parent selectors. Use the same props on Base UI, React Aria, and Radix.

RadioGroup

PropTypeDefaultDescription
valuestring-Controlled selected value. Pair with onValueChange when the parent owns the value.
defaultValuestring-Initial selected value for uncontrolled usage.
onValueChange(value: string) => void-Called with the next value whenever a radio is selected.
namestring-Shared name submitted with the form. Generated automatically when omitted.
disabledbooleanfalseDisables every radio in the group.
requiredbooleanfalseMarks the group as required for form validation.
aria-invalidbooleanfalseMarks the group as invalid. On React Aria it also paints every radio; set aria-invalid on each item for Base UI and Radix.
dir"ltr" | "rtl"closest dir on the page (rtl if none)Reading direction. Sets which way ArrowLeft and ArrowRight move.
classNamestring-Additional Tailwind classes merged with the group styles (last one wins).

RadioGroupItem

PropTypeDefaultDescription
valuestring-Value submitted when this radio is selected. Required.
idstring-Associates the control with a Label through htmlFor for accessible naming.
disabledbooleanfalseDisables this radio independently of the group. Arrow keys skip it.
aria-invalidbooleanfalseAdds a destructive border and ring. Show the error copy below the group.
classNamestring-Additional Tailwind classes merged onto the radio control (last one wins).

States

PropTypeDefaultDescription
checked--Primary fill with a filled indicator dot.
disabled--Unchecked stays full opacity; checked uses reduced opacity.
aria-invalid--Destructive border and ring. Show error copy below the group.