{
  "name": "tag-input",
  "type": "registry:ui",
  "framework": "react",
  "title": "Tag Input",
  "description": "A text field that keeps what was typed as removable chips.",
  "files": [
    {
      "path": "ui/tag-input.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport interface TagInputProps extends Omit<\n  React.InputHTMLAttributes<HTMLInputElement>,\n  \"value\" | \"defaultValue\" | \"onChange\" | \"type\"\n> {\n  /** Controlled list. Leave undefined to let the component own it. */\n  value?: readonly string[]\n  defaultValue?: readonly string[]\n  onValueChange?: (value: readonly string[]) => void\n  /** Stop accepting tags once the list is this long. */\n  max?: number\n  invalid?: boolean\n  /** The accessible name of a tag's remove button. */\n  removeLabel?: (tag: string) => string\n}\n\nconst defaultRemoveLabel = (tag: string) => `Remove ${tag}`\n\n/*\n * A text field that keeps what was typed as chips. Enter or a comma commits\n * the text, Backspace in an empty field takes the last chip back, and a\n * pasted list is split on commas and new lines. A duplicate is dropped\n * rather than added twice.\n */\nconst TagInput = React.forwardRef<HTMLInputElement, TagInputProps>(\n  (\n    {\n      className,\n      value,\n      defaultValue = [],\n      onValueChange,\n      max,\n      invalid = false,\n      disabled = false,\n      removeLabel = defaultRemoveLabel,\n      onKeyDown,\n      onPaste,\n      ...props\n    },\n    ref\n  ) => {\n    const [internal, setInternal] =\n      React.useState<readonly string[]>(defaultValue)\n    const [text, setText] = React.useState(\"\")\n    const isControlled = value !== undefined\n    const tags = isControlled ? value : internal\n\n    const commit = (next: readonly string[]) => {\n      if (!isControlled) setInternal(next)\n      onValueChange?.(next)\n    }\n\n    const add = (candidates: readonly string[]) => {\n      const next = [...tags]\n      for (const candidate of candidates) {\n        const tag = candidate.trim()\n        if (tag === \"\" || next.includes(tag)) continue\n        if (max !== undefined && next.length >= max) break\n        next.push(tag)\n      }\n      if (next.length !== tags.length) commit(next)\n    }\n\n    return (\n      // The wrapper is drawn as the field; a click anywhere in it is a click\n      // on the input, which is the only thing in it a keyboard reaches first.\n      // oxlint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions -- pointer convenience only; the input and the remove buttons are the keyboard targets\n      <div\n        data-tag-input=\"\"\n        data-invalid={invalid || undefined}\n        onClick={(event) => {\n          if (event.target === event.currentTarget) {\n            event.currentTarget.querySelector(\"input\")?.focus()\n          }\n        }}\n        className={cn(\n          \"flex min-h-[42px] w-full cursor-text flex-wrap items-center gap-1.5 rounded-[var(--newt-radius-sm)] border border-newt-border bg-newt-bg-input px-2 py-1.5 transition-colors duration-fast ease-newt focus-within:border-newt-brand data-[invalid]:border-newt-danger group-data-[invalid]/field:border-newt-danger\",\n          disabled && \"cursor-not-allowed opacity-50\",\n          className\n        )}\n      >\n        {tags.map((tag) => (\n          <span\n            key={tag}\n            className=\"inline-flex max-w-full items-center gap-1 rounded-[var(--newt-radius-sm)] bg-newt-bg-active py-[3px] pe-1 ps-2 text-[13px] font-medium leading-[18px] text-newt-text-primary\"\n          >\n            <span className=\"truncate\">{tag}</span>\n            <button\n              type=\"button\"\n              disabled={disabled}\n              aria-label={removeLabel(tag)}\n              onClick={(event) => {\n                commit(tags.filter((each) => each !== tag))\n                event.currentTarget\n                  .closest(\"[data-tag-input]\")\n                  ?.querySelector(\"input\")\n                  ?.focus()\n              }}\n              className=\"inline-flex h-4 w-4 shrink-0 items-center justify-center rounded-full text-newt-text-muted hover:bg-newt-danger hover:text-newt-on-accent focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-newt-text-link\"\n            >\n              <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"h-3 w-3\">\n                <path\n                  fill=\"currentColor\"\n                  d=\"M4.3 4.3a1 1 0 0 1 1.4 0L8 6.6l2.3-2.3a1 1 0 1 1 1.4 1.4L9.4 8l2.3 2.3a1 1 0 0 1-1.4 1.4L8 9.4l-2.3 2.3a1 1 0 0 1-1.4-1.4L6.6 8 4.3 5.7a1 1 0 0 1 0-1.4Z\"\n                />\n              </svg>\n            </button>\n          </span>\n        ))}\n        <input\n          ref={ref}\n          type=\"text\"\n          value={text}\n          disabled={disabled}\n          aria-invalid={invalid || undefined}\n          onChange={(event) => setText(event.target.value)}\n          onKeyDown={(event) => {\n            onKeyDown?.(event)\n            /*\n             * While an input method is composing, Enter confirms a candidate\n             * and is not the reader committing a tag.\n             */\n            if (event.defaultPrevented || event.nativeEvent.isComposing) return\n            if (event.key === \"Enter\" || event.key === \",\") {\n              event.preventDefault()\n              add([text])\n              setText(\"\")\n            } else if (\n              event.key === \"Backspace\" &&\n              text === \"\" &&\n              tags.length > 0\n            ) {\n              commit(tags.slice(0, -1))\n            }\n          }}\n          onPaste={(event) => {\n            onPaste?.(event)\n            if (event.defaultPrevented) return\n            const pasted = event.clipboardData.getData(\"text\")\n            if (!/[,\\n]/.test(pasted)) return\n            event.preventDefault()\n            add(pasted.split(/[,\\n]/))\n          }}\n          className=\"min-w-20 flex-1 border-0 bg-transparent px-1 py-[3px] font-[family-name:var(--newt-font-sans)] text-sm leading-[18px] text-newt-text-primary placeholder:text-newt-text-muted focus:outline-none disabled:cursor-not-allowed\"\n          {...props}\n        />\n      </div>\n    )\n  }\n)\nTagInput.displayName = \"TagInput\"\n\nexport { TagInput }\n",
      "type": "registry:ui"
    }
  ]
}