Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Radio Group Primitive

Use Radio Group when someone can pick one option from a visible list. The arrow keys move between choices.

Choose a plan

Anatomy

Use the Astro primitive adapter to render RadioGroup anatomy with the Runtime wiring included.

---
import { RadioGroup } from "@starwind-ui/astro/radio-group";
---
<RadioGroup.Root />

Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโ€™s types for the available props and events. Plain HTML uses DOM events and Runtime methods.

API Reference

Root

The main element that owns the Radio Group Runtime instance.

Default element
div
Discovery hook
data-sw-radio-group
Role
radiogroup

Props

Prop Type Default Toggle
value RadioGroupValue -
Description
Controls the current Radio Group value.
Kind
control
Targets
-
Full type
RadioGroupValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-default-value
  • starwind:value-change
  • setValue
defaultValue RadioGroupValue -
Description
Sets the initial Radio Group value for uncontrolled usage.
Kind
control
Targets
-
Full type
RadioGroupValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-default-value
  • starwind:value-change
  • setValue
disabled boolean false
Description
Disables the Root part.
Kind
option
Targets
-
Full type
boolean
form string -
Description
Associates the control with a form element.
Kind
option
Targets
-
Full type
string
name string -
Description
Sets the submitted form field name.
Kind
option
Targets
-
Full type
string
orientation "horizontal" | "vertical" "vertical"
Description
Sets the Radio Group orientation.
Kind
option
Targets
-
Full type
"horizontal" | "vertical"
readOnly boolean false
Description
Marks the control as read-only.
Kind
option
Targets
-
Full type
boolean
required boolean false
Description
Marks the form control as required.
Kind
option
Targets
-
Full type
boolean
onValueChange (value: string, details: RadioGroupValueChangeDetails) => void -
Description
Runs when the Radio Group value changes.
Kind
callback
Targets
-
Full type
(value: string, details: RadioGroupValueChangeDetails) => void
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-default-value
  • starwind:value-change
  • setValue

Events

Event Callback Value Toggle
valueChange onValueChange value: string
Description
Fires when the value changes for Radio Group.
DOM event
starwind:value-change
Details type
RadioGroupValueChangeDetails
Timing
before-state-commit
Cancelable
Yes
Cancellation sequence
  1. Check internal eligibility and intent.
  2. Create one details object for the proposal.
  3. Call the Runtime callback with the details object when the controller exposes one.
  4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
  5. Read details.isCanceled, including cancellation caused by preventDefault().
  6. Apply the accepted state.
  7. Notify Runtime subscribers and other accepted-only observers.

State

State Type Props Toggle
value RadioGroupValue value / defaultValue
Description
Tracks the current Radio Group value.
Initial attribute
data-default-value
Runtime getter
getValue
Runtime setter
setValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-default-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-default-value
  • starwind:value-change
  • setValue

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-radio-group - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-value - Reflects the value state on the Root part.
Metadata
Attribute Value Description
data-default-value - Reflects the default value prop on the Root part.
data-disabled - Reflects the disabled prop on the Root part.
data-form - Reflects the form prop on the Root part.
data-name - Reflects the name prop on the Root part.
data-orientation - Reflects the orientation prop on the Root part.
data-readonly - Reflects the readonly prop on the Root part.
data-required - Reflects the required prop on the Root part.

Runtime API

Factory
createRadioGroup
Import
@starwind-ui/runtime/radio-group
Root hook
root data-sw-radio-group
Option props
defaultValue, disabled, form, name, orientation, readOnly, required, value

Runtime Setters

Method Target Description
setValue state: value Updates the current Radio Group value from Runtime code.
setDisabled prop: disabled Updates whether Radio Group is disabled from Runtime code.
setFormOptions props: form, name, required Updates Radio Group form-related options from Runtime code.
setName prop: name Updates the Radio Group form field name from Runtime code.
setOrientation prop: orientation Updates the Radio Group orientation from Runtime code.
setReadOnly prop: readOnly Updates whether Radio Group is read-only from Runtime code.
setRequired prop: required Updates whether Radio Group is required from Runtime code.

Form Participation

FactValue
Form propsform, name, required, value
Hidden input-
Field integrationYes
Component Relationship
Radio Group Composite

Changelog

View version history v1.2.0 8 releases

v1.2.0

  • Radio Group horizontal arrow keys follow visible positions in RTL.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.1.0

  • In Astro, an attribute that you set on a Radio Group part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • In Svelte, a defined `value` that you pass when RadioGroup mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, RadioGroup keeps its own state.

v1.0.2

  • When a required Radio Group is the first field with an error in a Starwind Form, submit and `validate({ focus: true })` now put focus on the checked radio, or on the first radio, and the page scrolls to it.

v1.0.1

  • Reduced the JavaScript loaded by Radio Group when it is used without Field.
  • Reduced repeated JavaScript when Astro Radio Group is used with other Starwind components.
  • Reduced JavaScript used by Vue Radio Group. Props and defaults stay the same.

v1.0.0

  • This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.

v0.1.2

  • The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
  • In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.

v0.1.1

  • In the React Primitive files that the CLI copies, Radio Group now shows the correct value after a change applies and after your app changes the value.

v0.1.0

  • First release. Radio Group holds its radios and moves focus between them with the arrow keys. It fires value changes and sends its value with the form.