Tags input
Free-form list entry: type a value, press Enter, get a chip. Use it when the set of possible values is open-ended.
Purpose
TagsInput collects a list nobody can enumerate up front — labels, codes, keywords. Typing is the point: if the valid values are a known list, picking beats typing — use Multi-select for several from a list, or Combobox for one from a searchable list. For a single free-form value, that is an Input.
Enter or comma commits the draft as a chip; Backspace on an empty field removes the last one. Duplicates are rejected case-insensitively with an inline error, and blurring the field commits whatever was typed so half-entered tags are not lost.
Press Enter or comma to add; Backspace removes the last.
Validation and limits
Pass validate to reject candidates with your own message (return null to accept), and
maxTags to cap the list — the text field hides once the cap is reached and returns when a tag
is removed. The component can be controlled (value/onValueChange) or uncontrolled
(defaultValue):
<div className="grid max-w-md gap-1.5">
<Label htmlFor="corp-codes">Corporate codes (max 4, format C0000)</Label>
<TagsInput
inputId="corp-codes"
defaultValue={["C3167"]}
maxTags={4}
placeholder="e.g. C3167"
validate={(tag) =>
/^C\d{4}$/.test(tag) ? null : `"${tag}" is not a valid corporate code (C0000).`
}
onValueChange={setCodes}
/>
</div>States
Empty, filled, at limit and disabled below. The error state is transient — it appears when a
duplicate or a validate rejection is committed, turns the border --destructive, and clears
on the next keystroke. It cannot be set from outside; for form-level errors wrap the field in
your form's error presentation instead.
Structure
| Property | Value |
|---|---|
| Container | min-height 40px (min-h-10), grows as chips wrap |
| Padding | 8px horizontal, 6px vertical; 6px gap between chips |
| Border | 1px --input · error --destructive · shadow --shadow-1 |
| Radius | --radius-md |
| Focus | border --ring + 2px --ring at 40% (focus-within) |
| Chips | secondary Badge; 13px close glyph remove button |
| Text field | 14px, min-width 8rem; placeholder only while the list is empty |
| Error text | 12px --destructive-text, 6px below the container |
| Disabled | 50% opacity, not-allowed cursor |
Best practices
Do
- Give the field a visible
LabeltargetinginputId— or setinputLabelwhen a visible label genuinely cannot fit. - Use
validateto enforce a format at entry time — an error next to the keystroke beats one at submit. - Cap unbounded lists with
maxTagswhen downstream systems have a limit. - Tell users the commit keys ("Press Enter or comma to add") in a hint — the pattern is not universal.
Don't
- Use it for values from a known list — that is Multi-select.
- Rely on the placeholder as the label — it disappears after the first tag.
- Re-validate for duplicates in
validate— case-insensitive duplicate rejection is already built in.
Accessibility & keyboard
- The inner text field takes its accessible name from a visible
<Label htmlFor>targetinginputId; without one, theinputLabelprop (default "Add a tag") supplies anaria-labelfallback. - Each chip's remove control is a real button named "Remove tag".
- A rejected commit sets
aria-invalidon the text field and links the message viaaria-describedby(id{inputId}-error). The message is a polite live region, so it is announced the moment it appears — focus never leaves the field. - The container is a click-to-focus surface only; the text field inside is the sole tab stop for entry, so keyboard users lose nothing.
- Disabled sets
aria-disabledon the container and disables the field and every remove button. - Keyboard: Enter or comma commits the draft; Backspace on an empty field removes the last tag; blur commits whatever was typed; Tab moves through each chip's remove button to the text field.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string[] | undefined | — | Controlled list of tags. |
defaultValue | string[] | undefined | — | Uncontrolled initial tags. |
onValueChange | ((tags: string[]) => void) | undefined | — | — |
validate | ((tag: string, current: string[]) => string | null) | undefined | — | Reject a candidate tag by returning an error message (null accepts). |
placeholder | string | undefined | — | — |
disabled | boolean | undefined | — | — |
maxTags | number | undefined | — | Maximum number of tags; input hides when reached. |
inputLabel | string | undefined | "Add a tag" | Accessible name for the text field. A placeholder is not a label — it disappears on first keystroke and is ignored by some screen readers. Pass `inputId` and your own `<Label htmlFor>` instead when the field has a visible label; this is the fallback for when it does not. |
inputId | string | undefined | — | `id` for the inner text field, so a visible `<Label htmlFor>` can target it. |