A radio group allows a user to select a single item from a list of mutually exclusive options.
Vanilla CSS theme
This sets the
--tint CSS variable used by the Vanilla CSS examples.Theme
Favorite pet
orientation
isDisabled
Value
Use the value or defaultValue prop to set the selected item, and onChange to handle selection changes.
Favorite sport
Current selection: None
import {RadioGroup, Radio} from './RadioGroup';
import {useState} from 'react';
function Example() {
let [selected, setSelected] = useState<string | null>(null);
return (
<>
<RadioGroup
label="Favorite sport"
value={selected}
onChange={setSelected}>
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball">Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</RadioGroup>
<p>Current selection: {selected || 'None'}</p>
</>
);
}
Forms
Use the name prop to submit the selected radio to the server. Set the isRequired prop to validate that the user selects an option, or implement custom client or server-side validation. See the Forms guide to learn more.
import {RadioGroup, Radio} from './RadioGroup';
import {Button} from './Button';
import {Form} from './Form';;
<Form>
<RadioGroup
label="Favorite pet"
name="pet"
isRequired>
<Radio value="dog">Dog</Radio>
<Radio value="cat">Cat</Radio>
<Radio value="dragon">Dragon</Radio>
</RadioGroup>
<Button type="submit" style={{marginTop: 8}}>Submit</Button>
</Form>
API
<RadioGroup>
<Label />
<Radio>
<SelectionIndicator />
</Radio>
<Text slot="description" />
<FieldError />
</RadioGroup>
RadioGroup
| Name | Type | Default |
|---|---|---|
orientation | Orientation | Default: 'vertical'
|
| The axis the Radio Button(s) should align with. | ||
isDisabled | boolean | Default: — |
| Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
| Whether the input can be selected but not changed by the user. | ||
children | ChildrenOrFunction | Default: — |
| The children of the component. A function may be provided to alter the children based on component state. | ||
value | string | null | Default: — |
| The current value (controlled). | ||
defaultValue | string | null | Default: — |
| The default value (uncontrolled). | ||
onChange | | Default: — |
| Handler that is called when the value changes. | ||
Default className: react-aria-RadioGroup
| Render Prop | CSS Selector |
|---|---|
orientation | CSS Selector: [data-orientation="horizontal | vertical"]
|
| The orientation of the radio group. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the radio group is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the radio group is read only. | |
isRequired | CSS Selector: [data-required]
|
| Whether the radio group is required. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the radio group is invalid. | |
state | CSS Selector: — |
| State of the radio group. | |
Radio
| Name | Type | |
|---|---|---|
inputRef | RefObject | |
| A ref for the HTML input element. | ||
isDisabled | boolean | |
| Whether the radio button is disabled or not. Shows that a selection exists, but is not available in that circumstance. | ||
children | ChildrenOrFunction | |
| The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-Radio
| Render Prop | CSS Selector |
|---|---|
isSelected | CSS Selector: [data-selected]
|
| Whether the radio is selected. | |
isHovered | CSS Selector: [data-hovered]
|
| Whether the radio is currently hovered with a mouse. | |
isPressed | CSS Selector: [data-pressed]
|
| Whether the radio is currently in a pressed state. | |
isFocused | CSS Selector: [data-focused]
|
| Whether the radio is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether the radio is keyboard focused. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the radio is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the radio is read only. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the radio is invalid. | |
isRequired | CSS Selector: [data-required]
|
| Whether the checkbox is required. | |