# Combobox multiple

> Typing multiple selection with Combobox multiple - chips in the field, custom values, removable chips, keyboard chip navigation and values added on blur.

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

`Combobox multiple` is multiple selection with typing: selected values show as
chips in the field, typing filters the
list, and Enter adds the active option. Chips sit in a list labelled
`localeText.selectedValues`.

```tsx
import { Combobox } from '@salmonui/dropdowns/combobox';
import '@salmonui/dropdowns/combobox/styles.css';

<Combobox
  aria-label="Tags"
  multiple
  creatable
  commitOnBlur
  data={tags}
  defaultValue={['news']}
  getChipProps={(t) => (t.value === 'urgent' ? { className: 'urgent' } : undefined)}
/>
```

`value` / `defaultValue` hold an array; `onValueChange(values, event)`
reports `event.reason` `'add'`, `'create'`, `'deselect'` (picking a
selected option again), `'remove'` (a chip), `'clear'` or `'reset'`, and a controlled `value` vetoes any of them. The popup stays
open after each pick (`closeOnSelect` defaults to `false` here), and
`maxSelected` caps the count. Props, keys and server rendering are
otherwise those of [Combobox](https://salmonui.dev/docs/dropdowns/combobox).

### Live demo: Combobox multiple tags

Live demo: https://salmonui.dev/demos/dropdowns/combobox-tags

Typing MultiSelect with custom tags validated by onCreateItem, chip templates and per-chip styles, remove buttons, chip keyboard navigation, the optional dropdown button and changes vetoed by a controlled value.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Combobox, type ComboboxValue } from '@salmonui/dropdowns/combobox';
import { EventLog, useEventLog } from '../../components/demo-kit';

interface Tag {
  readonly value: string;
  readonly label: string;
  readonly locked?: boolean;
}

const TAGS: Tag[] = [
  { value: 'bug', label: 'bug' },
  { value: 'docs', label: 'docs' },
  { value: 'feature', label: 'feature' },
  { value: 'performance', label: 'performance' },
  { value: 'security', label: 'security', locked: true },
  { value: 'ux', label: 'ux' },
];

const MAX_TAG = 20;

// Created tags are lower-case words of at most 20 letters; anything else is rejected.
const createTag = (text: string): Tag | null => {
  const word = text.trim().toLowerCase();
  return /^[a-z-]+$/.test(word) && word.length <= MAX_TAG ? { value: word, label: word } : null;
};

export default function ComboboxTagsDemo() {
  const [log, push] = useEventLog();
  const [tags, setTags] = useState<readonly ComboboxValue[]>(['bug', 'security']);
  const [showTrigger, setShowTrigger] = useState(false);
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 520 }}>
      <Checkbox checked={showTrigger} onCheckedChange={(checked) => setShowTrigger(checked)}>
        Show the dropdown button
      </Checkbox>
      <div style={{ display: 'grid', gap: 4 }}>
        <span id="combobox-tags-label">Labels</span>
        <Combobox<Tag>
          aria-labelledby="combobox-tags-label"
          multiple
          creatable
          data={TAGS}
          onCreateItem={(text) => {
            const tag = createTag(text);
            push(tag ? `onCreateItem "${text}" → ${tag.value}` : `onCreateItem "${text}" → rejected`);
            return tag;
          }}
          showTrigger={showTrigger}
          getChipProps={(tag) => (tag.locked ? { style: { fontWeight: 700, outline: '1px dashed currentColor' } } : undefined)}
          renderChip={({ item, label }) => (
            <>
              <span aria-hidden="true">#</span>
              {label}
              {item.locked ? <small> (locked)</small> : null}
            </>
          )}
          value={tags}
          onValueChange={(next, event) => {
            push(`onValueChange [${next.join(', ')}] (reason ${event.reason}, source ${event.source})`);
            // A controlled value vetoes: "security" cannot be removed, and at most five labels.
            if (event.reason !== 'clear' && tags.includes('security') && !next.includes('security')) {
              push('vetoed: "security" is locked');
              return;
            }
            if (next.length > 5) {
              push('vetoed: at most five labels');
              return;
            }
            setTags(next);
          }}
          placeholder="Add a label"
        />
      </div>
      <output>Value: [{tags.join(', ')}]</output>
      <EventLog entries={log} />
    </div>
  );
}
```

| Task | How |
| --- | --- |
| Accept typed values not in the list | `creatable` |
| Add the typed text when focus leaves | `commitOnBlur` |
| React to or veto an added chip, style it | `onValueChange` with `reason: 'add'` or `'create'`; a controlled `value` vetoes; `getChipProps(item)` styles the chip |
| Build the item for a custom value | `onCreateItem(text)` |
| React to a removed chip | `onValueChange` with `reason: 'remove'` |
| Remove chips | Each chip has a remove button named `localeText.removeValue(label)`; `clearable` clears all |
| Show or hide the dropdown button | `showTrigger` |
| Custom chip content | `renderChip({ item, label, remove, disabled })` |

Chip keys: Backspace on an empty input removes the last chip; Left
Arrow at the start of the input moves focus to the last chip's remove
button; Left / Right move between chips (mirrored under `dir="rtl"`); Delete
or Backspace removes the focused chip; Right from the last chip, or Escape,
returns to the input. `maxVisibleValues` shows the first chips and a "+N"
chip.

See the [API reference](https://salmonui.dev/docs/reference/dropdowns/combobox) for every prop.
