alpha

RadioGroup

A radio group allows a user to select a single item from a list of mutually exclusive options.

installyarn add react-aria-components
version1.0.0-alpha.0
usageimport {RadioGroup} from 'react-aria-components'

Example#


import {RadioGroup, Radio, Label} from 'react-aria-components';

<RadioGroup>
  <Label>Favorite pet</Label>
  <Radio value="dogs">Dog</Radio>
  <Radio value="cats">Cat</Radio>
  <Radio value="dragon">Dragon</Radio>
</RadioGroup>
import {
  Label,
  Radio,
  RadioGroup
} from 'react-aria-components';

<RadioGroup>
  <Label>Favorite pet</Label>
  <Radio value="dogs">Dog</Radio>
  <Radio value="cats">Cat</Radio>
  <Radio value="dragon">Dragon</Radio>
</RadioGroup>
import {
  Label,
  Radio,
  RadioGroup
} from 'react-aria-components';

<RadioGroup>
  <Label>
    Favorite pet
  </Label>
  <Radio value="dogs">
    Dog
  </Radio>
  <Radio value="cats">
    Cat
  </Radio>
  <Radio value="dragon">
    Dragon
  </Radio>
</RadioGroup>
Show CSS
.react-aria-RadioGroup {
  display: flex;
  flex-direction: column;
  gap: 8px;

  &[aria-orientation=horizontal] {
    flex-direction: row;
    align-items: center;
  }

  & [slot=description] {
    font-size: 12px;
  }

  & [slot=errorMessage] {
    font-size: 12px;
    color: var(--spectrum-global-color-red-600);
  }
}

.react-aria-Radio {
  --label-color: var(--spectrum-alias-text-color);
  --deselected-color: gray;
  --deselected-color-pressed: dimgray;
  --background-color: var(--spectrum-global-color-gray-50);
  --selected-color: slateblue;
  --selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
  --invalid-color: var(--spectrum-global-color-static-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-static-red-700);

  display: flex;
  align-items: center;
  gap: 0.571rem;
  font-size: 1.143rem;
  color: var(--label-color);

  &:before {
    content: '';
    display: block;
    width: 1.286rem;
    height: 1.286rem;
    box-sizing: border-box;
    border: 0.143rem solid var(--deselected-color);
    background: var(--background-color);
    border-radius: 1.286rem;
    transition: all 200ms;
  }

  &[data-pressed]:before {
    border-color: var(--deselected-color-pressed);
  }

  &[data-selected] {
    &:before {
      border-color: var(--selected-color);
      border-width: 0.429rem;
    }

    &[data-pressed]:before {
      border-color: var(--selected-color-pressed);
    }
  }

  &[data-validation-state=invalid] {
    &:before {
      border-color: var(--invalid-color);
    }

    &[data-pressed]:before {
      border-color: var(--invalid-color-pressed);
    }
  }

  &[data-focus-visible]:before {
    box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
  }

  &[data-disabled] {
    opacity: 0.4;
  }
}

@media (forced-colors: active) {
  .react-aria-Radio {
    forced-color-adjust: none;

    --label-color: ButtonText;
    --deselected-color: ButtonBorder;
    --deselected-color-pressed: ButtonBorder;
    --selected-color: Highlight;
    --selected-color-pressed: Highlight;
    --background-color: HighlightText;
    --invalid-color: LinkText;
    --invalid-color-pressed: LinkText;
    --spectrum-alias-background-color-default: Canvas;

    &[data-disabled] {
      opacity: 1;
      --deselected-color: GrayText;
      --selected-color: GrayText;
      --label-color: GrayText;
    }
  }
}
.react-aria-RadioGroup {
  display: flex;
  flex-direction: column;
  gap: 8px;

  &[aria-orientation=horizontal] {
    flex-direction: row;
    align-items: center;
  }

  & [slot=description] {
    font-size: 12px;
  }

  & [slot=errorMessage] {
    font-size: 12px;
    color: var(--spectrum-global-color-red-600);
  }
}

