Radio Group
A set of options that lets users select exactly one.
A radio group lets users select exactly one option from a set. Selecting an item clears the selection before it, so the group never holds more than one value. Each item has a label that names its option, and a click on the label selects the item just as a click on the item does.
Usage
Examples
Default Value
Many choices have an option that fits most cases, such as the standard payment terms of a supplier. Through defaultValue, the radio group starts with that option selected, and users change it only when their case differs.
Controlled
Sometimes the selection depends on more than a click, for example a choice restored from saved settings or one that decides which fields a form shows next. With value and onValueChange, the application holds the selection itself, so the radio group reflects choices made elsewhere and the rest of the interface can respond to every change.
Disabled
A decision can become fixed for a while, for example once an order has been released to a supplier. Set on the group, disabled keeps every option in view, prevents users from selecting any of them and exposes this state to assistive technology. Its value is then left out when the form is submitted.
Disabled Item
Within an otherwise open choice, one option can be unavailable, such as a return to the supplier for perishable goods. Marked as disabled, that item can no longer be selected and exposes its state to assistive technology, while users choose freely among the remaining options.
Invalid
Some forms cannot be completed without a choice, and users need to see at once where that choice is still missing. When validation fails, aria-invalid on each item marks the choice as invalid, on screen as well as for assistive technology.
API Reference
RadioGroup
| Prop | value |
|---|---|
| Type | any |
| Default | – |
| Description | Sets the value of the selected item and makes the radio group controlled. Update it in onValueChange. |
| Prop | defaultValue |
|---|---|
| Type | any |
| Default | – |
| Description | Sets the value of the item that is selected at first render. The radio group then tracks the selection itself. |
| Prop | onValueChange |
|---|---|
| Type | (value: any, eventDetails: RadioGroupChangeEventDetails) => void |
| Default | – |
| Description | Runs when users select an item. It receives the value of that item. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from selecting any item and exposes the disabled state to assistive technology. A disabled radio group submits no value with the form. |
| Prop | readOnly |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from selecting a different item. Unlike a disabled radio group, it submits its value with the form. |
| Prop | required |
|---|---|
| Type | boolean |
| Default | false |
| Description | Requires users to select an item before they can submit the form. |
| Prop | name |
|---|---|
| Type | string |
| Default | – |
| Description | Identifies the radio group when the form is submitted. |
| Prop | form |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the form that the radio group belongs to. Set it when the radio group is rendered outside of that form. |
| Prop | inputRef |
|---|---|
| Type | Ref<HTMLInputElement> |
| Default | – |
| Description | Receives the hidden input element of the selected item. While no item is selected, it receives the hidden input of the first item that is not disabled. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the radio group with another component. |
RadioGroupItem
| Prop | value |
|---|---|
| Type | any |
| Default | – |
| Description | Identifies the item in value, defaultValue and onValueChange of the radio group. The selected item submits it with the form. Every item needs one. |
| Prop | id |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the hidden input, so that a label can refer to the item with htmlFor. With nativeButton, the ID goes to the rendered button instead. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from selecting the item and exposes the disabled state to assistive technology. A disabled radio group disables the item as well. |
| Prop | readOnly |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from selecting the item. A read-only radio group makes the item read-only as well. |
| Prop | required |
|---|---|
| Type | boolean |
| Default | false |
| Description | Marks the hidden input of the item as required for the validation of the form. A required radio group makes the item required as well. |
| Prop | inputRef |
|---|---|
| Type | Ref<HTMLInputElement> |
| Default | – |
| Description | Receives the hidden input element that carries the value of the item in a form. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the item with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | false |
| Description | Declares whether the rendered element is a native button. Set it to true when render returns a button. |