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

PropTypeDefaultDescription
allowDuplicatesbooleanAllow the same tag more than once. Default false.
aria-describedbystring
aria-invalidboolean
aria-labelstring
classNamestring
defaultValuestring[]
disabledboolean
idstring
maxTagsnumberStop accepting tags after this many.
namestringSubmits each tag as its own hidden input with this name.
onValueChange((tags: string[]) => void)
placeholderstring
separatorsreadonly 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.
valuestring[]

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