.react-aria-Radio {
  --label-color: var(--spectrum-alias-text-color);
  --deselected-color: gray;
  --deselected-color-pressed: dimgray;
  --background-color: var(--spectrum-global-color-gray-50);
  --selected-color: slateblue;
  --selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
  --invalid-color: var(--spectrum-global-color-static-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-static-red-700);

  display: flex;
  align-items: center;
  gap: 0.571rem;
  font-size: 1.143rem;
  color: var(--label-color);

  &:before {
    content: '';
    display: block;
    width: 1.286rem;
    height: 1.286rem;
    box-sizing: border-box;
    border: 0.143rem solid var(--deselected-color);
    background: var(--background-color);
    border-radius: 1.286rem;
    transition: all 200ms;
  }

  &[data-pressed]:before {
    border-color: var(--deselected-color-pressed);
  }

  &[data-selected] {
    &:before {
      border-color: var(--selected-color);
      border-width: 0.429rem;
    }

    &[data-pressed]:before {
      border-color: var(--selected-color-pressed);
    }
  }

  &[data-validation-state=invalid] {
    &:before {
      border-color: var(--invalid-color);
    }

    &[data-pressed]:before {
      border-color: var(--invalid-color-pressed);
    }
  }

  &[data-focus-visible]:before {
    box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
  }

  &[data-disabled] {
    opacity: 0.4;
  }
}

@media (forced-colors: active) {
  .react-aria-Radio {
    forced-color-adjust: none;

    --label-color: ButtonText;
    --deselected-color: ButtonBorder;
    --deselected-color-pressed: ButtonBorder;
    --selected-color: Highlight;
    --selected-color-pressed: Highlight;
    --background-color: HighlightText;
    --invalid-color: LinkText;
    --invalid-color-pressed: LinkText;
    --spectrum-alias-background-color-default: Canvas;

    &[data-disabled] {
      opacity: 1;
      --deselected-color: GrayText;
      --selected-color: GrayText;
      --label-color: GrayText;
    }
  }
}
.react-aria-RadioGroup {
  display: flex;
  flex-direction: column;
  gap: 8px;

  &[aria-orientation=horizontal] {
    flex-direction: row;
    align-items: center;
  }

  & [slot=description] {
    font-size: 12px;
  }

  & [slot=errorMessage] {
    font-size: 12px;
    color: var(--spectrum-global-color-red-600);
  }
}

.react-aria-Radio {
  --label-color: var(--spectrum-alias-text-color);
  --deselected-color: gray;
  --deselected-color-pressed: dimgray;
  --background-color: var(--spectrum-global-color-gray-50);
  --selected-color: slateblue;
  --selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
  --invalid-color: var(--spectrum-global-color-static-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-static-red-700);

  display: flex;
  align-items: center;
  gap: 0.571rem;
  font-size: 1.143rem;
  color: var(--label-color);

  &:before {
    content: '';
    display: block;
    width: 1.286rem;
    height: 1.286rem;
    box-sizing: border-box;
    border: 0.143rem solid var(--deselected-color);
    background: var(--background-color);
    border-radius: 1.286rem;
    transition: all 200ms;
  }

  &[data-pressed]:before {
    border-color: var(--deselected-color-pressed);
  }

  &[data-selected] {
    &:before {
      border-color: var(--selected-color);
      border-width: 0.429rem;
    }

    &[data-pressed]:before {
      border-color: var(--selected-color-pressed);
    }
  }

  &[data-validation-state=invalid] {
    &:before {
      border-color: var(--invalid-color);
    }

    &[data-pressed]:before {
      border-color: var(--invalid-color-pressed);
    }
  }

  &[data-focus-visible]:before {
    box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
  }

  &[data-disabled] {
    opacity: 0.4;
  }
}

@media (forced-colors: active) {
  .react-aria-Radio {
    forced-color-adjust: none;

    --label-color: ButtonText;
    --deselected-color: ButtonBorder;
    --deselected-color-pressed: ButtonBorder;
    --selected-color: Highlight;
    --selected-color-pressed: Highlight;
    --background-color: HighlightText;
    --invalid-color: LinkText;
    --invalid-color-pressed: LinkText;
    --spectrum-alias-background-color-default: Canvas;

    &[data-disabled] {
      opacity: 1;
      --deselected-color: GrayText;
      --selected-color: GrayText;
      --label-color: GrayText;
    }
  }
}

Features#


