{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tag-input","type":"registry:ui","title":"Tag input","description":"Text field that turns entries into chips on Enter, comma or paste, validates each one, and springs new chips in while removed ones slide out.","author":"GetUrUI <hello@geturui.dev>","dependencies":["motion"],"registryDependencies":["@geturui/field","@geturui/tag-group"],"files":[{"path":"registry/geturui/ui/tag-input.tsx","type":"registry:ui","target":"components/ui/tag-input.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\nimport { Field, fieldWellClasses, type FieldSize } from \"@/components/ui/field\";\nimport { Tag, type TagProps } from \"@/components/ui/tag-group\";\n\nconst wellPad: Record<FieldSize, string> = { sm: \"py-1 pl-1\", md: \"py-1.5 pl-1.5\", lg: \"py-2 pl-2\" };\nconst inputText: Record<FieldSize, string> = { sm: \"h-6 text-[13px]\", md: \"h-7 text-sm\", lg: \"h-8 text-[15px]\" };\nconst SPLIT = /[,;\\n\\t]+/;\n\nexport interface TagInputProps {\n  label?: React.ReactNode;\n  /** Helper text under the field. Replaced by any error. */\n  hint?: React.ReactNode;\n  /** External error; takes priority over validation messages. */\n  error?: React.ReactNode;\n  required?: boolean;\n  /** Well height and chip size: sm 32 / md 40 / lg 48. @default \"md\" */\n  size?: FieldSize;\n  value?: string[];\n  defaultValue?: string[];\n  onValueChange?: (value: string[]) => void;\n  placeholder?: string;\n  /** Most chips allowed; shows a counter in the label row. */\n  max?: number;\n  /** Return a message to reject an entry. */\n  validate?: (tag: string, current: string[]) => string | null | undefined | void;\n  /** Allow the same entry twice. @default false */\n  allowDuplicates?: boolean;\n  /** Normalise an entry before validation. @default trims whitespace */\n  transform?: (raw: string) => string;\n  /** Turn leftover text into a chip when the field loses focus. @default true */\n  addOnBlur?: boolean;\n  /** Extra Tag props per chip: initials, dot, icon. */\n  tagProps?: (tag: string) => Partial<Omit<TagProps, \"children\" | \"onRemove\" | \"value\">>;\n  /** Submits the chips as one comma-joined value. */\n  name?: string;\n  id?: string;\n  disabled?: boolean;\n  className?: string;\n}\n\n/** Text field that turns entries into chips on Enter, comma or paste; Backspace on an empty field removes the last one. */\nexport function TagInput({\n  label,\n  hint,\n  error,\n  required,\n  size = \"md\",\n  value,\n  defaultValue = [],\n  onValueChange,\n  placeholder,\n  max,\n  validate,\n  allowDuplicates = false,\n  transform = (raw) => raw.trim(),\n  addOnBlur = true,\n  tagProps,\n  name,\n  id,\n  disabled,\n  className,\n}: TagInputProps) {\n  const [own, setOwn] = React.useState<string[]>(defaultValue);\n  const tags = value ?? own;\n  const [draft, setDraft] = React.useState(\"\");\n  const [issue, setIssue] = React.useState<string | null>(null);\n  const [announce, setAnnounce] = React.useState(\"\");\n  const input = React.useRef<HTMLInputElement>(null);\n  const full = max !== undefined && tags.length >= max;\n\n  const commit = (next: string[]) => {\n    if (value === undefined) setOwn(next);\n    onValueChange?.(next);\n  };\n\n  /** Adds entries; returns what was rejected so text can stay in the field. */\n  const add = (raws: string[]) => {\n    const next = [...tags];\n    const rejected: string[] = [];\n    let message: string | null = null;\n    for (const raw of raws) {\n      const tag = transform(raw);\n      if (!tag) continue;\n      if (max !== undefined && next.length >= max) {\n        message = `You can add up to ${max}.`;\n        rejected.push(raw);\n        continue;\n      }\n      if (!allowDuplicates && next.some((t) => t.toLowerCase() === tag.toLowerCase())) {\n        message = `${tag} is already added.`;\n        rejected.push(raw);\n        continue;\n      }\n      const verdict = validate?.(tag, next);\n      if (verdict) {\n        message = verdict;\n        rejected.push(raw);\n        continue;\n      }\n      next.push(tag);\n    }\n    const added = next.slice(tags.length);\n    if (added.length) {\n      commit(next);\n      setAnnounce(added.length === 1 ? `Added ${added[0]}` : `Added ${added.length} items`);\n    }\n    setIssue(message);\n    return rejected;\n  };\n\n  const removeAt = (index: number) => {\n    const tag = tags[index];\n    commit(tags.filter((_, i) => i !== index));\n    setIssue(null);\n    setAnnounce(`Removed ${tag}`);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n    if ((e.key === \"Enter\" || e.key === \",\") && !e.nativeEvent.isComposing) {\n      if (!draft.trim()) {\n        if (e.key === \",\") e.preventDefault();\n        return;\n      }\n      e.preventDefault();\n      const rejected = add([draft]);\n      setDraft(rejected[0] ?? \"\");\n    } else if (e.key === \"Backspace\" && draft === \"\" && tags.length > 0) {\n      e.preventDefault();\n      removeAt(tags.length - 1);\n    } else if (e.key === \"Escape\" && draft) {\n      setDraft(\"\");\n      setIssue(null);\n    }\n  };\n\n  const onPaste = (e: React.ClipboardEvent<HTMLInputElement>) => {\n    const text = e.clipboardData.getData(\"text\");\n    if (!SPLIT.test(text)) return;\n    e.preventDefault();\n    const rejected = add((draft + text).split(SPLIT));\n    setDraft(rejected.map((r) => r.trim()).join(\", \"));\n  };\n\n  const shown = error ?? issue ?? undefined;\n  return (\n    <Field\n      label={label}\n      hint={hint}\n      error={shown}\n      required={required}\n      id={id}\n      disabled={disabled}\n      className={className}\n      labelAside={\n        max !== undefined ? (\n          <span className={cn(\"font-mono tabular-nums transition-colors\", full ? \"text-warning\" : \"text-ink-subtle\")}>\n            {tags.length}/{max}\n          </span>\n        ) : undefined\n      }\n    >\n      {({ id: fieldId, describedBy, invalid }) => (\n        <div\n          onClick={(e) => {\n            if (e.target === e.currentTarget) input.current?.focus();\n          }}\n          className={fieldWellClasses({\n            size,\n            invalid,\n            disabled,\n            className: cn(\"relative h-auto cursor-text flex-wrap gap-1.5\", wellPad[size]),\n          })}\n        >\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            {tags.map((tag, i) => (\n              <Tag key={tag + (allowDuplicates ? `-${i}` : \"\")} size={size} disabled={disabled} onRemove={() => removeAt(i)} {...tagProps?.(tag)}>\n                {tag}\n              </Tag>\n            ))}\n          </AnimatePresence>\n          <input\n            ref={input}\n            id={fieldId}\n            value={draft}\n            disabled={disabled}\n            placeholder={full ? undefined : tags.length ? \"\" : placeholder}\n            aria-invalid={invalid || undefined}\n            aria-describedby={describedBy}\n            aria-required={required || undefined}\n            onChange={(e) => {\n              setDraft(e.target.value);\n              if (issue) setIssue(null);\n            }}\n            onKeyDown={onKeyDown}\n            onPaste={onPaste}\n            onBlur={() => {\n              if (addOnBlur && draft.trim()) {\n                const rejected = add([draft]);\n                setDraft(rejected[0] ?? \"\");\n              }\n            }}\n            className={cn(\"min-w-[8ch] flex-1 bg-transparent pr-2 text-ink outline-none placeholder:text-ink-subtle\", inputText[size])}\n          />\n          {name && <input type=\"hidden\" name={name} value={tags.join(\",\")} />}\n          <span className=\"sr-only\" aria-live=\"polite\">\n            {announce}\n          </span>\n        </div>\n      )}\n    </Field>\n  );\n}\n"},{"path":"LICENSE-geturui.md","type":"registry:file","target":"LICENSE-geturui.md","content":"MIT License\n\nCopyright (c) 2026 geturui.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"}],"categories":["components","tag input","chips","email","multi value","form","tokens"],"meta":{"tier":"free","license":"MIT","docs":"https://geturui.com/docs/tag-input"}}