TagInput
A free-form multi-value input: Enter or a comma turns text into removable tags. Good for labels, emails and keywords.
Status: beta. Category: Forms.
import { TagInput } from '@glyphui/ui'
Exports: TagInput, tagInputStyles.
Notes
- Backspace in an empty input removes the last tag.
- Each remove button is labelled “Remove <tag>” for screen readers.
TagInput props
| Prop | Type | Default | Description |
|---|---|---|---|
allowDuplicates | boolean | Allow the same tag more than once. Default false. | |
aria-describedby | string | ||
aria-invalid | boolean | ||
aria-label | string | ||
className | string | ||
defaultValue | string[] | ||
disabled | boolean | ||
id | string | ||
maxTags | number | Stop accepting tags after this many. | |
name | string | Submits each tag as its own hidden input with this name. | |
onValueChange | ((tags: string[]) => void) | ||
placeholder | string | ||
separators | readonly string[] | Keys (besides Enter) that turn the typed text into a tag. Default [',']. | |
size | "sm" | "md" | "lg" | ||
validate | ((tag: string, tags: readonly string[]) => string | true) | Return an error message to reject a tag, or true to accept it. | |
value | string[] |
Example: Keywords
import { Field, TagInput } from '@glyphui/ui'
import { useState } from 'react'
export default function Example() {
const [tags, setTags] = useState(['design-system', 'react'])
return (
<Field label="Keywords" description="Press Enter or comma to add." className="w-full max-w-md">
<TagInput value={tags} onValueChange={setTags} placeholder="Add a keyword…" />
</Field>
)
}
Example: Validation and limits
Reject tags with validate; stop at maxTags. Paste a comma-separated list to add several.
import { Field, TagInput } from '@glyphui/ui'
const email = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
export default function Example() {
return (
<Field
label="Invite teammates"
description="Up to 5 email addresses."
className="w-full max-w-md"
>
<TagInput
placeholder="[email protected]"
maxTags={5}
validate={(tag) => (email.test(tag) ? true : `“${tag}” isn't a valid email address.`)}
/>
</Field>
)
}
Live page: https://glyphui.xyz/components/tag-input