Radio groups can be built in HTML with the <fieldset> and <input> elements, however these can be difficult to style. RadioGroup and Radio help achieve accessible radio groups that can be styled as needed.

  • Accessible – Radio groups are exposed to assistive technology via ARIA, and automatically associate a nested <Label>. Description and error message help text slots are supported as well.
  • HTML form integration – Each individual radio uses a visually hidden <input> element under the hood, which enables HTML form integration and autofill.
  • Cross-browser – Mouse, touch, keyboard, and focus interactions are normalized to ensure consistency across browsers and devices.

Anatomy#


CatDogInputFavorite PetRadio group labelDragonRadio groupRadio label

A radio group consists of a set of radio buttons, and a label. Each radio includes a label and a visual selection indicator. A single radio button within the group can be selected at a time. Users may click or touch a radio button to select it, or use the Tab key to navigate to the group, the arrow keys to navigate within the group, and the Space key to select an option.

RadioGroup also supports optional description and error message elements, which can be used to provide more context about the field, and any validation messages. These are linked with the inputs via the aria-describedby attribute.

Individual radio buttons must have a visual label. If the radio group does not have a visible label, an aria-label or aria-labelledby prop must be passed instead to identify the element to assistive technology.

Composed components#

A RadioGroup uses the following components, which may also be used standalone or reused in other components.

Label
A label provides context for an input element.

Props#


RadioGroup#

NameTypeDefaultDescription
orientationOrientation'vertical'The axis the Radio Button(s) should align with.
namestring

The name of the RadioGroup, used when submitting an HTML form. See MDN.

valuestringThe current value (controlled).
defaultValuestringThe default value (uncontrolled).
isDisabledbooleanWhether the input is disabled.
isReadOnlybooleanWhether the input can be selected but not changed by the user.
validationStateValidationStateWhether the input should display its "valid" or "invalid" visual styling.
isRequiredboolean

Whether user input is required on the input before form submission. Often paired with the necessityIndicator prop to add a visual indicator to the input.

childrenReactNode( (values: RadioGroupRenderProps )) => ReactNodeThe children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: RadioGroupRenderProps )) => stringThe CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: RadioGroupRenderProps )) => CSSPropertiesThe inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDefaultDescription
onChange( (value: T )) => voidHandler that is called when the value changes.
Layout
NameTypeDefaultDescription
slotstringA slot name for the component. Slots allow the component to receive props from a parent component.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
aria-errormessagestringIdentifies the element that provides an error message for the object.

Radio#

NameTypeDefaultDescription
valuestring

The value of the radio button, used when submitting an HTML form. See MDN.

isDisabledboolean

Whether the radio button is disabled or not. Shows that a selection exists, but is not available in that circumstance.

autoFocusbooleanWhether the element should receive focus on render.
childrenReactNode( (values: RadioRenderProps )) => ReactNodeThe children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: RadioRenderProps )) => stringThe CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: RadioRenderProps )) => CSSPropertiesThe inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDefaultDescription
onFocus( (e: FocusEvent<Target> )) => voidHandler that is called when the element receives focus.
onBlur( (e: FocusEvent<Target> )) => voidHandler that is called when the element loses focus.
onFocusChange( (isFocused: boolean )) => voidHandler that is called when the element's focus status changes.
onKeyDown( (e: KeyboardEvent )) => voidHandler that is called when a key is pressed.
onKeyUp( (e: KeyboardEvent )) => voidHandler that is called when a key is released.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.

Styling#


React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName naming convention.

.react-aria-Radio {
  /* ... */
}
.react-aria-Radio {
  /* ... */
}
.react-aria-Radio {
  /* ... */
}

A custom className can also be specified on any component. This overrides the default className provided by React Aria with your own.

<Radio className="my-radio">
  {/* ... */}
</Radio>
<Radio className="my-radio">
  {/* ... */}
</Radio>
<Radio className="my-radio">
  {/* ... */}
</Radio>

In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using DOM attributes, which you can target in CSS selectors. These are ARIA attributes wherever possible, or data attributes when a relevant ARIA attribute does not exist. For example:

.react-aria-Radio[data-selected] {
  /* ... */
}
.react-aria-Radio[data-selected] {
  /* ... */
}
.react-aria-Radio[data-selected] {
  /* ... */
}

The className and style props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.

