Radio Group
Allows users to select a single option from a list of mutually exclusive choices.
Structure
Reusable Components
It's recommended to use the RadioGroup
primitives to create your own custom components that can be used throughout your application.
In the example below, we're creating a custom MyRadioGroup
component that takes in an array of items and renders a radio group with those items along with a Label
component for each item.
You can then use the MyRadioGroup
component in your application like so:
Managing Value State
Bits UI offers several approaches to manage and synchronize the Radio Group's value state, catering to different levels of control and integration needs.
1. Two-Way Binding
For seamless state synchronization, use Svelte's bind:value
directive. This method automatically keeps your local state in sync with the component's internal state.
Key Benefits
- Simplifies state management
- Automatically updates
myValue
when the internal state changes (e.g., via clicking on an item) - Allows external control (e.g., selecting an item via a separate button)
2. Change Handler
For more granular control or to perform additional logic on state changes, use the onValueChange
prop. This approach is useful when you need to execute custom logic alongside state updates.
Use Cases
- Implementing custom behaviors on value change
- Integrating with external state management solutions
- Triggering side effects (e.g., logging, data fetching)
3. Fully Controlled
For complete control over the component's value state, use the controlledValue
prop. This approach requires you to manually manage the value state, giving you full control over when and how the component responds to value change events.
To implement controlled state:
- Set the
controlledValue
prop totrue
on theRadioGroup.Root
component. - Provide a
value
prop toRadioGroup.Root
, which should be a variable holding the current state. - Implement an
onValueChange
handler to update the state when the internal state changes.
When to Use
- Implementing complex open/close logic
- Coordinating multiple UI elements
- Debugging state-related issues
Note
While powerful, fully controlled state should be used judiciously as it increases complexity and can cause unexpected behaviors if not handled carefully.
For more in-depth information on controlled components and advanced state management techniques, refer to our Controlled State documentation.
HTML Forms
If you set the name
prop on the RadioGroup.Root
component, a hidden input element will be rendered to submit the value of the radio group to a form.
Required
To make the hidden input element required
you can set the required
prop on the RadioGroup.Root
component.
Disabling Items
You can disable a radio group item by setting the disabled
prop to true
.
Orientation
The orientation
prop is used to determine the orientation of the radio group, which influences how keyboard navigation will work.
When the orientation
is set to 'vertical'
, the radio group will navigate through the items using the ArrowUp
and ArrowDown
keys. When the orientation
is set to 'horizontal'
, the radio group will navigate through the items using the ArrowLeft
and ArrowRight
keys.
API Reference
The radio group component used to group radio items under a common name for form submission.
Property | Type | Description |
---|---|---|
value $bindable | string | The value of the currently selected radio item. You can bind to this value to control the radio group's value from outside the component. Default: undefined |
onValueChange | function | A callback that is fired when the radio group's value changes. Default: undefined |
controlledValue | boolean | Whether or not the Default: false |
disabled | boolean | Whether or not the radio group is disabled. This prevents the user from interacting with it. Default: false |
required | boolean | Whether or not the radio group is required. Default: false |
name | string | The name of the radio group used in form submission. If provided, a hidden input element will be rendered to submit the value of the radio group. Default: undefined |
loop | boolean | Whether or not the radio group should loop through the items when navigating with the arrow keys. Default: false |
orientation | enum | The orientation of the radio group. This will determine how keyboard navigation will work within the component. Default: 'vertical' |
ref $bindable | HTMLDivElement | The underlying DOM element being rendered. You can bind to this to get a reference to the element. Default: undefined |
children | Snippet | The children content to render. Default: undefined |
child | Snippet | Use render delegation to render your own element. See Child Snippet docs for more information. Default: undefined |
Data Attribute | Value | Description |
---|---|---|
data-orientation | enum | The orientation of the radio group. |
data-radio-group-root | '' | Present on the root element. |
An radio item, which must be a child of the RadioGroup.Root
component.
Property | Type | Description |
---|---|---|
value required | string | The value of the radio item. This should be unique for each radio item in the group. Default: undefined |
disabled | boolean | Whether the radio item is disabled. Default: false |
ref $bindable | HTMLButtonElement | The underlying DOM element being rendered. You can bind to this to get a reference to the element. Default: undefined |
children | Snippet | The children content to render. Default: undefined |
child | Snippet | Use render delegation to render your own element. See Child Snippet docs for more information. Default: undefined |
Data Attribute | Value | Description |
---|---|---|
data-disabled | '' | Present when the radio item is disabled. |
data-value | '' | The value of the radio item. |
data-state | enum | The radio item's checked state. |
data-orientation | enum | The orientation of the parent radio group. |
data-radio-group-item | '' | Present on the radio item element. |