The CheckboxGroup component is used for displaying a list of related options, in the form of Checkbox inputs, where multiple choices may be selected.

Importing

In a React app, the component can be imported via:

import { CheckboxGroup } from '@customerio/pluma-components/react';

In an Ember app, the component is named PlumaCheckboxGroup.

Usage

A CheckboxGroup should contain Checkbox components as children. CheckboxGroup will forward the name attribute to any Checkboxes nested within, as well as connect onChange handlers to them, so there's no need to define those props on the Checkboxes themselves:

Test checkbox group
import { CheckboxGroup, Checkbox } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState(['one']);

  return (
    <CheckboxGroup
      label="Test checkbox group"
      value={value}
      onChange={(value) => {
        setValue(value);
      }}
    >
      <Checkbox value="one" label="One" />
      <Checkbox value="two" label="Two" />
      <Checkbox value="three" label="Three" />
    </CheckboxGroup>
  );
}

Or, in Ember:

Loading...

Labels

A CheckboxGroup must always have an associated label. The component accepts a label string prop, which renders text above the nested checkboxes.

Alternatively, ariaLabelledby may be used to associate the group with a label elsewhere in the DOM. To make sure the association is set up both ways, it's recommended to also set for on the label, pointing to an id on the checkbox group:

import { Box, CheckboxGroup, Checkbox } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [value, setValue] = useState([]);

  return (
    <Box>
      <Box as="label" id="my_label_id" htmlFor="my_group">
        Make a selection
      </Box>
      <CheckboxGroup
        id="my_group"
        ariaLabelledby="my_label_id"
        value={value}
        onChange={(value) => {
          setValue(value);
        }}
      >
        <Checkbox value="one" label="One" />
        <Checkbox value="two" label="Two" />
        <Checkbox value="three" label="Three" />
      </CheckboxGroup>
    </Box>
  );
}

Finally, the ariaLabel prop can be used if no other element is suitable for a label.

Disabled groups

The isDisabled prop on a CheckboxGroup will take precedence over the isDisabled prop on individual Checkbox components.

It is not necessary to set isDisabled on the individual Checkbox components, as the CheckboxGroup provides that state to the Checkboxes through its context.

Test checkbox group
import { Box, CheckboxGroup, Checkbox } from '@customerio/pluma-components/react';
import { useState } from 'react';

export default function Example() {
  const [isDisabled, setIsDisabled] = useState(false);
  const [value, setValue] = useState([]);

  return (
    <Box>
      <Box as="label" mb="200" display="block">
        <Checkbox
          label="Disable group"
          isChecked={isDisabled}
          onChange={(e) => {
            setIsDisabled(e.target.checked);
          }}
        />
      </Box>
      <CheckboxGroup
        label="Test checkbox group"
        isDisabled={isDisabled}
        value={value}
        onChange={(value) => {
          setValue(value);
        }}
      >
        <Checkbox value="one" label="One" isDisabled={false} />
        <Checkbox value="two" label="Two" isDisabled={false} />
        <Checkbox value="three" label="Three" isDisabled={true} />
      </CheckboxGroup>
    </Box>
  );
}

API

The aria-label attribute to be applied on the fieldset

The aria-labelledby attribute to be applied on the fieldset

An optional id attribute to assign to the fieldset

Whether the entire group is disabled

The label text to be shown above the checkbox group

The name shared by checkboxes in this group. If none is provided, one will be generated

An array of the currently selected values

On this page