Forms
RadioGroup
RadioGroup is a full form field — pass `label`, `helpText`, `error`, and an `options` array, or compose custom layouts with `Radio` children.
Installation
npx shadcn add https://rck.vibeboxph.com//r/radio-group.jsonLive preview & controls
For side projects and experiments.
For teams shipping production apps.
Custom support, SSO, and SLA.
Pick the tier that fits your team.
Controls
<RadioGroup label="Choose a plan" defaultValue="pro" options={[...]} />Anatomy
RadioGroupField container with label, options list, and group help text.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| options | { value: string; text: string; disabled?: boolean; helpText?: React.ReactNode }[] | — | Declarative option list. |
| value / defaultValue | string | — | Controlled / uncontrolled selected value. |
| onValueChange | (value: string) => void | — | Fires when the selection changes. |
| label | React.ReactNode | — | Top field label. |
| helpText | React.ReactNode | — | Group-level help text. |
| error | React.ReactNode | — | Error text; sets `aria-invalid` on the group. |
| orientation | "horizontal" | "vertical" | vertical | Layout and keyboard navigation direction. |
| controlPosition | "start" | "end" | start | Inline-start/end placement for each option. |
Accessibility
- Implements the WAI-ARIA Radio Group pattern with roving focus and arrow-key navigation.
- Group `label`, `helpText`, and `error` are wired via `aria-labelledby` / `aria-describedby`.
Source
registry/rck/ui/radio-group.tsx
"use client";
import * as React from "react";
import * as RadioGroupPrimitive from "@/registry/rck/primitives/radio-group";
import { cn } from "@/lib/utils";
import { Radio } from "@/registry/rck/ui/radio";
import {
FieldLabel,
FieldMessage,
type BooleanGroupOption,
type ControlPosition,
} from "@/registry/rck/ui/field-chrome";
export type RadioGroupOption = BooleanGroupOption;
export interface RadioGroupProps extends Omit<
React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root>,
"children"
> {
label?: React.ReactNode;
helpText?: React.ReactNode;
error?: React.ReactNode;
required?: boolean;
containerClassName?: string;
options?: ReadonlyArray<RadioGroupOption>;
controlPosition?: ControlPosition;
children?: React.ReactNode;
}
export const RadioGroup = React.forwardRef<
React.ComponentRef<typeof RadioGroupPrimitive.Root>,
RadioGroupProps
>(
(
{
className,
containerClassName,
label,
helpText,
error,
required,
options,
controlPosition = "start",
orientation = "vertical",
id: providedId,
children,
disabled,
...props
},
ref,
) => {
const generatedId = React.useId();
const groupId = providedId ?? generatedId;
const helpTextId = helpText && !error ? `${groupId}-help-text` : undefined;
const errorId = error ? `${groupId}-error` : undefined;
const describedBy = cn(helpTextId, errorId) || undefined;
const hasOptions = Boolean(options?.length);
const content = hasOptions
? options!.map((option) => (
<Radio
key={option.value}
value={option.value}
text={option.text}
helpText={option.helpText}
controlPosition={controlPosition}
disabled={disabled || option.disabled}
/>
))
: children;
return (
<div
data-slot="radio-group-container"
className={cn("flex flex-col gap-1.5", containerClassName)}
>
{label && (
<FieldLabel id={`${groupId}-label`} required={required}>
{label}
</FieldLabel>
)}
<RadioGroupPrimitive.Root
ref={ref}
id={groupId}
data-slot="radio-group"
aria-labelledby={label ? `${groupId}-label` : undefined}
aria-describedby={describedBy}
aria-invalid={error ? true : undefined}
aria-required={required || undefined}
orientation={orientation}
disabled={disabled}
className={cn(
hasOptions && orientation === "horizontal"
? "flex flex-wrap gap-4"
: "grid gap-2",
className,
)}
{...props}
>
{content}
</RadioGroupPrimitive.Root>
{helpText && !error && <FieldMessage id={helpTextId}>{helpText}</FieldMessage>}
{error && (
<FieldMessage id={errorId} invalid role="alert">
{error}
</FieldMessage>
)}
</div>
);
},
);
RadioGroup.displayName = RadioGroupPrimitive.Root.displayName;
registry/rck/ui/radio.tsx
"use client";
import * as React from "react";
import * as RadioGroupPrimitive from "@/registry/rck/primitives/radio-group";
import { Circle } from "@/lib/icons";
import { cn } from "@/lib/utils";
import { BooleanFieldLayout, type ControlPosition } from "@/registry/rck/ui/field-chrome";
export type RadioControlProps = React.ComponentPropsWithoutRef<
typeof RadioGroupPrimitive.Item
>;
/** Bare radio control — compose with `Radio` inside `RadioGroup`. */
export const RadioControl = React.forwardRef<
React.ComponentRef<typeof RadioGroupPrimitive.Item>,
RadioControlProps
>(({ className, ...props }, ref) => (
<RadioGroupPrimitive.Item
ref={ref}
data-slot="radio-control"
className={cn(
"group border-border-strong bg-background aspect-square size-4 shrink-0 rounded-full border shadow-sm " +
"focus-visible:ring-ring transition-colors focus-visible:ring-2 focus-visible:outline-none " +
"focus-visible:ring-offset-background focus-visible:ring-offset-2 " +
"disabled:cursor-not-allowed disabled:opacity-50 " +
"data-[state=checked]:border-primary data-[state=checked]:text-primary",
className,
)}
{...props}
>
<RadioGroupPrimitive.Indicator
data-slot="radio-indicator"
className="flex items-center justify-center"
>
<Circle className="size-2 fill-current text-current" aria-hidden="true" />
</RadioGroupPrimitive.Indicator>
</RadioGroupPrimitive.Item>
));
RadioControl.displayName = "RadioControl";
export function Radio({
className,
text,
helpText,
controlPosition = "start",
id,
disabled,
...props
}: RadioControlProps & {
text: React.ReactNode;
helpText?: React.ReactNode;
controlPosition?: ControlPosition;
id?: string;
}) {
return (
<BooleanFieldLayout
text={text}
helpText={helpText}
controlPosition={controlPosition}
disabled={disabled}
id={id}
className={className}
>
{({ id: fieldId, describedBy, disabled: fieldDisabled }) => (
<RadioControl
id={fieldId}
disabled={fieldDisabled}
aria-describedby={describedBy}
{...props}
/>
)}
</BooleanFieldLayout>
);
}
/** @deprecated Use `RadioControl` instead. */
export const RadioGroupItem = RadioControl;
/** @deprecated Use `Radio` instead. */
export const RadioGroupField = Radio;
registry/rck/ui/field-chrome.tsx
import * as React from "react";
import { cva, type VariantProps } from "@/lib/cva";
import { cn } from "@/lib/utils";
export const fieldWrapperVariants = cva(
"flex w-full items-center gap-2 rounded-md border bg-background px-3 shadow-sm " +
"transition-colors has-[input:disabled]:cursor-not-allowed has-[input:disabled]:opacity-50 " +
"has-[textarea:disabled]:cursor-not-allowed has-[textarea:disabled]:opacity-50",
{
variants: {
size: {
sm: "h-8 text-[10px]",
md: "h-[39px] text-[11px]",
lg: "h-11 text-xs",
},
invalid: {
true: "border-destructive focus-within:ring-2 focus-within:ring-destructive/40",
false:
"border-border-strong focus-within:border-primary focus-within:ring-2 focus-within:ring-ring/30",
},
},
defaultVariants: {
size: "md",
invalid: false,
},
},
);
/** Built-in field label — shared by Input, Textarea, and boolean field wrappers. */
export function FieldLabel({
className,
required,
children,
...props
}: React.LabelHTMLAttributes<HTMLLabelElement> & { required?: boolean }) {
return (
<label
data-slot="field-label"
className={cn("text-foreground text-sm leading-none font-medium", className)}
{...props}
>
{children}
{required && <span className="text-destructive ml-0.5">*</span>}
</label>
);
}
export function FieldMessage({
className,
invalid,
...props
}: React.HTMLAttributes<HTMLParagraphElement> & { invalid?: boolean }) {
return (
<p
data-slot="field-message"
className={cn(
"text-xs",
invalid ? "text-destructive" : "text-muted-foreground",
className,
)}
{...props}
/>
);
}
export type ControlPosition = "start" | "end";
export interface BooleanGroupOption {
value: string;
/** Inline label beside the control. */
text: string;
disabled?: boolean;
helpText?: React.ReactNode;
}
export interface BooleanFieldLayoutProps {
label?: React.ReactNode;
text?: React.ReactNode;
helpText?: React.ReactNode;
error?: React.ReactNode;
required?: boolean;
/** Places the control at inline-start (default) or inline-end — flips automatically in RTL. */
controlPosition?: ControlPosition;
disabled?: boolean;
id?: string;
className?: string;
containerClassName?: string;
children: (args: {
id: string;
describedBy?: string;
disabled?: boolean;
}) => React.ReactNode;
}
export function BooleanFieldLayout({
label,
text,
helpText,
error,
required,
controlPosition = "start",
disabled,
id: providedId,
className,
containerClassName,
children,
}: BooleanFieldLayoutProps) {
const generatedId = React.useId();
const id = providedId ?? generatedId;
const helpTextId = helpText && !error ? `${id}-help-text` : undefined;
const errorId = error ? `${id}-error` : undefined;
const describedBy = cn(helpTextId, errorId) || undefined;
const control = children({ id, describedBy, disabled });
return (
<div
data-slot="boolean-field"
className={cn("flex flex-col gap-1.5", containerClassName)}
>
{label && (
<FieldLabel htmlFor={id} required={required}>
{label}
</FieldLabel>
)}
<div
className={cn(
"flex w-full items-start gap-2",
controlPosition === "end" && "flex-row-reverse justify-between",
className,
)}
>
{control}
{text && (
<label
htmlFor={id}
className={cn(
"text-foreground text-sm leading-none font-medium",
controlPosition === "end" && "flex-1",
disabled && "cursor-not-allowed opacity-50",
)}
>
{text}
</label>
)}
</div>
{helpText && !error && <FieldMessage id={helpTextId}>{helpText}</FieldMessage>}
{error && (
<FieldMessage id={errorId} invalid role="alert">
{error}
</FieldMessage>
)}
</div>
);
}
export type FieldWrapperVariantProps = VariantProps<typeof fieldWrapperVariants>;
Related examples
Options APIPlan picker with label, help text, and per-option descriptions.
Horizontal optionsTheme preference rendered inline from the options array.