Skip to contents

Checkbox Group

Provides shared state to a series of checkboxes.

View as Markdown
Apples

Usage guidelines

Anatomy

Checkbox Group is composed together with Checkbox. Import the components and place them together:

Anatomy

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.

Using aria-labelledby to label a checkbox group

For individual checkboxes, use the Label component. It supports the same enclosing labeling pattern as a native label element, so clicking on any gaps between the label text and checkbox still toggles it.

Using Label to label a checkbox

The Field and Fieldset components are alternatives that add validation and group labeling support.

  1. 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 the render prop. <Fieldset.Legend> provides the visible label for the group.
  2. Individual label: Place each individual checkbox in <Field.Item>, then label it using an enclosing <Field.Label>. Enclosing labels ensure that clicking on any gaps between the label and checkbox still toggles it.
Using Field and Fieldset to label a checkbox group

Form integration

To use a checkbox group in a form, pass the checkbox group’s name to <Field.Root>:

Using Checkbox Group in a form

Parent checkbox

A checkbox that controls other checkboxes within a <CheckboxGroup> can be created:

  1. Make <CheckboxGroup> a controlled component
  2. Pass an array of all the child checkbox values to the allValues prop on the <CheckboxGroup> component
  3. Add the parent boolean prop to the parent <Checkbox.Root>

Nested parent checkbox

API reference

defaultValuestring[]
Description

Names of the checkboxes in the group that should be initially ticked.

To render a controlled checkbox group, use the value prop 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 defaultValue prop instead.

Type
string[] | undefined
onValueChangefunction
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[]
Description

Names of all checkboxes in the group. Use this when creating a parent checkbox.

Type
string[] | undefined
disabledbooleanfalse
Description

Whether the component should ignore user interaction.

Type
boolean | undefined
Default
false
classNamestring | function
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 ReactElement or a function that returns the element to render.

Type
| ReactElement
| ((
    props: HTMLProps,
    state: CheckboxGroup.State,
  ) => ReactElement)
data-disabled
Present when the checkbox group is disabled.