Checkbox Group

Checkbox Groups combine a series of checkboxes together.

Give FeedbackWAI-ARIABundle Size
index.tsx

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 { CheckboxGroup } from '@base-ui-components/react/checkbox-group';
import { Checkbox } from '@base-ui-components/react/checkbox';

Anatomy

Checkbox Group is composed of a Root component and Checkbox components:

<CheckboxGroup.Root>
  <Checkbox.Root />
  <Checkbox.Root />
</CheckboxGroup.Root>

Labeling

Field components are used to label the Checkbox Group and individual Checkboxes:

import { Field } from '@base-ui-components/react/field';
<Field.Root>
  <CheckboxGroup.Root>
    <Field.Label>Colors</Field.Label>
    <Field.Root>
      <Checkbox.Root name="red" />
      <Field.Label>Red</Field.Label>
    </Field.Root>
    <Field.Root>
      <Checkbox.Root name="blue" />
      <Field.Label>Blue</Field.Label>
    </Field.Root>
  </CheckboxGroup.Root>
</Field.Root>

Controlled

The value and onValueChange props control the Checkbox Group with external state. value is an array of strings matching the name props of Checkboxes that are currently checked:

const [value, setValue] = useState(['red']);

return (
  <CheckboxGroup.Root value={value} onValueChange={setValue}>
    <Checkbox.Root name="red" /> {/* Checked */}
    <Checkbox.Root name="green" />
    <Checkbox.Root name="blue" />
  </CheckboxGroup.Root>
);

Parent Checkbox

A Checkbox can control a group of child Checkboxes.

  1. Make CheckboxGroup.Root controlled and add allValues as a prop — an array of strings that contains the names of all the child checkboxes.
  2. Add a parent prop to the Checkbox.Root component that controls the other (child) Checkboxes inside the group.
  3. Give the child Checkboxes a name prop that identifies them inside the allValues array.
const allValues = ['a', 'b', 'c'];

function App() {
  const [value, setValue] = useState([]);
  return (
    <CheckboxGroup.Root value={value} onValueChange={setValue} allValues={allValues}>
      <Checkbox.Root parent />
      {allValues.map((value) => (
        <Checkbox.Root key={value} name={value} />
      ))}
    </CheckboxGroup.Root>
  );
}
UnstyledCheckboxGroupNested.tsx

To preserve the initial state of the child checkboxes when the parent checkbox is toggled, set the preserveChildStates prop to true:

<CheckboxGroup.Root preserveChildStates>
  <Checkbox.Root parent />
  {allValues.map((value) => (
    <Checkbox.Root key={value} name={value} />
  ))}
</CheckboxGroup.Root>

API Reference

CheckboxGroupRoot

The foundation for building custom-styled checkbox groups.

PropTypeDefaultDescription
allValuesarrayOfAll values of the checkboxes in the group.
classNameunionClass names applied to the element or a function that returns them based on the component's state.
defaultValuearrayOfThe default checked values of the checkbox group. Use when uncontrolled.
disabledboolfalseWhether the checkbox group is disabled.
onValueChangefuncA callback function that is called when the value of the checkbox group changes. Use when controlled.
preserveChildStatesboolfalseWhether the parent checkbox should preserve its child states when checked/unchecked, leading to a tri-state checkbox group.
renderunionA function to customize rendering of the component.
valuearrayOfThe currently checked values of the checkbox group. Use when controlled.

Contents