{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "inline-edit-unique-name",
  "title": "Inline Edit Unique Name",
  "description": "An inline rename that checks the name against existing ones when you save, ignoring case and the item's own current name, and says which name clashes.",
  "registryDependencies": [
    "@tealmac/inline-edit"
  ],
  "files": [
    {
      "path": "registry/patterns/inline-edit-unique-name/inline-edit-unique-name.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { InlineEdit } from \"@/components/ui/inline-edit\"\n\nexport interface InlineEditUniqueNameProps {\n  name: string\n  /** Other names that are taken (the current name does not count). */\n  existing: string[]\n  /** Optionally ask the server too, for names the client cannot know. Return true if taken. */\n  isTaken?: (name: string) => Promise<boolean>\n  onRename: (next: string) => void | Promise<void>\n  label?: string\n}\n\n/**\n * Names that must be unique are checked twice: instantly against the names\n * already loaded (case-insensitive, trimmed) and then, if given, against the\n * server. Renaming to a different casing of your own name is allowed. The\n * error names the clashing entry so the person knows what to change.\n */\nexport function InlineEditUniqueName({\n  name,\n  existing,\n  isTaken,\n  onRename,\n  label = \"Name\",\n}: InlineEditUniqueNameProps) {\n  const [current, setCurrent] = React.useState(name)\n  const norm = (s: string) => s.trim().toLowerCase()\n\n  return (\n    <InlineEdit\n      label={label}\n      value={current}\n      validate={(next) => {\n        if (!next) return `${label} is required`\n        const clash = existing.find(\n          (e) => norm(e) === norm(next) && norm(next) !== norm(current)\n        )\n        return clash ? `\"${clash}\" is already used` : null\n      }}\n      onSave={async (next) => {\n        if (norm(next) !== norm(current) && (await isTaken?.(next))) {\n          throw new Error(`\"${next}\" was just taken`)\n        }\n        await onRename(next)\n        setCurrent(next)\n      }}\n    />\n  )\n}\n",
      "type": "registry:block"
    }
  ],
  "type": "registry:block"
}