<Radio
  className={({ isPressed }) => isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Radio
  className={({ isPressed }) =>
    isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Radio
  className={(
    { isPressed }
  ) =>
    isPressed
      ? 'bg-gray-700'
      : 'bg-gray-600'}
/>

Render props may also be used as children to alter what elements are rendered based on the current state. For example, you could render an extra element when the radio is selected.

<Radio>
  {({isSelected}) => (
    <>
      {isSelected && <SelectedIcon />}
      Option
    </>
  )}
</Radio>
<Radio>
  {({isSelected}) => (
    <>
      {isSelected && <SelectedIcon />}
      Option
    </>
  )}
</Radio>
<Radio>
  {(
    { isSelected }
  ) => (
    <>
      {isSelected && (
        <SelectedIcon />
      )}
      Option
    </>
  )}
</Radio>

The states and selectors for each component used in a RadioGroup are documented below.

RadioGroup#

A RadioGroup can be targeted with the .react-aria-RadioGroup CSS selector, or by overriding with a custom className. It supports the following states and render props:

NameCSS SelectorDescription
orientation[aria-orientation="horizontal | vertical"]The orientation of the radio group.
isDisabled[aria-disabled]Whether the radio group is disabled.
isReadOnly[aria-readonly]Whether the radio group is read only.
isRequired[aria-required]Whether the radio group is required.
validationState[aria-invalid]The validation state of the radio group.

Radio#

A Radio can be targeted with the .react-aria-Radio CSS selector, or by overriding with a custom className. It supports the following states:

NameCSS SelectorDescription
isSelected[data-selected]Whether the radio is selected.
isHovered[data-hovered]Whether the radio is currently hovered with a mouse.
isPressed[data-pressed]Whether the radio is currently in a pressed state.
isFocused[data-focused]Whether the radio is focused, either via a mouse or keyboard.
isFocusVisible[data-focus-visible]Whether the radio is keyboard focused.
isDisabled[data-disabled]Whether the radio is disabled.
isReadOnly[data-readonly]Whether the radio is read only.
validationState[data-validation-state="valid | invalid"]Whether the radio is valid or invalid.
isRequired[data-required]Whether the checkbox is required.

Text#

The help text elements within a RadioGroup can be targeted with the [slot=description] and [slot=errorMessage] CSS selectors, or by adding a custom className.

Reusable wrappers#


If you will use a RadioGroup in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.

This example wraps RadioGroup and all of its children together into a single component which accepts a label prop, which is passed to the right place. It also shows how to use the description and errorMessage slots to render help text (see below for details).

import type {RadioGroupProps} from 'react-aria-components';
import {Text} from 'react-aria-components';

interface MyRadioGroupProps extends RadioGroupProps {
  label?: string,
  description?: string,
  errorMessage?: string
}

function MyRadioGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyRadioGroupProps) {
  return (
    <RadioGroup {...props}>
      {label}
      {children}
      {description && <Text slot="description">{description}</Text>}
      {errorMessage && <Text slot="errorMessage">{errorMessage}</Text>}
    </RadioGroup>
  );
}

<MyRadioGroup label="Favorite sport">
  <Radio value="soccer">Soccer</Radio>
  <Radio value="baseball">Baseball</Radio>
  <Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
import type {RadioGroupProps} from 'react-aria-components';
import {Text} from 'react-aria-components';

interface MyRadioGroupProps extends RadioGroupProps {
  label?: string;
  description?: string;
  errorMessage?: string;
}

function MyRadioGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyRadioGroupProps) {
  return (
    <RadioGroup {...props}>
      {label}
      {children}
      {description && (
        <Text slot="description">{description}</Text>
      )}
      {errorMessage && (
        <Text slot="errorMessage">{errorMessage}</Text>
      )}
    </RadioGroup>
  );
}

<MyRadioGroup label="Favorite sport">
  <Radio value="soccer">Soccer</Radio>
  <Radio value="baseball">Baseball</Radio>
  <Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
import type {RadioGroupProps} from 'react-aria-components';
import {Text} from 'react-aria-components';

interface MyRadioGroupProps
  extends
    RadioGroupProps {
  label?: string;
  description?: string;
  errorMessage?: string;
}

function MyRadioGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyRadioGroupProps) {
  return (
    <RadioGroup
      {...props}
    >
      {label}
      {children}
      {description && (
        <Text slot="description">
          {description}
        </Text>
      )}
      {errorMessage && (
        <Text slot="errorMessage">
          {errorMessage}
        </Text>
      )}
    </RadioGroup>
  );
}

