RadioGroup
Radio groups allow users to select a single option from a list of mutually exclusive options. All possible options are exposed up front for users to compare.
Value
Use the value or defaultValue prop to set the selected item, and onChange to handle selection changes.
Current selection: None
import {RadioGroup, Radio} from '@react-spectrum/s2';
import {useState} from 'react';
function Example() {
let [selected, setSelected] = useState(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.
API
<RadioGroup>
<Radio />
</RadioGroup>
RadioGroup
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
| The Radios contained within the RadioGroup. | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
| The size of the RadioGroup. | ||
orientation | Orientation | Default: 'vertical'
|
| The axis the radio elements should align with. | ||
isEmphasized | boolean | Default: — |
| Whether the RadioGroup should be displayed with an emphasized style. | ||
isDisabled | boolean | Default: — |
| Whether the input is disabled. | ||
isReadOnly | boolean | Default: — |
| Whether the input can be selected but not changed by the user. | ||
styles | | Default: — |
Spectrum-defined styles, returned by the style() macro. | ||
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. | ||
Radio
| Name | Type | |
|---|---|---|
children | ReactNode | |
| The label for the element. | ||
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. | ||
styles | | |
Spectrum-defined styles, returned by the style() macro. | ||
Testing
Test utils
@react-spectrum/test-utils offers common radio group interaction utilities which you may find helpful when writing tests. To install, simply
add it to your dev dependencies via your preferred package manager.
yarn add @react-spectrum/test-utils --dev
Once installed, you can access the User that @react-spectrum/test-utils provides in your test file as shown below. This user only needs to be initialized once and then can be used to generate
the RadioGroup tester in your test cases. This gives you access to RadioGroup specific utilities that you can then call within your test to query for specific subcomponents or simulate common interactions.
The example test case below shows how you might go about setting up the RadioGroup tester, use it to changing radio selection, and verify the radio group's state after each interaction.
// RadioGroup.test.ts
import {render} from '@testing-library/react';
import {User} from '@react-spectrum/test-utils';
let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanceTimersByTime});
// ...
it('RadioGroup can switch the selected radio', async function () {
// Render your test component/app and initialize the radiogroup tester
let {getByRole} = render(
<RadioGroup>
...
</RadioGroup>
);
let radioGroupTester = testUtilUser.createTester('RadioGroup', {root: getByRole('radiogroup')});
let radios = radioGroupTester.radios;
expect(radioGroupTester.selectedRadio).toBeFalsy();
await radioGroupTester.triggerRadio({radio: radios[0]});
expect(radioGroupTester.selectedRadio).toBe(radios[0]);
await radioGroupTester.triggerRadio({radio: radios[1]});
expect(radioGroupTester.selectedRadio).toBe(radios[1]);
});
See below for the full definition of the User and the RadioGroup tester.
Properties
| Name | Type | Default |
|---|---|---|
advanceTimer | UserOpts['advanceTimer'] | Default: — |
| A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table). | ||
interactionType | UserOpts['interactionType'] | Default: mouse
|
| The interaction type (mouse, touch, keyboard) that the test util user will use when interacting with a component. This can be overridden at the aria pattern util level if needed. | ||
Methods
constructor | ||
createTester | ||
| Creates an aria pattern tester, inheriting the options provided to the original user. | ||
Properties
| Name | Type | |
|---|---|---|
selectedRadio | HTMLElement | null | |
| Returns the currently selected radio in the radiogroup if any. | ||
radios | HTMLElement | |
| Returns the radios. | ||
radiogroup | HTMLElement | |
| Returns the radiogroup. | ||
Methods
constructor | ||
setInteractionType | ||
| Set the interaction type used by the radio tester. | ||
findRadio | ||
| Returns a radio matching the specified index or text content. | ||
triggerRadio | ||
| Triggers the specified radio. Defaults to using the interaction type set on the radio tester. | ||