Components

Checkbox Group

Provides shared state to a series of checkboxes.

Apples

Usage guidelines#

Anatomy#

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

Anatomytsx
import { Checkbox } from '@egoist/solid-base/checkbox';
import { CheckboxGroup } from '@egoist/solid-base/checkbox-group';

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

Examples#

Labeling a checkbox group#

Label the group with aria-labelledby and a sibling label element:

Using aria-labelledby to label a checkbox grouptsx
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">{/* ... */}</CheckboxGroup>

An enclosing <label> is the simplest labeling pattern for each checkbox:

Using an enclosing label to label a checkboxtsx
<label>
  <Checkbox.Root value="http" />
  HTTP
</label>

Rendering as a native button#

By default, <Checkbox.Root> renders a <span> element to support enclosing labels. Prefer rendering each checkbox as a native button when using sibling labels (for/id).

Sibling label pattern with a native buttontsx
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">
  <div>
    <label for="protocol-http">HTTP</label>
    <Checkbox.Root id="protocol-http" value="http" nativeButton render="button">
      <Checkbox.Indicator />
    </Checkbox.Root>
  </div>
</CheckboxGroup>

Native buttons with wrapping labels are supported by using the render callback to avoid invalid HTML, so the hidden input is placed outside the label:

Render callbacktsx
<div id="protocols-label">Allowed network protocols</div>
<CheckboxGroup aria-labelledby="protocols-label">
  <Checkbox.Root
    value="http"
    nativeButton
    render={(buttonProps) => (
      <label>
        <button {...buttonProps} />
        HTTP
      </label>
    )}
  />
</CheckboxGroup>

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>

The group controls the parent checkbox's indeterminate state when some, but not all, child checkboxes are checked.

Nested parent checkbox#

Groups can be nested. Keep both groups controlled and sync them in their onValueChange handlers; the outer parent checkbox receives an explicit indeterminate prop when only some of the nested permissions are checked.

API reference#

<CheckboxGroup>

Provides a shared state to a series of checkboxes.

PropTypeDefault
idstring—

The id of the group element.

valuestring[]—

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

To render an uncontrolled checkbox group, use the defaultValue prop instead.

defaultValuestring[]—

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

To render a controlled checkbox group, use the value prop instead.

onValueChange| ((value: string[], eventDetails: CheckboxGroupChangeEventDetails) => void)—

Event handler called when a checkbox in the group is ticked or unticked. Provides the new value as an argument.

allValuesstring[]—

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

disabledbooleanfalse

Whether the component should ignore user interaction.

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-disabled

Present when the checkbox group is disabled.