<MyRadioGroup label="Favorite sport">
  <Radio value="soccer">
    Soccer
  </Radio>
  <Radio value="baseball">
    Baseball
  </Radio>
  <Radio value="basketball">
    Basketball
  </Radio>
</MyRadioGroup>

Usage#


The following examples show how to use the MyRadioGroup component created in the above example.

Orientation#

RadioGroups are vertically oriented by default. The orientation prop can be used to change the orientation to horizontal.

<MyRadioGroup label="Favorite avatar" orientation="horizontal">
  <Radio value="wizard">Wizard</Radio>
  <Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite avatar"
  orientation="horizontal"
>
  <Radio value="wizard">Wizard</Radio>
  <Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite avatar"
  orientation="horizontal"
>
  <Radio value="wizard">
    Wizard
  </Radio>
  <Radio value="dragon">
    Dragon
  </Radio>
</MyRadioGroup>

Default value#

An initial, uncontrolled value can be provided to the RadioGroup using the defaultValue prop, which accepts a value corresponding with the value prop of each Radio.

<MyRadioGroup label="Are you a wizard?" defaultValue="yes">
  <Radio value="yes">Yes</Radio>
  <Radio value="no">No</Radio>
</MyRadioGroup>
<MyRadioGroup label="Are you a wizard?" defaultValue="yes">
  <Radio value="yes">Yes</Radio>
  <Radio value="no">No</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Are you a wizard?"
  defaultValue="yes"
>
  <Radio value="yes">
    Yes
  </Radio>
  <Radio value="no">
    No
  </Radio>
</MyRadioGroup>

Controlled value#

A controlled value can be provided using the value prop, which accepts a value corresponding with the value prop of each Radio. The onChange event is fired when the user selects a radio.

function Example() {
  let [selected, setSelected] = React.useState('');

  return (
    <>
      <MyRadioGroup
        label="Favorite avatar"
        value={selected}
        onChange={setSelected}
      >
        <Radio value="wizard">Wizard</Radio>
        <Radio value="dragon">Dragon</Radio>
      </MyRadioGroup>
      <p>You have selected: {selected}</p>
    </>
  );
}
function Example() {
  let [selected, setSelected] = React.useState('');

  return (
    <>
      <MyRadioGroup
        label="Favorite avatar"
        value={selected}
        onChange={setSelected}
      >
        <Radio value="wizard">Wizard</Radio>
        <Radio value="dragon">Dragon</Radio>
      </MyRadioGroup>
      <p>You have selected: {selected}</p>
    </>
  );
}
function Example() {
  let [
    selected,
    setSelected
  ] = React.useState('');

  return (
    <>
      <MyRadioGroup
        label="Favorite avatar"
        value={selected}
        onChange={setSelected}
      >
        <Radio value="wizard">
          Wizard
        </Radio>
        <Radio value="dragon">
          Dragon
        </Radio>
      </MyRadioGroup>
      <p>
        You have
        selected:{' '}
        {selected}
      </p>
    </>
  );
}

Validation#

RadioGroups can display a validation state to communicate to the user if the current value is invalid. Implement your own validation logic in your app and pass "invalid" to the RadioGroup via the validationState prop.

<MyRadioGroup label="Favorite avatar" validationState="invalid">
  <Radio value="wizard">Wizard</Radio>
  <Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite avatar"
  validationState="invalid"
>
  <Radio value="wizard">Wizard</Radio>
  <Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite avatar"
  validationState="invalid"
>
  <Radio value="wizard">
    Wizard
  </Radio>
  <Radio value="dragon">
    Dragon
  </Radio>
</MyRadioGroup>

Help text#

The description slot can be used to associate additional help text with a radio group. Additionally, the errorMessage slot can be used to help the user fix a validation error. It should be combined with the validationState prop to semantically mark the radio group as invalid for assistive technologies.

