# Select multiple

> Multiple selection without typing on Select multiple, with checkboxes, joined labels, select all, group selection, limits and summaries.

Canonical: https://salmonui.dev/docs/dropdowns/select-multiple

Multiple selection comes in two components: `Select multiple` when users
pick from the list (with checkboxes or a joined-labels summary) and
[`Combobox multiple`](https://salmonui.dev/docs/dropdowns/combobox-multiple) when they type
(chips, custom values). With `multiple`, `value` is an array
and the listbox carries `aria-multiselectable="true"`.

```tsx
import { Select } from '@salmonui/dropdowns/select';
import '@salmonui/dropdowns/select/styles.css';

<Select multiple aria-label="Fruits" indicator="checkbox" showSelectAll groupSelection data={fruits} defaultValue={['apple']} />
```

Controlled, `value` is a `readonly` array and `onValueChange(values, event)`
reports the whole next array, with `reason` `'select'`, `'deselect'`,
`'selectAll'`, `'clear'` or `'reset'`.

### Live demo: Select multiple

Live demo: https://salmonui.dev/demos/dropdowns/select-multiple

MultiSelect CheckBox mode with select all and group selection, a selection limit with a custom separator, and a count summary over 10,000 items.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Select, type SelectValue } from '@salmonui/dropdowns/select';

const FRUITS = [
  { value: 'apple', label: 'Apple', group: 'Pome' },
  { value: 'pear', label: 'Pear', group: 'Pome' },
  { value: 'apricot', label: 'Apricot', group: 'Stone' },
  { value: 'cherry', label: 'Cherry', group: 'Stone' },
  { value: 'peach', label: 'Peach', group: 'Stone' },
  { value: 'lemon', label: 'Lemon', group: 'Citrus' },
  { value: 'lime', label: 'Lime', group: 'Citrus' },
  { value: 'orange', label: 'Orange', group: 'Citrus' },
];

const ITEMS = Array.from({ length: 10_000 }, (_, i) => ({ value: i, label: `Item ${i}` }));

