useRadioGroup

Provides the behavior and accessibility implementation for a radio group component. Radio groups allow users to select a single item from a list of mutually exclusive options.

Favorite pet
Example
RadioGroup.tsx
RadioGroup.css
import {RadioGroup, Radio} from './RadioGroup';

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

API

Shows a radio group component with labels pointing to its parts, including the radio group label, and radio group element containing three radios with input and label elements.CatDogDragonFavorite PetInputRadio group labelRadio groupRadio label
<RadioGroup>
  <Label />
  <RadioField>
    <RadioButton>
      <SelectionIndicator />
    </RadioButton>
    <Text slot="description" />
  </RadioField>
  <Text slot="description" />
  <FieldError />
</RadioGroup>
useRadioGroupState(props: ): useRadioGroup(props: , state: ): useRadio( props: , state: , ref: <HTMLInputElementnull> ):

RadioGroupState

Properties

NameType
isDisabledboolean

Whether the radio group is disabled.

isReadOnlyboolean

Whether the radio group is read only.

selectedValuestringnull

The currently selected value.

defaultSelectedValuestringnull

The default selected value.

lastFocusedValuestringnull

The value of the last focused radio.

realtimeValidation

Realtime validation results, updated as the user edits the value.

displayValidation

Currently displayed validation results, updated when the user commits their changes.

Methods

setSelectedValue(value: stringnull): void
Sets the selected value.
setLastFocusedValue(value: stringnull): void
Sets the last focused value.
updateValidation(result: ): void
Updates the current validation result. Not displayed to the user until commitValidation is called.
resetValidation(): void
Resets the displayed validation state to valid when the user resets the form.
commitValidation(): void
Commits the realtime validation so it is displayed to the user.

AriaRadioGroupProps

NameTypeDefault
orientationDefault: 'vertical'

The axis the Radio Button(s) should align with.

isDisabledbooleanDefault:

Whether the input is disabled.

isReadOnlybooleanDefault:

Whether the input can be selected but not changed by the user.

valuestringnullDefault:

The current value (controlled).

defaultValuestringnullDefault:

The default value (uncontrolled).

onChange(value: string) => voidDefault:

Handler that is called when the value changes.

RadioGroupAria

NameType
radioGroupPropsDOMAttributes

Props for the radio group wrapper element.

labelPropsDOMAttributes

Props for the radio group's visible label (if any).

descriptionPropsDOMAttributes

Props for the radio group description element, if any.

errorMessagePropsDOMAttributes

Props for the radio group error message element, if any.

validationDetailsValidityState

The native validation details for the input.

AriaRadioProps

NameType
childrenReactNode

The label for the Radio. Accepts any renderable node.

isDisabledboolean

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

RadioAria

NameType
labelPropsLabelHTMLAttributes<HTMLLabelElement>

Props for the label wrapper element.

inputPropsInputHTMLAttributes<HTMLInputElement>

Props for the input element.

descriptionProps<HTMLElement>

Props for the checkbox description element, if any.

isDisabledboolean

Whether the radio is disabled.

isSelectedboolean

Whether the radio is currently selected.

isPressedboolean

Whether the radio is in a pressed state.