suiss/ui · Component
Radio Group
A set of radio buttons where only one option can be selected at a time.
Radio Group is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Radio Group wraps the radio group primitive of the chosen base. RadioGroup holds the selected value and contains one RadioGroupItem per option. Each item renders a round control with an indicator dot when checked; pair it with a Label to give it visible text.
Use a radio group for a small set of mutually exclusive options that should all stay visible. Use Select or Native Select for longer lists, and Checkbox when several options can be selected.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a RadioGroup:
Examples
With descriptions
Each option has a title and a secondary description. Link them to the item with aria-labelledby and aria-describedby.
Disabled item
Set disabled on a single RadioGroupItem to disable that option. Arrow-key navigation skips it.
Set isDisabled on a single RadioGroupItem to disable that option. Arrow-key navigation skips it.
Disabled
Set disabled on the RadioGroup to disable every item. The group ignores clicks.
Set isDisabled on the RadioGroup to disable every item. The group ignores clicks.
Invalid
Set aria-invalid on the items to show them with a destructive border.
Set isInvalid on the group to show its items with a destructive border.
Accessibility
- The group has the
radiogrouprole and each item theradiorole witharia-checked. - Name the group with
aria-labeloraria-labelledby, and name each item with a wrappingLabeloraria-labelledby. - Arrow keys move focus and selection between items, skipping disabled ones.
- Set
aria-invalidon items to communicate a validation error.
- Set
isInvalidon the group to communicate a validation error.
- Each item has an enlarged invisible hit area for easier pointer targeting.
API Reference
RadioGroup
The container that manages the selected value and keyboard navigation.
| Prop | Type | Default |
|---|---|---|
defaultValue | any | — |
value | any | — |
onValueChange | (value: any) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
defaultValue | string | — |
value | string | — |
onValueChange | (value: string) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
defaultValue | string | null | — |
value | string | null | — |
onChange | (value: string) => void | — |
isDisabled | boolean | false |
isInvalid | boolean | false |
Also accepts the props of the primitive part it wraps.
The group is uncontrolled with defaultValue, or controlled with value and onValueChange, which is called when the selected item changes. Set disabled to disable every item.
The group is uncontrolled with defaultValue, or controlled with value and onChange, which is called when the selected item changes. Set isDisabled to disable every item, and isInvalid to mark the group as invalid.
RadioGroupItem
A single radio button with a checked indicator.
| Prop | Type | Default |
|---|---|---|
value | string | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
value | string | — |
isDisabled | boolean | false |
Also accepts the props of the primitive part it wraps.
Give each item a unique value; it identifies the item in the group's value. Wrap each item and its text in a Label so clicking the text selects the option. Items carry the peer class, so a following Label can react to their state, and the label switches to regular weight next to a radio item.
Each part sets a data-slot, such as radio-group or radio-group-item, for styling.
Web preview
RadioGroup is a View with the radiogroup role that holds the selected value. Each RadioGroupItem is a small round Pressable with the radio role; when checked it fills with the primary color and shows a dot. Items have an enlarged hit area so they are easy to press.
Installation
- Install the following dependencies:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/radio-group.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/radio-group.tsx:
- Update the import paths to match your project setup.
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/radio-group.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
With descriptions
Each option has a label and a line of description.
Web preview
Disabled item
One option is disabled and cannot be selected.
Web preview
Disabled
The whole group is disabled, so no item can be selected.
Web preview
Invalid
Items in the invalid state show a destructive border.
Web preview
Accessibility
- The group has the
radiogrouprole and reportsdisabledinaccessibilityState. - Each item has the
radiorole and reportscheckedanddisabledinaccessibilityState. - The indicator is hidden from screen readers; label each item with a linked
Labelor anaccessibilityLabel.
API Reference
RadioGroup
The container that holds the selected value.
Also accepts the props of RadioGroupProps<Value>.
Control the group with value and onValueChange(value, details), or leave it uncontrolled with defaultValue. Values are generic, so they can be strings or other types. Set disabled to disable all items. Selecting an item triggers a selection haptic when a haptics adapter is provided through HapticsContext.
RadioGroupItem
A single radio button with its checked indicator.
| Prop | Type | Default |
|---|---|---|
invalid | boolean | - |
Also accepts the props of RadioPrimitive.RootProps<Value>.
Give every item a unique value, and set disabled on a single item to disable it. invalid shows a destructive border while the item is unchecked. Put a Label next to each item and link it through nativeID and accessibilityLabelledBy. In dark mode unchecked items drop the border and shadow.
RadioGroup
The container that holds the selected value.
| Parameter | Type | Default |
|---|---|---|
selection | Binding<Value?> | - |
content | () -> Content | - |
RadioGroupItem
A single radio button with its checked indicator.
| Parameter | Type | Default |
|---|---|---|
value | Value | - |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
Overloads: init(value:label:), init(value:), init(_:value:).
RadioGroup
The container that holds the selected value.
| Parameter | Type | Default |
|---|---|---|
value | T? | - |
onValueChange | (T) -> Unit | - |
modifier | Modifier | Modifier |
enabled | Boolean | LocalFieldState.current.enabled |
invalid | Boolean | LocalFieldState.current.invalid |
content | @Composable ColumnScope.() -> Unit | - |
RadioGroupItem
A single radio button with its checked indicator.
| Parameter | Type | Default |
|---|---|---|
value | T | - |
modifier | Modifier | Modifier |
enabled | Boolean | true |
invalid | Boolean | false |
interactionSource | MutableInteractionSource? | null |
label | (@Composable () -> Unit)? | null |