Tag input
A list of removable tags typed into one field, with suggestions.
Demo
Usage
import { Field, TagInput } from '@tuttn/slant'
// Uncontrolled, with suggestions, submitted with a form:
// formData.getAll('labels') is the list.
<TagInput label="Labels" defaultValue={['design']} suggestions={labels} name="labels" />
// Controlled, checking what is typed: text that fails stays in the input.
const [recipients, setRecipients] = useState<string[]>([])
<TagInput label="Recipients" value={recipients} onValueChange={setRecipients}
placeholder="Add an email" validate={text => /@/.test(text)} max={5} />
// Suggestions only, in a Field: the label names the input and the list.
<Field>
<Field.Label>Reviewers</Field.Label>
<TagInput suggestions={people} allowCustom={false} />
<Field.Description>Pick up to three.</Field.Description>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string[] | — | The tags. Pass it to control the field; leave it out to let the field keep its own. |
defaultValue | string[] | [] | The tags an uncontrolled field starts with. |
onValueChange | (value: string[]) => void | — | Called with the new list whenever a tag is added or removed. |
suggestions | readonly SelectOption[] | — | Tags to offer as the reader types. The input becomes a combobox over a list of them, filtered by the text and leaving out the tags already added. A tag that is a suggestion's value shows the suggestion's label. |
allowCustom | boolean | true | Whether typed text that is no suggestion can become a tag. Set it to false to accept suggestions only. |
validate | (text: string) => boolean | — | Checks typed text before it becomes a tag (an email address, say). Text it turns down stays in the input. Suggestions skip it. |
max | number | — | The most tags the field takes. At the limit, adding stops and the suggestions stay closed. |
delimiters | string[] | [','] | The strings that end a tag as they are typed or pasted. Enter always adds, and pasted newlines split too; pass `[]` for Enter alone. |
label | string | — | The accessible name, unless a Field's label or `aria-labelledby` names the field. |
name | string | — | Submits the tags with a form: one hidden input per tag, so `formData.getAll(name)` is the list, and none while it is empty. |
form | string | — | The id of a form elsewhere on the page that the hidden inputs belong to. |
size | 'sm' | 'md' | 'lg' | 'md' | Height on the control scale (28, 34 or 40px at regular density) while the tags fit on one line; the field grows a line at a time. |
invalid | boolean | — | Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this. |
…rest | InputHTMLAttributes<HTMLInputElement> | — | The ref and every other attribute (placeholder, autoFocus, aria-*, handlers) reach the text <input>; className and style go on the field around the tags. `name` and `form` go on the hidden inputs instead, so the text being typed is never submitted. |
Keyboard
- Enter adds the trimmed text, and doesn't submit the form while there is text. Typing a delimiter (a comma by default) adds the text before it. With the suggestions open and one active, Enter adds that one instead.
- Backspace in the empty input removes the last tag.
- Left Arrow at the start of the input (Right Arrow under
dir="rtl") moves focus to the last tag's remove button. There, Left and Right move between the tags' buttons and back into the input, and Backspace or Delete removes that tag and moves on to the next one, or the input. Tab leaves the field: the remove buttons are out of the tab order. - With suggestions, Down and Up Arrow open the list and move through it, wrapping at the ends. Escape closes it; with the list closed, Escape clears the text, and goes no further, so a dialog around the field stays open.
Accessibility
The tags are a list, each with a remove button named by messages.removeTag ("Remove Design"). Every tag added or removed is announced politely (messages.tagAdded, messages.tagRemoved), and so is the number of matching suggestions once the reader pauses. With suggestions the input is a combobox with list autocomplete: focus stays in it, and aria-activedescendant points at the active option. The list is named as the input is. The input is named by a Field's label, aria-labelledby, or label.
Forms
With a name, each tag is a hidden input of that name, so formData.getAll(name) is the list, and an empty field submits nothing. In a Field, required means at least one tag: the input is required only while there are none. A disabled field submits nothing.
Behaviour
- A tag is skipped when its text is empty, is already there (ignoring case), fails
validate, would passmax, or is no suggestion whileallowCustomis false. Skipped text stays in the input for the reader to fix. - Typed text that is a suggestion's value or text adds that suggestion, and a tag that is a suggestion's value shows its label.
validatechecks custom text only. - Pasting text with delimiters or line breaks adds each piece; the pieces that can't be added stay in the input.
- The list filters suggestions by "contains" and leaves out the tags already added. With custom tags allowed, no suggestion is active until the reader moves into the list, so Enter adds what was typed; with
allowCustom={false}the first match is active, so Enter takes it, and no matches shows a "No results" line. - Read-only or disabled, the tags show without remove buttons and nothing can be added. Disabled, the border and the input dim while the tags keep their contrast, so they stay readable.
For choosing one option from a list, see Combobox or Select; for a single removable tag, Chip.