{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"email-recipient-tag-input-that-validates-each-address","type":"registry:component","title":"Email Recipient Tag Input That Validates Each Address","description":"","dependencies":[],"files":[{"path":"components/we-inc/email-recipient-tag-input-that-validates-each-address.tsx","type":"registry:component","content":"import { useRef, useState, type ClipboardEvent, type KeyboardEvent } from \"react\";\n\n// A loose check that catches typos without rejecting unusual but valid addresses.\nconst VALID = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]{2,}$/;\nconst START = [\"ines.ferreira@larkhill.co\", \"tom@larkhill.co\", \"j.okafor@gmail\"];\n\nexport default function EmailRecipientTagInput() {\n  const [list, setList] = useState<string[]>(START);\n  const [text, setText] = useState(\"\");\n  const [note, setNote] = useState(\"\");\n  const field = useRef<HTMLInputElement>(null);\n  const bad = list.filter((a) => !VALID.test(a)).length;\n\n  const addAll = (raw: string) => {\n    const parts = raw.split(/[\\s,;]+/).map((p) => p.trim()).filter(Boolean);\n    const next = [...list];\n    for (const p of parts) if (!next.includes(p)) next.push(p);\n    setList(next);\n    setText(\"\");\n    const wrong = parts.filter((p) => !VALID.test(p));\n    setNote(wrong.length ? `${wrong.join(\", \")} does not look like an email address` : `Added ${parts.join(\", \")}`);\n  };\n  const remove = (a: string) => { setList(list.filter((x) => x !== a)); setNote(`Removed ${a}`); field.current?.focus(); };\n  // Clicking a wrong address puts it back in the field to fix it.\n  const edit = (a: string) => { setList(list.filter((x) => x !== a)); setText(a); field.current?.focus(); };\n\n  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n    if ([\"Enter\", \",\", \";\", \" \", \"Tab\"].includes(e.key) && text.trim()) { e.preventDefault(); addAll(text); }\n    else if (e.key === \"Backspace\" && text === \"\" && list.length) remove(list[list.length - 1]);\n  };\n  const onPaste = (e: ClipboardEvent<HTMLInputElement>) => { e.preventDefault(); addAll(text + \" \" + e.clipboardData.getData(\"text\")); };\n\n  return (\n    <section className=\"bg-slate-100 px-4 py-16\">\n      <div className=\"mx-auto max-w-2xl overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm\">\n        <div className=\"flex items-center justify-between border-b border-slate-200 px-5 py-3\">\n          <h2 className=\"text-sm font-semibold text-slate-900\">Share &ldquo;Q3 site photos&rdquo;</h2>\n          <span className=\"text-xs text-slate-600\">Larkhill Interiors</span>\n        </div>\n        <div className=\"flex items-start gap-3 border-b border-slate-200 px-5 py-3\">\n          <label htmlFor=\"eret-field\" className=\"mt-1.5 w-8 shrink-0 text-sm text-slate-600\">To</label>\n          <div onClick={() => field.current?.focus()} className=\"flex min-h-[40px] flex-1 flex-wrap items-center gap-1.5\">\n            {list.map((a) => {\n              const ok = VALID.test(a);\n              return (\n                <span key={a} className={`flex h-8 max-w-full items-center rounded-full text-sm ${ok ? \"bg-slate-100 text-slate-800\" : \"bg-rose-50 text-rose-800 ring-1 ring-inset ring-rose-300\"}`}>\n                  {ok ? (\n                    <span className=\"flex items-center gap-2 truncate pl-1\">\n                      <span className=\"grid h-6 w-6 shrink-0 place-items-center rounded-full bg-slate-700 text-[11px] font-semibold uppercase text-white\" aria-hidden=\"true\">{a[0]}</span>\n                      <span className=\"truncate\">{a}</span>\n                    </span>\n                  ) : (\n                    <button type=\"button\" onClick={() => edit(a)} aria-label={`Fix ${a}, not a valid address`} className=\"flex h-8 items-center gap-1.5 truncate rounded-full pl-2.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-rose-500\">\n                      <svg viewBox=\"0 0 20 20\" width=\"14\" height=\"14\" aria-hidden=\"true\"><circle cx=\"10\" cy=\"10\" r=\"8\" fill=\"#e11d48\" /><path d=\"M10 6v5M10 13.5v.5\" stroke=\"#fff\" strokeWidth=\"1.8\" strokeLinecap=\"round\" /></svg>\n                      <span className=\"truncate\">{a}</span>\n                    </button>\n                  )}\n                  <button type=\"button\" aria-label={`Remove ${a}`} onClick={() => remove(a)} className=\"ml-0.5 mr-1 grid h-6 w-6 shrink-0 place-items-center rounded-full hover:bg-black/5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-700\">\n                    <svg viewBox=\"0 0 20 20\" width=\"12\" height=\"12\" aria-hidden=\"true\"><path d=\"M5 5l10 10M15 5L5 15\" stroke=\"currentColor\" strokeWidth=\"2.2\" strokeLinecap=\"round\" /></svg>\n                  </button>\n                </span>\n              );\n            })}\n            <input\n              id=\"eret-field\"\n              ref={field}\n              type=\"email\"\n              multiple\n              value={text}\n              onChange={(e) => setText(e.target.value)}\n              onKeyDown={onKey}\n              onPaste={onPaste}\n              onBlur={() => text.trim() && addAll(text)}\n              aria-describedby=\"eret-msg\"\n              placeholder=\"Add people by email\"\n              className=\"h-8 min-w-[10rem] flex-1 bg-transparent text-sm text-slate-900 placeholder:text-slate-500 focus:outline-none\"\n            />\n          </div>\n        </div>\n        <div className=\"flex flex-col gap-3 px-5 py-4 sm:flex-row sm:items-center sm:justify-between\">\n          <p id=\"eret-msg\" className={`text-sm ${bad ? \"text-rose-700\" : \"text-slate-600\"}`}>\n            {bad ? `${bad} ${bad === 1 ? \"address needs\" : \"addresses need\"} fixing. Click it to edit.` : `${list.length} people will get a view link.`}\n          </p>\n          <button type=\"button\" disabled={bad > 0 || !list.length} className=\"h-10 rounded-lg bg-sky-700 px-5 text-sm font-medium text-white hover:bg-sky-800 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-700 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:bg-slate-300 disabled:text-slate-700\">Send invites</button>\n        </div>\n        <p aria-live=\"polite\" className=\"sr-only\">{note}</p>\n      </div>\n    </section>\n  );\n}\n"}]}
