---
title: ToggleSwitchGroup
---

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

# ToggleSwitchGroup

<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%3D2534%253A18332" />

```jsx live
<ToggleSwitchGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third', tooltip: 'Third time is a charm' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
/>
```

## Introduction

ToggleSwitchGroup allows the selection of (by default) one element from a selection of labeled buttons.

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

## Guidance

Use for sets of options with very simple, short labels, ideally one word or two.

Consider `RadioGroup` for items with labels of a few words in length and `RadioCard` for more complex options requiring a title and additional description content.

### `maxValues` prop

The prop `maxValues` may be passed to allow more than one item to be selected. Pass a number to set that
as the maximum, or `null` to have no maximum and allow any number of selections.

```jsx live
<ToggleSwitchGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  maxValues={2}
/>
```

### `onChange` prop

An `onChange` function may be provided, which is called when a ToggleSwitchGroup item is toggled.

```jsx live
<ToggleSwitchGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  onChange={(state) => setTimeout(() => alert(`ToggleSwitchGroup has selected: ${state}`), 100)}
/>
```

### Controlled usage with `values` prop

Pass an array of item ids to the `values` prop as well as `onChange` to control the ToggleSwitchGroup from a parent.

This is usually the easiest way to control sibling content with a ToggleSwitchGroup.

```jsx live
function ControlledToggleSwitchGroup() {
  const [values, setValues] = useState([])
  const text = values.length ? `Saved: "${values.join('", "')}".` : 'No selection saved'
  return (
    <StackView space={2}>
      <Typography variant={{ size: 'small' }}>{text}</Typography>
      <ToggleSwitchGroup
        values={values}
        onChange={setValues}
        items={[
          { label: 'First item', id: 'first' },
          { label: 'Second item', id: 'second' },
          { label: 'Third item', id: 'third' },
          { label: 'Fourth item', id: 'fourth' },
          { label: 'Fifth item', id: 'fifth' }
        ]}
      />
    </StackView>
  )
}
```

### Uncontrolled usage

The ToggleSwitchGroup manages its own state internally if `values` is not passed.

A default selection may be set by passing an array of item IDs to `initialValues`. Do not use both `initialValues` and `values` props in the same `ToggleSwitchGroup`.

```jsx live
<ToggleSwitchGroup
  items={[
    { label: 'First item', id: 'first' },
    { label: 'Second item', id: 'second' },
    { label: 'Third item', id: 'third' },
    { label: 'Fourth item', id: 'fourth' },
    { label: 'Fifth item', id: 'fifth' }
  ]}
  initialValues={['second']}
/>
```

### Accessibility

By default, `ToggleSwitchGroup` is treated by accessibility tools as a group of radio buttons.

If `maxValues` prop is used to allow multiple selections, the buttons will be treated like checkboxes.

### Platform considerations

<AvailableOnBoth />

## Props

<PropsTable base={BaseDocs} />

## Tokens

<TokensTable base={BaseDocs} component="ToggleSwitchGroup" />

## Variants

ToggleSwitchGroup's variants are the same as for [ToggleSwitch](toggle-switch).

## Feedback

<Feedback component="ToggleSwitchGroup" />
