Checkbox Group
Provides shared state to a series of checkboxes.
View as MarkdownUsage guidelines
- Form controls must have an accessible name: It can be created using
<label>elements, or theFieldandFieldsetcomponents. See Labeling a checkbox group and the forms guide.
Anatomy
Checkbox Group is composed together with Checkbox. Import the components and place them together:
import { Checkbox } from '@base-ui/react/checkbox';
import { CheckboxGroup } from '@base-ui/react/checkbox-group';
<CheckboxGroup>
<Checkbox.Root />
</CheckboxGroup>Examples
Labeling a checkbox group
A visible label for the group is created by specifying aria-labelledby on <CheckboxGroup> to reference the id of a sibling element that contains the group label text.
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">{/* ... */}</CheckboxGroup>For individual checkboxes, use an enclosing <label> element that wraps each <Checkbox.Root>. An enclosing label is announced to screen readers when focus is on the control, and ensures that clicking on any gaps between the label and checkbox still toggles it.
<label>
<Checkbox.Root value="http" />
HTTP
</label>The Field and Fieldset components eliminate the need to track id or aria-labelledby associations manually. They support both enclosing and sibling labeling patterns, along with a UX improvement to prevent double clicks from selecting the label text.
- Group label: Since the group contains multiple controls, it can be labeled as a fieldset. Use Fieldset by replacing the
<Fieldset.Root>element with<CheckboxGroup>via therenderprop.<Fieldset.Legend>provides the visible label for the group. - Individual label: Label an individual checkbox using an enclosing
<Field.Label>. Enclosing labels ensure that clicking on any gaps between the label and checkbox still toggles it.
<Field.Root>
<Fieldset.Root render={<CheckboxGroup />}>
<Fieldset.Legend>Allowed network protocols</Fieldset.Legend>
<Field.Label>
<Checkbox.Root value="http" />
HTTP
</Field.Label>
<Field.Label>
<Checkbox.Root value="https" />
HTTPS
</Field.Label>
<Field.Label>
<Checkbox.Root value="ssh" />
SSH
</Field.Label>
</Fieldset.Root>
</Field.Root>Form integration
To use a checkbox group in a form, pass the checkbox group’s name to <Field.Root>:
<Form>
<Field.Root name="allowedNetworkProtocols">
<Fieldset.Root render={<CheckboxGroup />}>
<Fieldset.Legend>Allowed network protocols</Fieldset.Legend>
<Field.Label>
<Checkbox.Root value="http" />
HTTP
</Field.Label>
<Field.Label>
<Checkbox.Root value="https" />
HTTPS
</Field.Label>
<Field.Label>
<Checkbox.Root value="ssh" />
SSH
</Field.Label>
</Fieldset.Root>
</Field.Root>
</Form>Parent checkbox
A checkbox that controls other checkboxes within a <CheckboxGroup> can be created:
- Make
<CheckboxGroup>a controlled component - Pass an array of all the child checkbox values to the
allValuesprop on the<CheckboxGroup>component - Add the
parentboolean prop to the parent<Checkbox>
Nested parent checkbox
API reference
defaultValuestring[]—
- Name
- Description
Names of the checkboxes in the group that should be initially ticked.
To render a controlled checkbox group, use the
valueprop instead.- Type
string[] | undefined
valuestring[]—
- Name
- Description
Names of the checkboxes in the group that should be ticked.
To render an uncontrolled checkbox group, use the
defaultValueprop instead.- Type
string[] | undefined
onValueChangefunction—
- Name
- Description
Event handler called when a checkbox in the group is ticked or unticked. Provides the new value as an argument.
- Type
| (( value: string[], eventDetails: CheckboxGroupChangeEventDetails, ) => void) | undefined
allValuesstring[]—
- Name
- Description
Names of all checkboxes in the group. Use this when creating a parent checkbox.
- Type
string[] | undefined
disabledbooleanfalse
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | undefined- Default
false
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: CheckboxGroup.State) => string | undefined)
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | (( state: CheckboxGroup.State, ) => CSSProperties | undefined) | undefined
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: CheckboxGroup.State, ) => ReactElement)