{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "name-input",
  "title": "Name Input",
  "description": "Name field that spots caps lock, all-lowercase, stray digits, doubled spaces and a first/last mix-up, and offers, never forces, a fix cased the way McDonald, O'Brien, van der Berg and de la Cruz are really written, with a Keep option.",
  "dependencies": [
    "lucide-react"
  ],
  "files": [
    {
      "path": "registry/default/name-input/name-case.ts",
      "content": "// Bollard UI — name-input · MIT licence · https://bollard-ui.vercel.app/components/name-input\r\n\r\n/** Name particles that stay lower case between other words: Maria de la Cruz, Anna van der Berg. */\r\nconst PARTICLES = new Set([\"van\", \"von\", \"der\", \"den\", \"de\", \"del\", \"della\", \"di\", \"da\", \"das\", \"dos\", \"du\", \"la\", \"le\", \"ten\", \"ter\", \"bin\", \"bint\", \"ibn\", \"al\", \"el\", \"zu\"])\r\n/** \"Mac\" names that take a second capital. Others (Mack, Macon, Machado) don't, so this is a list, not a rule. */\r\nconst MAC = new Set([\"macdonald\", \"macarthur\", \"macgregor\", \"mackenzie\", \"macleod\", \"macmillan\", \"macintyre\", \"macneil\", \"maclean\", \"macpherson\", \"macaulay\", \"macinnes\", \"macdougall\", \"mackinnon\", \"macfarlane\", \"maclachlan\", \"macrae\", \"mackay\"])\r\nconst SUFFIXES = new Set([\"ii\", \"iii\", \"iv\"])\r\n\r\n/** Common family names that are rarely given names, used only to spot a swapped first/last. */\r\nconst SURNAMES = new Set([\r\n  \"sharma\", \"patel\", \"singh\", \"kaur\", \"kumar\", \"gupta\", \"khan\", \"reddy\", \"rao\", \"das\", \"shah\", \"mehta\", \"iyer\", \"nair\", \"joshi\",\r\n  \"kulkarni\", \"deshpande\", \"verma\", \"agarwal\", \"banerjee\", \"chatterjee\", \"mukherjee\", \"pillai\", \"menon\", \"chopra\", \"kapoor\",\r\n  \"malhotra\", \"bhat\", \"jain\", \"yadav\", \"pandey\", \"mishra\", \"patil\", \"pawar\", \"jadhav\", \"shinde\",\r\n  \"smith\", \"johnson\", \"williams\", \"brown\", \"jones\", \"garcia\", \"miller\", \"davis\", \"rodriguez\", \"martinez\", \"hernandez\",\r\n  \"lopez\", \"gonzalez\", \"wilson\", \"anderson\", \"taylor\", \"nguyen\", \"wang\", \"zhang\", \"chen\", \"müller\", \"schmidt\", \"schneider\",\r\n])\r\n/** Common given names, the other half of the swap signal. */\r\nconst GIVEN = new Set([\r\n  \"priya\", \"rahul\", \"amit\", \"anita\", \"sunita\", \"rohan\", \"neha\", \"pooja\", \"arjun\", \"aditya\", \"ananya\", \"vikram\", \"sanjay\",\r\n  \"deepak\", \"kavya\", \"aisha\", \"fatima\", \"mohammed\", \"ravi\", \"suresh\", \"ramesh\", \"meera\", \"tushar\", \"isha\", \"karan\", \"sneha\",\r\n  \"john\", \"mary\", \"james\", \"maria\", \"anna\", \"david\", \"sarah\", \"michael\", \"emma\", \"olivia\", \"liam\", \"noah\", \"sofia\", \"wei\", \"sean\", \"patrick\",\r\n])\r\n\r\nconst cap = (word: string) => word.charAt(0).toUpperCase() + word.slice(1)\r\n\r\n/** Cases one word: McDonald, MacGregor (listed only), O'Brien, D'Angelo, Jean-Pierre. */\r\nfunction caseWord(word: string): string {\r\n  const low = word.toLowerCase()\r\n  if (low.includes(\"-\")) return low.split(\"-\").map(caseWord).join(\"-\")\r\n  const apostrophe = /^(\\p{L})(['’])(\\p{L}.*)$/u.exec(low)\r\n  if (apostrophe) return apostrophe[1].toUpperCase() + apostrophe[2] + cap(apostrophe[3])\r\n  if (/^mc\\p{L}{2,}/u.test(low)) return \"Mc\" + cap(low.slice(2))\r\n  if (MAC.has(low)) return \"Mac\" + cap(low.slice(3))\r\n  return cap(low)\r\n}\r\n\r\nconst letters = (word: string) => word.replace(/[^\\p{L}]/gu, \"\")\r\nconst isUpper = (word: string) => letters(word).length > 0 && word === word.toUpperCase() && word !== word.toLowerCase()\r\nconst isLower = (word: string) => letters(word).length > 0 && word === word.toLowerCase() && word !== word.toUpperCase()\r\n\r\n/**\r\n * Re-cases a name typed in all caps or all lower case, word by word. Words the\r\n * person already mixed-cased (DeShawn, Macdonald) are left exactly as typed.\r\n * Short all-caps words inside an otherwise normal name (PJ, AJ) are treated as initials.\r\n */\r\nexport function smartCaseName(name: string, { surname = false }: { surname?: boolean } = {}): string {\r\n  const words = name.trim().split(/\\s+/).filter(Boolean)\r\n  const shouting = words.every((w) => isUpper(w) || !letters(w))\r\n  return words\r\n    .map((word, i) => {\r\n      const low = word.toLowerCase()\r\n      if (!isLower(word) && !isUpper(word)) return word\r\n      if (isUpper(word) && !shouting && letters(word).length <= 2) return word\r\n      if (i > 0 && SUFFIXES.has(low)) return low.toUpperCase()\r\n      // A leading particle stays lower case in a surname-only field (\"van der Berg\"), and is capitalised in a full name.\r\n      if ((i > 0 || surname) && i < words.length - 1 && PARTICLES.has(low)) return low\r\n      return caseWord(word)\r\n    })\r\n    .join(\" \")\r\n}\r\n\r\nexport interface NameIssue {\r\n  kind: \"digits\" | \"case\" | \"spaces\"\r\n  /** The cleaned-up name to offer, when one can be made safely. */\r\n  fix?: string\r\n  /** \"caps\" or \"lower\" for case issues; the stray digits for digit issues. */\r\n  detail?: string\r\n}\r\n\r\n/** Finds the most important slip in a name, or null when it looks fine. Never changes the input. */\r\nexport function findNameIssue(name: string, options?: { surname?: boolean }): NameIssue | null {\r\n  if (!name.trim()) return null\r\n  const words = name.trim().split(/\\s+/)\r\n  const digits = name.match(/\\d/g)\r\n  // Digits stuck to the edge of a word (Sharma2) are safe to drop; inside a word (J0hn) they're left to the user.\r\n  const stripped = words.map((w) => w.replace(/^\\d+|\\d+$/g, \"\")).filter(Boolean).join(\" \")\r\n  const cased = smartCaseName(stripped, options)\r\n  if (digits) return { kind: \"digits\", detail: digits.join(\"\"), fix: /\\d/.test(stripped) ? undefined : cased }\r\n  if (cased !== words.join(\" \")) return { kind: \"case\", fix: cased, detail: words.every(isUpper) || isUpper(words[0]) ? \"caps\" : \"lower\" }\r\n  if (/\\s{2,}/.test(name.trim()) || /^\\s/.test(name)) return { kind: \"spaces\", fix: cased }\r\n  return null\r\n}\r\n\r\n/**\r\n * True when the given and family names look entered the wrong way round:\r\n * \"Sharma,\" / \"Priya\", a particle-led surname in the first field, or a common\r\n * surname first and a common given name last.\r\n */\r\nexport function looksSwapped(first: string, last: string): boolean {\r\n  const f = first.trim().toLowerCase()\r\n  const l = last.trim().toLowerCase()\r\n  if (!f || !l) return false\r\n  if (/,$/.test(f)) return true\r\n  const firstWord = f.split(/\\s+/)[0]\r\n  if (f.includes(\" \") && PARTICLES.has(firstWord) && !PARTICLES.has(l.split(/\\s+/)[0])) return true\r\n  // McKenzie and O'Neal are given names too, so a Mc/O' first field needs a known given name beside it.\r\n  if (/^(mc|o['’])\\p{L}/u.test(f) && GIVEN.has(l)) return true\r\n  // Only when both sides agree: a known surname first AND a known given name last. Plenty of\r\n  // surnames are also first names (Taylor Swift, Kumar Sanu), so a surname alone proves nothing.\r\n  return SURNAMES.has(f) && GIVEN.has(l) && !GIVEN.has(f) && !SURNAMES.has(l)\r\n}\r\n",
      "type": "registry:ui",
      "target": "@components/name-input/name-case.ts"
    },
    {
      "path": "registry/default/name-input/name-input.tsx",
      "content": "\"use client\"\r\n\r\n// Bollard UI — name-input · MIT licence · https://bollard-ui.vercel.app/components/name-input\r\n\r\nimport * as React from \"react\"\r\nimport { ArrowLeftRight, TriangleAlert, X } from \"lucide-react\"\r\n\r\nimport { cn } from \"@/lib/utils\"\r\nimport { findNameIssue, looksSwapped } from \"@/components/name-input/name-case\"\r\n\r\nexport * from \"@/components/name-input/name-case\"\r\n\r\nconst fieldClass =\r\n  \"h-9 w-full min-w-0 rounded-md border bg-transparent px-3 text-base shadow-xs transition-[color,box-shadow] outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm dark:bg-input/30 data-warning:border-amber-500 dark:data-warning:border-amber-400\"\r\nconst fixClass =\r\n  \"inline-flex h-6 min-w-0 items-center gap-1 rounded-md border border-amber-500/40 px-2 font-medium text-amber-900 transition-colors hover:bg-amber-500/10 dark:text-amber-200 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50\"\r\nconst keepClass =\r\n  \"grid size-6 shrink-0 place-items-center rounded-md transition-colors hover:bg-amber-500/10 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50\"\r\n\r\n/** One amber warning line with an optional one-tap fix and a \"keep it\" button. */\r\nfunction Warning({ message, fixLabel, onFix, onKeep, keepLabel, icon: Icon = TriangleAlert }: {\r\n  message: string\r\n  fixLabel?: React.ReactNode\r\n  onFix?: () => void\r\n  onKeep: () => void\r\n  keepLabel: string\r\n  icon?: typeof TriangleAlert\r\n}) {\r\n  return (\r\n    <p className=\"flex gap-1.5 text-amber-700 dark:text-amber-400\">\r\n      <span className=\"flex h-6 shrink-0 items-center\">\r\n        <Icon aria-hidden className=\"size-3.5\" />\r\n      </span>\r\n      <span className=\"flex min-w-0 flex-wrap items-center gap-x-2\">\r\n        <span className=\"py-1\">{message}</span>\r\n        <span className=\"flex min-w-0 items-center gap-1\">\r\n          {onFix && (\r\n            <button type=\"button\" onClick={onFix} className={fixClass}>\r\n              <span className=\"truncate\">{fixLabel}</span>\r\n            </button>\r\n          )}\r\n          <button type=\"button\" onClick={onKeep} aria-label={keepLabel} title={keepLabel} className={keepClass}>\r\n            <X aria-hidden className=\"size-3.5\" />\r\n          </button>\r\n        </span>\r\n      </span>\r\n    </p>\r\n  )\r\n}\r\n\r\nexport interface NameInputProps\r\n  extends Omit<React.ComponentProps<\"input\">, \"value\" | \"defaultValue\" | \"onChange\" | \"type\"> {\r\n  /** Visible label. */\r\n  label?: string\r\n  /** Controlled value. */\r\n  value?: string\r\n  /** Initial value when uncontrolled. */\r\n  defaultValue?: string\r\n  /** Called with the value exactly as typed. Nothing is ever rewritten without a tap. */\r\n  onValueChange?: (value: string) => void\r\n  /** Helper text shown while there is nothing to point out. */\r\n  hint?: string\r\n  /** The field holds only a family name, so a leading particle stays lower case (\"van der Berg\"). */\r\n  surname?: boolean\r\n  /** Pause in ms before suggestions appear while typing. Leaving the field shows them at once. */\r\n  delay?: number\r\n  /** Shown when the name is in capitals. */\r\n  capsMessage?: string\r\n  /** Shown when the name is all lowercase. */\r\n  lowerMessage?: string\r\n  /** Shown when a name has digits; receives them, e.g. \"2\". */\r\n  digitsMessage?: (digits: string) => string\r\n  /** Shown for doubled or stray spaces. */\r\n  spacesMessage?: string\r\n  /** Fix button text; receives the suggested name. */\r\n  fixLabel?: (fix: string) => string\r\n  /** Accessible label of the button that keeps the name as typed. */\r\n  keepLabel?: string\r\n}\r\n\r\n/**\r\n * A person-name field that notices caps lock, an all-lowercase name, stray\r\n * digits and doubled spaces, and offers a cased version that respects McDonald,\r\n * O'Brien, Jean-Pierre, van der Berg and de la Cruz. Words the person already\r\n * mixed-cased are never touched, and nothing changes until they tap the fix.\r\n */\r\nexport function NameInput({\r\n  label = \"Full name\",\r\n  value,\r\n  defaultValue = \"\",\r\n  onValueChange,\r\n  hint,\r\n  surname = false,\r\n  delay = 700,\r\n  capsMessage = \"Typed in capitals?\",\r\n  lowerMessage = \"Names usually start with a capital.\",\r\n  digitsMessage = (digits) => `Names rarely contain digits (${digits}).`,\r\n  spacesMessage = \"There's an extra space.\",\r\n  fixLabel = (fix) => `Use “${fix}”`,\r\n  keepLabel = \"Keep it as typed\",\r\n  id,\r\n  className,\r\n  onBlur,\r\n  ref,\r\n  ...props\r\n}: NameInputProps) {\r\n  const [inner, setInner] = React.useState(defaultValue)\r\n  const [settled, setSettled] = React.useState(defaultValue)\r\n  const [kept, setKept] = React.useState<string>()\r\n  const inputRef = React.useRef<HTMLInputElement>(null)\r\n  const autoId = React.useId()\r\n  const inputId = id ?? autoId\r\n  const name = value ?? inner\r\n\r\n  React.useImperativeHandle(ref, () => inputRef.current as HTMLInputElement)\r\n\r\n  React.useEffect(() => {\r\n    const timer = setTimeout(() => setSettled(name), delay)\r\n    return () => clearTimeout(timer)\r\n  }, [name, delay])\r\n\r\n  const update = (next: string) => {\r\n    if (value === undefined) setInner(next)\r\n    onValueChange?.(next)\r\n  }\r\n\r\n  const issue = settled === name && name !== kept ? findNameIssue(name, { surname }) : null\r\n  const message = !issue\r\n    ? \"\"\r\n    : issue.kind === \"digits\"\r\n      ? digitsMessage(issue.detail ?? \"\")\r\n      : issue.kind === \"spaces\"\r\n        ? spacesMessage\r\n        : issue.detail === \"caps\" ? capsMessage : lowerMessage\r\n\r\n  return (\r\n    <div data-slot=\"name-input\" className={cn(\"grid w-full max-w-sm gap-1.5\", className)}>\r\n      <label htmlFor={inputId} className=\"w-fit text-sm font-medium\">\r\n        {label}\r\n      </label>\r\n      <input\r\n        ref={inputRef}\r\n        id={inputId}\r\n        type=\"text\"\r\n        autoComplete={surname ? \"family-name\" : \"name\"}\r\n        autoCapitalize=\"words\"\r\n        spellCheck={false}\r\n        value={name}\r\n        onChange={(event) => update(event.target.value)}\r\n        onBlur={(event) => {\r\n          setSettled(name)\r\n          onBlur?.(event)\r\n        }}\r\n        aria-describedby={`${inputId}-hint`}\r\n        data-warning={issue ? \"\" : undefined}\r\n        className={fieldClass}\r\n        {...props}\r\n      />\r\n      <div id={`${inputId}-hint`} aria-live=\"polite\" className=\"min-h-6 text-xs\">\r\n        {issue ? (\r\n          <Warning\r\n            message={message}\r\n            fixLabel={issue.fix && fixLabel(issue.fix)}\r\n            onFix={issue.fix ? () => {\r\n              update(issue.fix as string)\r\n              setSettled(issue.fix as string)\r\n              inputRef.current?.focus()\r\n            } : undefined}\r\n            onKeep={() => setKept(name)}\r\n            keepLabel={keepLabel}\r\n          />\r\n        ) : hint ? (\r\n          <p className=\"py-1 text-muted-foreground\">{hint}</p>\r\n        ) : null}\r\n      </div>\r\n    </div>\r\n  )\r\n}\r\n\r\nexport interface PersonName {\r\n  first: string\r\n  last: string\r\n}\r\n\r\nexport interface NameFieldsProps extends Omit<React.ComponentProps<\"fieldset\">, \"defaultValue\" | \"onChange\"> {\r\n  /** Controlled names. */\r\n  value?: PersonName\r\n  /** Initial names when uncontrolled. */\r\n  defaultValue?: PersonName\r\n  /** Called with both names whenever either changes. */\r\n  onValueChange?: (value: PersonName) => void\r\n  /** Label for the first-name box. */\r\n  firstLabel?: string\r\n  /** Label for the last-name box. */\r\n  lastLabel?: string\r\n  /** Shown when the two look entered the wrong way round. */\r\n  swapMessage?: string\r\n  /** Button text to swap first and last. */\r\n  swapLabel?: string\r\n  /** Shown when the whole name went into the first field; receives the word that looks like the surname. */\r\n  splitMessage?: (word: string) => string\r\n  /** Button text to split a full name. */\r\n  splitLabel?: string\r\n  /** Accessible label for keeping the name as typed. */\r\n  keepLabel?: string\r\n  /** Pause in ms after typing stops before the pair is checked. */\r\n  delay?: number\r\n}\r\n\r\n/**\r\n * First and last name fields that also catch the pair entered the wrong way\r\n * round (Sharma / Priya) or the whole name typed into the first field, each\r\n * with a one-tap swap or move. Each field gets NameInput's casing checks.\r\n */\r\nexport function NameFields({\r\n  value,\r\n  defaultValue = { first: \"\", last: \"\" },\r\n  onValueChange,\r\n  firstLabel = \"First name\",\r\n  lastLabel = \"Last name\",\r\n  swapMessage = \"First and last name look swapped.\",\r\n  swapLabel = \"Swap them\",\r\n  splitMessage = (word) => `Is “${word}” the last name?`,\r\n  splitLabel = \"Move it\",\r\n  keepLabel = \"Keep them as typed\",\r\n  delay = 700,\r\n  className,\r\n  ...props\r\n}: NameFieldsProps) {\r\n  const [inner, setInner] = React.useState(defaultValue)\r\n  const [kept, setKept] = React.useState<string>()\r\n  const names = value ?? inner\r\n  const key = `${names.first}\\u0000${names.last}`\r\n  const [settled, setSettled] = React.useState(key)\r\n\r\n  React.useEffect(() => {\r\n    const timer = setTimeout(() => setSettled(key), delay)\r\n    return () => clearTimeout(timer)\r\n  }, [key, delay])\r\n\r\n  const update = (next: PersonName) => {\r\n    if (value === undefined) setInner(next)\r\n    onValueChange?.(next)\r\n  }\r\n\r\n  const words = names.first.trim().split(/\\s+/)\r\n  const ready = key === settled && key !== kept\r\n  const swapped = ready && looksSwapped(names.first, names.last)\r\n  const split = ready && !names.last.trim() && words.length === 2 && /^\\p{Lu}/u.test(words[1]) ? words[1] : \"\"\r\n\r\n  return (\r\n    <fieldset data-slot=\"name-fields\" className={cn(\"@container grid w-full min-w-0 max-w-sm gap-1.5\", className)} {...props}>\r\n      <div className=\"grid gap-x-3 @xs:grid-cols-2\">\r\n        <NameInput label={firstLabel} autoComplete=\"given-name\" value={names.first} onValueChange={(first) => update({ ...names, first })} />\r\n        <NameInput label={lastLabel} surname value={names.last} onValueChange={(last) => update({ ...names, last })} />\r\n      </div>\r\n      <div aria-live=\"polite\" className=\"text-xs empty:hidden\">\r\n        {swapped ? (\r\n          <Warning\r\n            icon={ArrowLeftRight}\r\n            message={swapMessage}\r\n            fixLabel={swapLabel}\r\n            onFix={() => update({ first: names.last.trim(), last: names.first.trim().replace(/,$/, \"\") })}\r\n            onKeep={() => setKept(key)}\r\n            keepLabel={keepLabel}\r\n          />\r\n        ) : split ? (\r\n          <Warning\r\n            message={splitMessage(split)}\r\n            fixLabel={splitLabel}\r\n            onFix={() => update({ first: words[0], last: split })}\r\n            onKeep={() => setKept(key)}\r\n            keepLabel={keepLabel}\r\n          />\r\n        ) : null}\r\n      </div>\r\n    </fieldset>\r\n  )\r\n}\r\n",
      "type": "registry:ui",
      "target": "@ui/name-input.tsx"
    }
  ],
  "meta": {
    "stops": "Names saved in caps or swapped",
    "mistakes": [
      "name all caps",
      "first last name swapped",
      "name capitalization",
      "mcdonald o'brien casing",
      "name validation"
    ],
    "aliases": [
      "name",
      "full name",
      "first name",
      "last name",
      "signup",
      "profile",
      "title case",
      "customer name",
      "form field"
    ],
    "flows": [
      "signup-login",
      "forms"
    ],
    "fields": [
      "name"
    ],
    "summary": "Spots caps lock, all-lowercase, stray digits and swapped first/last names, and offers (never forces) a correctly cased fix.",
    "catches": [
      "ALL CAPS or all-lowercase names",
      "Digits and doubled spaces",
      "First and last names swapped (from common-name lists)",
      "A full name typed into one box: offers to split",
      "Re-casing that respects Mc/Mac, O', particles and hyphens"
    ],
    "limits": [
      "Casing and swap lists are English/Western-centric; they can be wrong for other naming traditions",
      "Only suggests; some people really write their name in capitals or lowercase",
      "Mac names are a list, so unlisted ones aren't re-cased"
    ],
    "a11y": [
      "Visible label; input is described by a polite hint region",
      "Warnings appear after a pause while typing",
      "Keep button has an aria-label and title; icons are aria-hidden"
    ],
    "usage": "import { NameInput } from \"@/components/ui/name-input\"",
    "runtimeDependencies": [
      "lucide-react"
    ],
    "zeroRuntimeDependencies": false
  },
  "docs": "https://bollard-ui.vercel.app/components/name-input",
  "categories": [
    "Validators"
  ],
  "type": "registry:ui"
}
