{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tag-input-that-adds-on-enter-or-comma-and-removes-with-backspace","type":"registry:component","title":"Tag Input That Adds on Enter or Comma and Removes With Backspace","description":"Free React tag input with Tailwind and a plain HTML version: Enter or comma adds a chip, Backspace removes the last one, paste splits lists, changes are announced.","dependencies":[],"files":[{"path":"components/we-inc/tag-input-that-adds-on-enter-or-comma-and-removes-with-backspace.tsx","type":"registry:component","content":"import { useRef, useState, type KeyboardEvent } from \"react\";\n\n// Starting tags and the field label. Change these for your own form.\nconst START = [\"product design\", \"figma\", \"remote\"];\nconst LABEL = \"Skills for this role\";\n\nexport default function TagInputAddsOnEnterOrComma() {\n  const [tags, setTags] = useState<string[]>(START);\n  const [text, setText] = useState(\"\");\n  const [note, setNote] = useState(\"\");\n  const field = useRef<HTMLInputElement>(null);\n\n  // Adds every comma separated piece, skipping blanks and repeats.\n  const addAll = (raw: string) => {\n    const next = [...tags];\n    const added: string[] = [];\n    for (const part of raw.split(/[,\\n]/)) {\n      const t = part.trim().toLowerCase();\n      if (!t) continue;\n      if (next.includes(t)) { setNote(`${t} is already in the list`); continue; }\n      next.push(t);\n      added.push(t);\n    }\n    setTags(next);\n    setText(\"\");\n    if (added.length) setNote(`Added ${added.join(\", \")}`);\n  };\n\n  const remove = (t: string) => {\n    setTags(tags.filter((x) => x !== t));\n    setNote(`Removed ${t}`);\n    field.current?.focus();\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"Enter\" || e.key === \",\") {\n      e.preventDefault();\n      addAll(text);\n    } else if (e.key === \"Backspace\" && text === \"\" && tags.length) {\n      remove(tags[tags.length - 1]);\n    }\n  };\n\n  return (\n    <section className=\"bg-stone-100 px-4 py-16 sm:py-20\">\n      <div className=\"mx-auto max-w-xl rounded-2xl border border-stone-200 bg-white p-6 shadow-sm sm:p-8\">\n        <h2 className=\"text-lg font-semibold tracking-tight text-stone-900\">New job post</h2>\n        <p className=\"mt-1 text-sm text-stone-600\">Junior product designer, Halden Studio</p>\n\n        <label htmlFor=\"tiec-field\" className=\"mt-6 block text-sm font-medium text-stone-800\">{LABEL}</label>\n        <div\n          onClick={() => field.current?.focus()}\n          className=\"mt-2 flex min-h-[52px] flex-wrap items-center gap-2 rounded-xl border border-stone-300 bg-white px-2.5 py-2 transition-colors focus-within:border-indigo-600 focus-within:ring-4 focus-within:ring-indigo-100\"\n        >\n          <ul className=\"contents\" aria-label=\"Added skills\">\n            {tags.map((t) => (\n              <li key={t} className=\"flex h-8 items-center gap-1 rounded-lg bg-indigo-50 pl-3 pr-1 text-sm font-medium text-indigo-900\">\n                {t}\n                <button\n                  type=\"button\"\n                  aria-label={`Remove ${t}`}\n                  onClick={(e) => { e.stopPropagation(); remove(t); }}\n                  className=\"grid h-6 w-6 place-items-center rounded-md text-indigo-700 hover:bg-indigo-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600\"\n                >\n                  <svg viewBox=\"0 0 20 20\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path d=\"M5 5l10 10M15 5L5 15\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n                </button>\n              </li>\n            ))}\n          </ul>\n          <input\n            id=\"tiec-field\"\n            ref={field}\n            value={text}\n            onChange={(e) => (e.target.value.includes(\",\") ? addAll(e.target.value) : setText(e.target.value))}\n            onKeyDown={onKey}\n            onBlur={() => text.trim() && addAll(text)}\n            aria-describedby=\"tiec-hint\"\n            placeholder={tags.length ? \"Add another\" : \"Type a skill\"}\n            className=\"h-8 min-w-[8rem] flex-1 bg-transparent px-1 text-sm text-stone-900 placeholder:text-stone-500 focus:outline-none\"\n          />\n        </div>\n        <p id=\"tiec-hint\" className=\"mt-2 text-xs text-stone-600\">Press Enter or a comma to add. Backspace in an empty field removes the last skill.</p>\n        <p aria-live=\"polite\" className=\"sr-only\">{note}</p>\n        <input type=\"hidden\" name=\"skills\" value={tags.join(\",\")} />\n\n        <div className=\"mt-6 flex items-center justify-between border-t border-stone-200 pt-5\">\n          <span className=\"text-sm text-stone-600\">{tags.length} {tags.length === 1 ? \"skill\" : \"skills\"}</span>\n          <button type=\"button\" className=\"h-10 rounded-lg bg-stone-900 px-4 text-sm font-medium text-white hover:bg-stone-800 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600 focus-visible:ring-offset-2\">Save draft</button>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