// MultiSelect CheckBox mode with select all and group selection, a
// selection limit, and a count summary over 10,000 virtualized items.
export default function SelectMultipleDemo() {
  const [tags, setTags] = useState<readonly SelectValue[]>(['apple', 'lime']);
  return (
    <div style={{ display: 'grid', gap: 16, maxWidth: 320 }}>
      <div style={{ display: 'grid', gap: 4 }}>
        <span id="demo-tags-label">Fruits (checkboxes)</span>
        <Select aria-labelledby="demo-tags-label" multiple indicator="checkbox" showSelectAll groupSelection data={FRUITS} value={tags} onValueChange={setTags} />
      </div>
      <div style={{ display: 'grid', gap: 4 }}>
        <span id="demo-limit-label">Up to two</span>
        <Select aria-labelledby="demo-limit-label" multiple maxSelected={2} data={FRUITS} valueSeparator=" · " />
      </div>
      <div style={{ display: 'grid', gap: 4 }}>
        <span id="demo-count-label">10,000 items</span>
        <Select aria-labelledby="demo-count-label" multiple indicator="checkbox" showSelectAll valueDisplay="count" searchable data={ITEMS} />
      </div>
    </div>
  );
}
```

| Task | How |
| --- | --- |
| Checkboxes in the list | `indicator="checkbox"` |
| Show the selection as text | The default `valueDisplay="text"`: labels joined with `Intl.ListFormat` of `locale`, or `valueSeparator` |
| Chips or text | One display per instance, set by `valueDisplay`; it does not switch on focus |
| Select all option | `showSelectAll`: a tri-state first option; texts `localeText.selectAll` / `deselectAll` |
| Select all from code, react to it | Set `value`; the select-all option reports `reason: 'selectAll'` with lazy `items` |
| Limit the selection | `maxSelected`: the remaining options become `aria-disabled` and the limit is announced (`localeText.limitReached`) |
| Select a whole group | `groupSelection`: group headers become tri-state options |
| Hide picked options | `hideSelected` (default `false`) |
| Order of the list | List order stays data order; `hideSelected` keeps picked options out of the way |
| Keep the popup open after a pick | `closeOnSelect` (default `false` with `multiple`) |
| React to changes | `onValueChange` fires on every change; act on blur in `onBlur` |
| Act on a chip | Chips are not selectable; add a handler in `renderChip` |
| Open on click | A click opens the popup |
| Summarize many values | `valueDisplay="count"` (`localeText.selectedCount`, plural through `Intl.PluralRules`) or `maxVisibleValues` with "+N" |

The text display shows up to `maxVisibleValues` labels (default 100), then
"+N", so a select all over 100,000 items costs no label work.

## Design notes

- One display per instance: switching between chips and text on
  focus changes the control's height and shifts the layout, so the display
  is fixed per instance. Use `valueDisplay="text"` or `"count"` on `Select
  multiple`, or [`Combobox multiple`](https://salmonui.dev/docs/dropdowns/combobox-multiple)
  for chips.
- List order is data order: moving selected items to the top
  moves the option under the pointer and the active descendant, against the
  APG listbox model. To keep picked options out of the way, set
  `hideSelected`; the trigger text lists the values in selection order.
- A change event per change: one notification per change keeps
  `value` and the parent's state in step. To act once when focus leaves,
  do the work in `onBlur`, reading the last value from `onValueChange`, or
  read the value on submit.
- Chips are not selectable: a selected-chip state is a second
  selection model with no assistive technology pattern. `Select multiple`
  has no chips; on `Combobox multiple` chips are removable values that
  keyboard focus moves between. To act on a chip, add a handler in
  `renderChip` (for example an `onClick` on the chip content).

Select all covers local `data`. Over a remote source there is no select-all
option: the
value is the list of item values, and selecting every remote item would
need all of them. `showSelectAll` renders nothing there and Ctrl+A leaves
the value unchanged. Fetch the keys from your own endpoint and set `value`:

```tsx
const selectAll = async () => setValue(await fetch('/api/fruit-ids').then((r) => r.json()));
```

## Keyboard

Keys are the Select keys: Space and Enter toggle the active option
and the popup stays open, Ctrl+A (Cmd+A) selects all (or deselects all
when every option is selected; a no-op over a remote source), Alt+Up,
Escape and Tab close. Selected disabled options stay selected when all are
deselected.

See [Select](https://salmonui.dev/docs/dropdowns/select) for events, search, the popup and SSR,
and the [API reference](https://salmonui.dev/docs/reference/dropdowns/select) for every prop.

### Live demo: Select multiple options and keys

Live demo: https://salmonui.dev/demos/dropdowns/select-multiple-options

An array value shown as delimited text, select all from the list and from code, hidden selected options, closing on select, click to open, the multiple-selection keys and ARIA.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select, type SelectValue } from '@salmonui/dropdowns/select';
import { EventLog, useEventLog } from '../../components/demo-kit';

const TOPPINGS = ['Basil', 'Capers', 'Garlic', 'Mushrooms', 'Olives', 'Onions', 'Peppers', 'Pineapple', 'Rocket', 'Tomatoes'];

export default function SelectMultipleOptionsDemo() {
  const [log, push] = useEventLog();
  const [toppings, setToppings] = useState<readonly SelectValue[]>(['Basil', 'Olives']);
  const [hideSelected, setHideSelected] = useState(false);
  const [closeOnSelect, setCloseOnSelect] = useState(false);
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 480 }}>
      <div role="group" aria-label="Options" style={{ display: 'flex', flexWrap: 'wrap', gap: 16, alignItems: 'center' }}>
        <Checkbox checked={hideSelected} onCheckedChange={(checked) => setHideSelected(checked)}>
          <code>hideSelected</code>
        </Checkbox>
        <Checkbox checked={closeOnSelect} onCheckedChange={(checked) => setCloseOnSelect(checked)}>
          <code>closeOnSelect</code>
        </Checkbox>
      </div>
      <span id="select-multiple-options-label">Toppings</span>
      <Select
        aria-labelledby="select-multiple-options-label"
        multiple
        data={TOPPINGS}
        valueSeparator=" · "
        hideSelected={hideSelected}
        closeOnSelect={closeOnSelect}
        showSelectAll
        value={toppings}
        onValueChange={(next, event) => {
          push(`onValueChange [${next.join(', ')}] (reason ${event.reason}, source ${event.source})`);
          setToppings(next);
        }}
        placeholder="Choose toppings"
      />
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
        <Button variant="outline" onClick={() => setToppings(TOPPINGS)}>
          Select all from code
        </Button>
        <Button variant="outline" onClick={() => setToppings([])}>
          Clear from code
        </Button>
      </div>
      <output>Value: [{toppings.join(', ')}]</output>
      <EventLog entries={log} />
    </div>
  );
}
```
