Radio Group

Radio Groups contain a set of checkable buttons where only one of the buttons can be checked at a time.

Give FeedbackWAI-ARIABundle Size

Introduction

index.tsx

Installation

Base UI components are all available as a single package.

npm install @base_ui/react

Once you have the package installed, import the component.

import { RadioGroup } from '@base_ui/react/RadioGroup';
import { Radio } from '@base_ui/react/Radio';

Anatomy

Radio Group is composed of a Root and Radio components:

<RadioGroup.Root>
  <Radio.Root>
    <Radio.Indicator />
  </Radio.Root>
</RadioGroup.Root>

Identifying items

The value prop is required on Radio.Root to identify it in the Radio Group:

<RadioGroup.Root>
  <Radio.Root value="a">
    <Radio.Indicator />
  </Radio.Root>
  <Radio.Root value="b">
    <Radio.Indicator />
  </Radio.Root>
</RadioGroup.Root>

Default value

The defaultValue prop determines the initial value of the component when uncontrolled, linked to the value prop on an individual Radio item:

<RadioGroup.Root defaultValue="a">
  <Radio.Root value="a" />
  <Radio.Root value="b" />
</RadioGroup.Root>

Controlled

The value and onValueChange props contain the value string of the currently selected Radio item in the Radio Group:

const [value, setValue] = React.useState('a');

return (
  <RadioGroup.Root value={value} onValueChange={setValue}>
    <Radio.Root value="a" />
    <Radio.Root value="b" />
  </RadioGroup.Root>
);

Styling

The Radio components have a [data-radio] attribute with values "checked" or "unchecked" to style based on the checked state:

<Radio.Root className="Radio">
  <Radio.Indicator className="RadioIndicator" />
</Radio.Root>
.Radio {
  border: 1px solid black;
}

.RadioIndicator {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid black;
}

.Radio[data-radio='checked'] {
  background: black;
  color: white;
}

.RadioIndicator[data-radio='checked'] {
  background: white;
}

API Reference

RadioGroupRoot

PropTypeDefaultDescription
classNameunionClass names applied to the element or a function that returns them based on the component's state.
defaultValueanyThe default value of the selected radio button. Use when uncontrolled.
disabledboolfalseDetermines if the radio group is disabled.
namestringThe name of the radio group submitted with the form data.
onValueChangefuncCallback fired when the value changes.
readOnlyboolfalseDetermines if the radio group is readonly.
renderunionA function to customize rendering of the component.
requiredboolfalseDetermines if the radio group is required.
valueanyThe value of the selected radio button. Use when controlled.

RadioRoot

PropTypeDefaultDescription
valueanyThe unique identifying value of the radio in a group.
classNameunionClass names applied to the element or a function that returns them based on the component's state.
disabledboolfalseDetermines if the radio is disabled.
readOnlyboolfalseDetermines if the radio is readonly.
renderunionA function to customize rendering of the component.
requiredboolfalseDetermines if the radio is required.

RadioIndicator

PropTypeDefaultDescription
classNameunionClass names applied to the element or a function that returns them based on the component's state.
keepMountedbooltrueWhether the component should be kept mounted when not checked.
renderunionA function to customize rendering of the component.

Contents