No results found

Radio Group

Select a single option from a list of choices with keyboard navigation and accessibility support.

Features

  • WAI ARIA RadioGroup design pattern
  • Single selection from multiple options
  • Error state handling and validation
  • Custom error messages with aria-invalid
  • Optional description text support
  • Visual and screen reader feedback
  • Controlled and uncontrolled value management
  • Custom accessible labels and descriptions
  • Disabled state support
  • Focus management with tab navigation
  • Hidden native radio inputs for form submission
  • Visual indicators for selected state
  • Keyboard navigation support
  • Required field validation

Anatomy

PartDescription
<RadioGroup.Root>Root component that manages the radio group's state and behavior
<RadioGroup.Indicator>Visual indicator component that shows the selected state of a radio option
<RadioGroup.Trigger>Interactive trigger component that handles radio option selection
<RadioGroup.Label>Label component for the radio group or individual radio options
<RadioGroup.Description>A description component for the radio group that provides additional context
<RadioGroup.HiddenNativeInput>—
<RadioGroup.ErrorMessage>Displays error message when radio group validation fails
<RadioGroup.Item>Individual radio option container component

Examples

Basic Usage

The most basic radio group implementation requires a RadioGroup.Root component containing RadioGroup.Item components with their associated values.

This example demonstrates:

Error Handling

Radio groups can display error messages when validation fails, such as when no option is selected.

Error: Please select an item.

This example shows:

Component State

Using Component State

The Radio Group component provides several ways to manage its state through props on the RadioGroup.Root component.

  1. Controlled Value As shown in the controlledValue example above, you can control the selected value using the value prop:
const value = useSignal<string>("Item 2");
<RadioGroup.Root value={value.value}>
  // Radio items
</RadioGroup.Root>
  1. Uncontrolled Value You can set an initial uncontrolled value using the defaultValue prop:
<RadioGroup.Root defaultValue="Item 1">
  // Radio items
</RadioGroup.Root>
  1. Required State Make the radio group required by setting the required prop:
<RadioGroup.Root required>
  // Radio items
</RadioGroup.Root>
  1. Disabled State Disable the entire radio group using the disabled prop:
<RadioGroup.Root disabled>
  // Radio items
</RadioGroup.Root>

State Interactions

  1. Handling Selection Changes Listen for selection changes using the onChange$ prop:
<RadioGroup.Root 
  onChange$={(value) => {
    // Handle the new selected value
    console.log('Selected:', value);
  }}
>
  // Radio items
</RadioGroup.Root>
  1. Error State As shown in the error example above, the Radio Group automatically manages error states when validation fails. The error state is cleared when a valid selection is made.
  2. Description State Enable description support by setting the isDescription prop:
<RadioGroup.Root isDescription>
  <RadioGroup.Description>
    Additional context about the radio group
  </RadioGroup.Description>
  // Radio items
</RadioGroup.Root>

The state management in Radio Group is designed to handle common use cases while maintaining accessibility and providing clear feedback to users through visual indicators and ARIA attributes.

Based on the provided implementation and examples, I'll document the core configuration options for the Radio Group component.

Core Configuration

Value Management

The Radio Group can be configured in both controlled and uncontrolled modes. The value can be managed through the value prop on RadioGroup.Root. As shown in the controlledValue example above, you can control the selected value by passing a value prop:

The value prop accepts a string and determines which radio option is currently selected.

Required Validation

The Radio Group supports required field validation through the required prop on RadioGroup.Root. When enabled, it enforces selection before form submission. As shown in the error example above, the component displays an error message when no option is selected and the field is required.

Description Support

The Radio Group can be configured to display additional descriptive text through the isDescription prop on RadioGroup.Root.

When isDescription is true but no description is provided, a warning will be logged to the console.

Disabled State

The entire Radio Group can be disabled using the disabled prop on RadioGroup.Root. When disabled:

type DisabledConfig = {
  disabled?: boolean; // Disables all radio options when true
}

Event Handling

The Radio Group supports change event handling through the onChange$ prop:

type ChangeHandler = {
  onChange$?: QRL<(checked: string) => void>;
}

This handler is called whenever a radio option is selected, receiving the new value as an argument.

Hidden Input Configuration

For form submission, the component includes a hidden native radio input that can be configured with:

type HiddenInputConfig = {
  name?: string;      // Form field name
  required?: boolean; // Required field validation
  value?: string;     // Current value
}

These configurations ensure proper form integration while maintaining accessibility and native form behavior.

Forms

The Radio Group component includes form integration features through the RadioGroup.HiddenNativeInput component and built-in validation support.

Form Validation

The Radio Group provides built-in validation with error handling through the RadioGroup.ErrorMessage component.

Error: Please select an item.

In this example, the following form-specific features are demonstrated:

Form Integration

The component includes a hidden native radio input through RadioGroup.HiddenNativeInput that enables form submission and browser-native validation. Key form integration features:

API Reference

Radio Group Root

Inherits from: <div />

Props

PropTypeDefaultDescription
"bind:value"Signal<boolean>-
onChange$QRL<(checked: string) => void>-
defaultValuestring-
disabledboolean-
isDescriptionboolean-
namestring-
requiredboolean-
valuestring-

Data Attributes

AttributeDescription
data-disabledIndicates whether the radio group is disabled
data-checkedIndicates whether the radio group has a selected value

Radio Group Trigger

Inherits from: <button />

Props

PropTypeDefaultDescription
valuestring-
_indexnumber-

Data Attributes

AttributeDescription
data-disabledIndicates whether this radio trigger is disabled
data-checkedIndicates whether this radio trigger is checked

Radio Group Indicator

Data Attributes

AttributeDescription
data-hiddenIndicates whether the indicator is hidden based on selection state
data-checkedIndicates whether this indicator is in a checked state

Radio Group Error Message

Inherits from: <div />

Data Attributes

AttributeDescription
data-visibleIndicates whether the error message is currently visible

Radio Group Item

Inherits from: <div />

Props

PropTypeDefaultDescription
valuestring-

Radio Group Description

Inherits from: <div />

Radio Group Hidden Input

Inherits from: <input />

Radio Group Label

Inherits from: <label />

Accessibility

Keyboard Interactions

KeyDescription
SpaceWhen focus is on a radio button trigger, selects that radio option
EnterWhen focus is on a radio button trigger, selects that radio option
ArrowDownMoves focus to the next radio button trigger in the group
ArrowRightMoves focus to the next radio button trigger in the group
ArrowUpMoves focus to the previous radio button trigger in the group
ArrowLeftMoves focus to the previous radio button trigger in the group
TabMoves focus to the next focusable element outside the radio group
Shift+TabMoves focus to the previous focusable element outside the radio group