Radio Group
Radio Groups contain a set of checkable buttons where only one of the buttons can be checked at a time.
Introduction
Installation
Base UI components are all available as a single package.
npm install @base-ui-components/react
Once you have the package installed, import the component.
import { RadioGroup } from '@base-ui-components/react/RadioGroup'; import { Radio } from '@base-ui-components/react/Radio';
Anatomy
Radio Group is composed of a Root
and Radio
components:
<RadioGroup.Root />
is a top-level element that wraps the other components.<Radio.Root />
renders an individual<button>
radio item.<Radio.Indicator />
renders a<span>
for providing a visual indicator. You can style this itself and/or place an icon inside.
<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
Prop | Type | Default | Description |
---|---|---|---|
className | union | Class names applied to the element or a function that returns them based on the component's state. | |
defaultValue | any | The default value of the selected radio button. Use when uncontrolled. | |
disabled | bool | false | Determines if the radio group is disabled. |
name | string | The name of the radio group submitted with the form data. | |
onValueChange | func | Callback fired when the value changes. | |
readOnly | bool | false | Determines if the radio group is readonly. |
render | union | A function to customize rendering of the component. | |
required | bool | false | Determines if the radio group is required. |
value | any | The value of the selected radio button. Use when controlled. |
RadioRoot
Prop | Type | Default | Description |
---|---|---|---|
value | any | The unique identifying value of the radio in a group. | |
className | union | Class names applied to the element or a function that returns them based on the component's state. | |
disabled | bool | false | Determines if the radio is disabled. |
readOnly | bool | false | Determines if the radio is readonly. |
render | union | A function to customize rendering of the component. | |
required | bool | false | Determines if the radio is required. |
RadioIndicator
Prop | Type | Default | Description |
---|---|---|---|
className | union | Class names applied to the element or a function that returns them based on the component's state. | |
keepMounted | bool | true | Whether the component should be kept mounted when not checked. |
render | union | A function to customize rendering of the component. |