# Tag Input

The chosen tags inline, then Add tag, which opens a searchable list with a tick on the right of each chosen tag and Create “…” for a new one. Built on ui-sdk's multiple Combobox.

- Beta: ready to use; its API can still change in a minor release.
- Category: Form fields

```tsx
import { TagInput } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it to label a record with several of a firm's tags, like Urgent and Referral on an opportunity.
- Do: Set `onCreate` only where a person may add to the firm's tags. Add the new tag to `options` and the value, and return a Promise so its row shows as pending until it saves.
- Do: Give an option a `tone` when the colour means something across the firm, like urgent. The tag's text still says it, so colour is never the only signal.
- Do: Set `addLabel` and `searchLabel` to what is being added when the tags have a narrower meaning, like "Add practice area".
- Don't: Use it to choose records, like the contacts on a matter. Use `Combobox` with `multiple`, which searches as you type and shows a second line like an email.

## Keyboard

| Keys | What they do |
| --- | --- |
| Typing | Opens the list with the tags that match. |
| Arrow keys | Move the highlight while focus stays in the input. |
| Enter | Chooses the highlighted tag, or creates one from the text on the create option. |
| Backspace | In an empty input, removes the last tag. |
| Escape | Closes the list and keeps focus in the input. |

## Related

- [Badge](./badge.md)
- [Combobox](./combobox.md)
- [Select](./select.md)

## Examples

What the examples import:

```tsx
import { useState } from "react";
import { Field, TagInput, type TagOption } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const starting: TagOption[] = [
  { value: "conveyancing", label: "Conveyancing", tone: "info" },
  { value: "urgent", label: "Urgent", tone: "warning" },
  { value: "referral", label: "Referral", tone: "purple" },
  { value: "estate", label: "Estate", tone: "teal" },
  { value: "vip", label: "VIP", tone: "yellow" },
];

function TagDemo() {
  const [options, setOptions] = useState(starting);
  const [value, setValue] = useState(["conveyancing", "urgent", "referral"]);
  const create = (name: string) => {
    const tag = {
      value: name.toLowerCase(),
      label: name,
      tone: "slate" as const,
    };
    setOptions(current => [...current, tag]);
    setValue(current => [...current, tag.value]);
  };
  return (
    <div className="w-md">
      <Field label="Tags">
        <TagInput
          options={options}
          value={value}
          onValueChange={setValue}
          onCreate={create}
        />
      </Field>
    </div>
  );
}
```

### Choosing and creating

Press Add tag and type: arrows move, Enter toggles a tag, Escape closes. Text that matches no tag offers Create. Each tag's × removes it; Backspace does too from the keyboard. A new tag opens its space and turns in; a removed one fades first, then the row closes up behind it.

```tsx
<TagDemo />
```

### Empty and disabled

```tsx
<div className="flex flex-col gap-4">
  <TagInput options={starting} />
  <TagInput options={starting} defaultValue={["vip"]} disabled />
</div>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `options` (required) | `TagOption[]` |  | Every tag that can be chosen. |
| `value` | `string[]` |  | The chosen tags' values. Pass it to control the input. |
| `defaultValue` | `string[]` |  | The tags chosen at first, when uncontrolled. |
| `onValueChange` | `(value: string[]) => void` |  | Runs with the chosen tags' values whenever one is added or removed. |
| `onCreate` | `(name: string) => unknown` |  | Shows a Create "…" row for text that matches no tag, and runs with that text. Add the new tag to `options` and to the value; return a Promise to show the row as pending until it settles. |
| `addLabel` | `string` | `"Add tag"` | The trigger's text. |
| `searchLabel` | `string` | `"Search tags"` | Names the search field in the list. |
| `name` | `string` |  | The name the values submit under. |
| `disabled` | `boolean` |  | Shows it as unavailable and stops it responding. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
