Radio Group
A set of mutually exclusive options, built on native radios.
Installation
Shadwire follows shadcn/ui's open code model: the component's source is copied into your application and becomes yours. Install it with the CLI:
The radio-group registry item installs these files:
-
app/components/ui_component.rb -
app/components/ui/radio_group_component.rb -
app/components/ui/radio_group/item_component.rb -
app/helpers/ui/radio_group_helper.rb -
vendor/shadwire/shadwire.css
Haven't installed the CLI yet? gem install shadwire, then shadwire init.
Usage
Items that share a name:
form a group, and the browser handles single selection and arrow-key navigation.
Label the group with aria: { label: }.
Or render the components directly:
Composition
Each item is a native <input type="radio">;
the name: is repeated on every item
because ViewComponent injects no parent context.
Examples
Default
Native radios that share one name:, so the browser handles arrow-key navigation.
Disabled item
disabled: true disables one item without affecting the group.
With form_with
Use form.field_name for the shared name and form.field_id(attr, value) for each id.
API reference
ui_radio_group(**options, &block) and
ui_radio_group_item(**options)
wrap the matching components.
| Component | Argument | Default | Description |
|---|---|---|---|
RadioGroup |
class / class_name, **attrs |
— | The role="radiogroup" container; label it with aria: { label: }. |
RadioGroup::Item |
name: (required) |
— | The name shared by the group's radios. |
RadioGroup::Item |
value: (required) |
— | The value submitted when the item is selected. |
RadioGroup::Item |
checked / disabled |
false |
The native input's initial state. |
RadioGroup::Item |
**attrs |
{} |
Free HTML attributes (id, data:, aria:, …). |