function Example() {
  let [selected, setSelected] = React.useState('dogs');
  let isValid = selected === 'dogs';

  return (
    <MyRadioGroup
      aria-label="Favorite pet"
      onChange={setSelected}
      validationState={isValid ? 'valid' : 'invalid'}
      description={isValid ? 'Please select a pet.' : undefined}
      errorMessage={isValid ? undefined :
        selected === 'cats'
          ? 'No cats allowed.'
          : 'Please select dogs.'
      }>
      <Radio value="dogs">
        Dogs
      </Radio>
      <Radio value="cats">
        Cats
      </Radio>
      <Radio value="dragons">
        Dragons
      </Radio>
    </MyRadioGroup>
  );
}
function Example() {
  let [selected, setSelected] = React.useState('dogs');
  let isValid = selected === 'dogs';

  return (
    <MyRadioGroup
      aria-label="Favorite pet"
      onChange={setSelected}
      validationState={isValid ? 'valid' : 'invalid'}
      description={isValid
        ? 'Please select a pet.'
        : undefined}
      errorMessage={isValid
        ? undefined
        : selected === 'cats'
        ? 'No cats allowed.'
        : 'Please select dogs.'}
    >
      <Radio value="dogs">
        Dogs
      </Radio>
      <Radio value="cats">
        Cats
      </Radio>
      <Radio value="dragons">
        Dragons
      </Radio>
    </MyRadioGroup>
  );
}
function Example() {
  let [
    selected,
    setSelected
  ] = React.useState(
    'dogs'
  );
  let isValid =
    selected === 'dogs';

  return (
    <MyRadioGroup
      aria-label="Favorite pet"
      onChange={setSelected}
      validationState={isValid
        ? 'valid'
        : 'invalid'}
      description={isValid
        ? 'Please select a pet.'
        : undefined}
      errorMessage={isValid
        ? undefined
        : selected ===
            'cats'
        ? 'No cats allowed.'
        : 'Please select dogs.'}
    >
      <Radio value="dogs">
        Dogs
      </Radio>
      <Radio value="cats">
        Cats
      </Radio>
      <Radio value="dragons">
        Dragons
      </Radio>
    </MyRadioGroup>
  );
}

Disabled#

The entire RadioGroup can be disabled with the isDisabled prop.

<MyRadioGroup label="Favorite sport" isDisabled>
  <Radio value="soccer">Soccer</Radio>
  <Radio value="baseball">Baseball</Radio>
  <Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite sport" isDisabled>
  <Radio value="soccer">Soccer</Radio>
  <Radio value="baseball">Baseball</Radio>
  <Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite sport"
  isDisabled
>
  <Radio value="soccer">
    Soccer
  </Radio>
  <Radio value="baseball">
    Baseball
  </Radio>
  <Radio value="basketball">
    Basketball
  </Radio>
</MyRadioGroup>

To disable an individual radio, pass isDisabled to the Radio instead.

<MyRadioGroup label="Favorite sport">
  <Radio value="soccer">Soccer</Radio>
  <Radio value="baseball" isDisabled>Baseball</Radio>
  <Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite sport">
  <Radio value="soccer">Soccer</Radio>
  <Radio value="baseball" isDisabled>Baseball</Radio>
  <Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite sport">
  <Radio value="soccer">
    Soccer
  </Radio>
  <Radio
    value="baseball"
    isDisabled
  >
    Baseball
  </Radio>
  <Radio value="basketball">
    Basketball
  </Radio>
</MyRadioGroup>

Read only#

The isReadOnly prop makes the selection immutable. Unlike isDisabled, the RadioGroup remains focusable. See the MDN docs for more information.

<MyRadioGroup label="Favorite avatar" defaultValue="wizard" isReadOnly>
  <Radio value="wizard">Wizard</Radio>
  <Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite avatar"
  defaultValue="wizard"
  isReadOnly
>
  <Radio value="wizard">Wizard</Radio>
  <Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite avatar"
  defaultValue="wizard"
  isReadOnly
>
  <Radio value="wizard">
    Wizard
  </Radio>
  <Radio value="dragon">
    Dragon
  </Radio>
</MyRadioGroup>

HTML forms#

RadioGroup supports the name prop, paired with the Radio value prop, for integration with HTML forms.

<MyRadioGroup label="Favorite pet" name="pet">
  <Radio value="dogs">Dogs</Radio>
  <Radio value="cats">Cats</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite pet" name="pet">
  <Radio value="dogs">Dogs</Radio>
  <Radio value="cats">Cats</Radio>
</MyRadioGroup>
<MyRadioGroup
  label="Favorite pet"
  name="pet"
>
  <Radio value="dogs">
    Dogs
  </Radio>
  <Radio value="cats">
    Cats
  </Radio>
</MyRadioGroup>

Advanced customization#


Hooks#

If you need to customize things further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useRadioGroup for more details.