---
label: CheckboxGroup
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, TokensTable, PropsTable } from '../../src/components'
import { CheckboxGroup } from '@telus-uds/components-web'
import { CheckboxGroup as BaseDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import NoStylisticVariants from '../../partials/_variants-none.mdx'

# CheckboxGroup

<LibraryInfo figmaLink="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Ffile%2F4zJn2Qn6uXOqPP1GVDipy6%2F%25F0%259F%258D%2580-Allium-Components%3Fnode-id%3D458%253A457" />

```jsx live
<CheckboxGroup
  items={[
    {
      label: 'First item',
      id: 'first'
    },
    {
      label: 'Second item',
      id: 'second'
    },
    {
      label: 'Third item',
      id: 'third'
    }
  ]}
/>
```

## Introduction

A CheckboxGroup renders a group of [Checkbox](checkbox) buttons in a list,
with a text label for each radio button. Exactly one radio button may be selected at a time.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

Use CheckboxGroup where the user needs to select one item from multiple options, and where those
options can be explained simply with one short phrase (generally, up to around 5 words).

### Alternatives

- If the options are very simple and can be described in one or two words or numbers, consider
  [ButtonGroup](button-group) with `maxItems={null}`
- If it should be possible to select only one item at a time, consider
  [RadioGroup](radio-group)

### Labeling the group

Three props are available for labelling the group to users:

- `legend` sets the main title for the group and should be a short, simple name for what the user
  is to choose.
- `hint` is an optional short sentence clarifying the `legend` to help users understand what choice they are
  being asked to make and what the result of their choice will be.
- `tooltip` allows further clarifying information to be included but not shown by default, only shown
  on pressing a [TooltipButton](tooltip). This should be used for information
  that will help many users make a choice, but which is not needed by all users.

```jsx live
<CheckboxGroup
  legend="Account extras"
  hint="Choose optional extras as part of your contract"
  tooltip="Data amounts are listed in GB (gigabytes). 1 GB is roughly 40 minutes of Blu-ray quality video."
  items={[
    {
      label: '100 GB internet data bonus',
      id: 'data-bonus'
    },
    {
      label: 'Free calls to friends and family',
      id: 'family'
    },
    {
      label: 'Use internet data for international calls',
      id: 'international'
    }
  ]}
/>
```

### Items

Use the `items` prop to pass an array of objects describing each RadioCard in the group:

- `label`: text displayed alongside the radio button
- `id`: identifier used to store which RadioCard is selected (uses index if undefined). This is
  written into HTML as an id attr on web, so should be unique and not match IDs used elsewhere on a page.
- `onChange`: optional function called on selection, in addition to updating the group's selection state

```jsx live
<CheckboxGroup
  items={[
    {
      label: 'First item',
      id: 'first'
    },
    {
      label: 'Second item',
      id: 'second'
    },
    {
      label: 'Third item',
      id: 'third',
      onChange: (value, event) =>
        console.log('Third item selected', value, event.currentTarget, event)
    }
  ]}
/>
```

### Controlled

The selection state may be controlled by a parent by passing `checkedIds` and an `onChange`
function that updates the parent's state. If the component is controlled in this way,
`checkedIds` must never be undefined; pass `null` or an empty string if there is no selection.

```jsx live
function ControlledCheckboxGroup() {
  const [checkedIds, setCheckedIds] = useState([])
  return (
    <StackView space={3}>
      <Typography>Currently selected: "{checkedIds.join('", "')}"</Typography>
      <CheckboxGroup
        checkedIds={checkedIds}
        onChange={setCheckedIds}
        items={[
          { label: 'First item', id: 'first' },
          { label: 'Second item', id: 'second' },
          { label: 'Third item', id: 'third' }
        ]}
      />
    </StackView>
  )
}
```

### Uncontrolled

If `checkedIds` is not provided, CheckboxGroup manages its own state. An initial selection may
be provided by passing `initialCheckedIds`.

```jsx live
<CheckboxGroup
  initialCheckedIds="second"
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' }
  ]}
/>
```

### `onChange` functions

The `onChange` handler for CheckboxGroup may be used in both controlled and uncontrolled usage and
takes two arguments: `value` (string) and `event` (React SyntheticEvent object).

The `event` object shape varies between web and native applications, and type of user interaction.
It is usually more stable to have application logic respond to the `value` and not `event` properties.

On web, `event.currentTarget` will point to the accessible container that holds the radio role and
state, and should be used if accessing the raw HTML element is required (`event.target` will vary
on mouse clicks depending on where exactly the click occured).

```jsx live
<CheckboxGroup
  onChange={(value, event) => console.log(value, event.currentTarget, event)}
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' }
  ]}
/>
```

### Use in forms

For web forms, the `name` prop may be used to define the name of the group's `<fieldset>` and input
elements, and `legend` may be used to provide a title with `<legend>` type for the fieldset.

```jsx live
<CheckboxGroup
  legend="Choose a product"
  name="product-choice"
  items={[
    { label: 'First product', id: 'first' },
    { label: 'Second product', id: 'second' },
    { label: 'Third product', id: 'third' }
  ]}
/>
```

### Validation

Validation state may be set by passing 'error' or 'success' to the `validation` prop.

```jsx live
function ValidationExample() {
  const [validation, setValidation] = useState(null)
  const validate = (ids) =>
    setValidation(ids.every((id) => ['a', 'c'].includes(id)) ? 'success' : 'error')
  return (
    <CheckboxGroup
      legend="Choose only 'a' and/or 'c'"
      onChange={validate}
      validation={validation}
      feedback={validation && `${validation === 'success' ? 'Valid' : 'Invalid'} selection chosen.`}
      items={[
        { label: "Option 'a'", id: 'a' },
        { label: "Option 'b'", id: 'b' },
        { label: "Option 'c'", id: 'c' },
        { label: "Option 'd'", id: 'd' }
      ]}
    />
  )
}
```

### A11y guidelines

CheckboxGroup accepts all the common accessibility props, and controls the accessibility state of
children like Checkbox and Feedback based on current state.

### Platform considerations

<AvailableOnBoth />

## Props

<PropsTable base={BaseDocs} />

## Tokens

<TokensTable base={BaseDocs} component="CheckboxGroup" tokensProps={['tokens', 'radioTokens']} />

## Variants

<NoStylisticVariants />

## Feedback

<Feedback component="CheckboxGroup" />
