{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "repeatable-field-group",
  "title": "Repeatable Field Group",
  "description": "A set of fields people can repeat: add a row, remove one, drag rows into order. Rows grow in and slide out, focus lands in the new row, Enter in the last row adds another, and min / max limits keep the list in bounds.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/repeatable-field-group/repeatable-field-group.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, Reorder, useDragControls, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface RepeatableField {\n  key: string;\n  label: string;\n  type?: \"text\" | \"email\" | \"number\" | \"url\" | \"select\";\n  placeholder?: string;\n  /** For type \"select\". */\n  options?: { value: string; label: string }[];\n  /** Flex grow of this column (default 1). */\n  grow?: number;\n  /** Value a new row starts with. */\n  initial?: string;\n}\n\nexport type RepeatableRow = Record<string, string>;\n\nexport interface RepeatableFieldGroupProps {\n  fields?: RepeatableField[];\n  value?: RepeatableRow[];\n  defaultValue?: RepeatableRow[];\n  onValueChange?: (rows: RepeatableRow[]) => void;\n  /** Rows can't be removed below this. */\n  min?: number;\n  /** The add button disables at this many rows. */\n  max?: number;\n  label?: string;\n  description?: string;\n  addLabel?: string;\n  /** Show drag handles to reorder rows. */\n  reorderable?: boolean;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.3)\", panel: \"#0E0E0E\", border: \"rgba(255,255,255,0.1)\", input: \"rgba(255,255,255,0.04)\", inputBorder: \"rgba(255,255,255,0.12)\", inputHover: \"rgba(255,255,255,0.22)\", focus: \"rgba(245,244,241,0.85)\", hover: \"rgba(255,255,255,0.07)\", danger: \"#FF7A6B\", dashed: \"rgba(255,255,255,0.18)\", option: \"#141414\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.3)\", panel: \"#FFFFFF\", border: \"rgba(10,10,10,0.1)\", input: \"#FFFFFF\", inputBorder: \"rgba(10,10,10,0.14)\", inputHover: \"rgba(10,10,10,0.28)\", focus: \"rgba(10,10,10,0.85)\", hover: \"rgba(10,10,10,0.05)\", danger: \"#E5484D\", dashed: \"rgba(10,10,10,0.2)\", option: \"#FFFFFF\" },\n};\n\nconst DEFAULT_FIELDS: RepeatableField[] = [\n  { key: \"email\", label: \"Email\", type: \"email\", placeholder: \"name@company.com\", grow: 1.6 },\n  {\n    key: \"role\",\n    label: \"Role\",\n    type: \"select\",\n    initial: \"viewer\",\n    options: [\n      { value: \"admin\", label: \"Admin\" },\n      { value: \"editor\", label: \"Editor\" },\n      { value: \"viewer\", label: \"Viewer\" },\n    ],\n  },\n];\n\nconst DEFAULT_ROWS: RepeatableRow[] = [\n  { email: \"maya@northwind.co\", role: \"admin\" },\n  { email: \"leo@northwind.co\", role: \"editor\" },\n];\n\ntype Row = { id: string; values: RepeatableRow };\n\nexport function RepeatableFieldGroup({ fields = DEFAULT_FIELDS, value, defaultValue = DEFAULT_ROWS, onValueChange, min = 1, max = 6, label = \"Invite teammates\", description = \"They'll get an email with a link to join.\", addLabel = \"Add another\", reorderable = true, theme = \"dark\", className }: RepeatableFieldGroupProps) {\n  const p = PALETTES[theme];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const seq = React.useRef(0);\n  const [rows, setRows] = React.useState<Row[]>(() => (value ?? defaultValue).map((values, i) => ({ id: `r${i}`, values })));\n  const focusNext = React.useRef<string | null>(null);\n  const listRef = React.useRef<HTMLDivElement>(null);\n\n  // Controlled: keep our row ids, but take the values from the prop.\n  const shown: Row[] = value ? value.map((values, i) => ({ id: rows[i]?.id ?? `c${i}`, values })) : rows;\n\n  const commit = (next: Row[]) => {\n    setRows(next);\n    onValueChange?.(next.map((r) => r.values));\n  };\n  const blank = (): RepeatableRow => Object.fromEntries(fields.map((f) => [f.key, f.initial ?? \"\"]));\n  const add = () => {\n    if (shown.length >= max) return;\n    seq.current += 1;\n    const id = `n${seq.current}`;\n    focusNext.current = id;\n    commit([...shown, { id, values: blank() }]);\n  };\n  const remove = (id: string) => {\n    if (shown.length <= min) return;\n    const i = shown.findIndex((r) => r.id === id);\n    const next = shown.filter((r) => r.id !== id);\n    // Hand focus to the row that takes its place, so keyboard users don't land on <body>.\n    focusNext.current = next[Math.min(i, next.length - 1)]?.id ?? null;\n    commit(next);\n  };\n  const edit = (id: string, key: string, v: string) => commit(shown.map((r) => (r.id === id ? { ...r, values: { ...r.values, [key]: v } } : r)));\n\n  React.useEffect(() => {\n    const id = focusNext.current;\n    if (!id) return;\n    focusNext.current = null;\n    (listRef.current?.querySelector(`[data-row=\"${id}\"] input, [data-row=\"${id}\"] select`) as HTMLElement | null)?.focus({ preventScroll: true });\n  });\n\n  const fieldStyle: React.CSSProperties = { height: 38, borderRadius: 10, background: p.input, border: `1px solid ${p.inputBorder}`, color: p.text, fontSize: 13.5, [\"--rf-hover\" as string]: p.inputHover, [\"--tw-ring-color\" as string]: p.focus };\n  const fieldClass = \"w-full min-w-0 outline-none transition-[border-color] duration-150 hover:[border-color:var(--rf-hover)] focus-visible:ring-2 focus:[border-color:var(--rf-hover)]\";\n  const full = shown.length >= max;\n\n  return (\n    <div role=\"group\" aria-labelledby={`${uid}-label`} className={cn(\"flex w-full flex-col\", className)} style={{ gap: 14, padding: 18, borderRadius: 18, background: p.panel, border: `1px solid ${p.border}`, fontFamily: \"Inter, sans-serif\", color: p.text }}>\n      <div className=\"flex flex-col\" style={{ gap: 3 }}>\n        <span id={`${uid}-label`} className=\"text-[14.5px] font-semibold\">\n          {label}\n        </span>\n        {description && (\n          <span className=\"text-[12.5px]\" style={{ color: p.muted }}>\n            {description}\n          </span>\n        )}\n      </div>\n\n      <div className=\"flex items-center text-[11.5px] font-medium tracking-wide uppercase\" style={{ gap: 8, color: p.faint, paddingLeft: reorderable ? 26 : 0, paddingRight: 36 }} aria-hidden=\"true\">\n        {fields.map((f) => (\n          <span key={f.key} style={{ flex: f.grow ?? 1, minWidth: 0 }}>\n            {f.label}\n          </span>\n        ))}\n      </div>\n\n      <Reorder.Group ref={listRef} as=\"div\" axis=\"y\" values={shown} onReorder={commit} className=\"flex flex-col\" style={{ gap: 8, marginTop: -6 }}>\n        <AnimatePresence initial={false}>\n          {shown.map((row, i) => (\n            <FieldRow key={row.id} row={row} index={i} fields={fields} p={p} reduce={reduce} reorderable={reorderable && shown.length > 1} canRemove={shown.length > min} fieldStyle={fieldStyle} fieldClass={fieldClass} onEdit={edit} onRemove={remove} onEnter={i === shown.length - 1 ? add : undefined} />\n          ))}\n        </AnimatePresence>\n      </Reorder.Group>\n\n      <div className=\"flex items-center justify-between\" style={{ gap: 12 }}>\n        <motion.button\n          type=\"button\"\n          onClick={add}\n          disabled={full}\n          whileTap={full || reduce ? undefined : { scale: 0.97 }}\n          className=\"flex cursor-pointer items-center rounded-[10px] text-[13px] font-medium outline-none hover:bg-[var(--rf-add-hover)] focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-40\"\n          style={{ height: 34, padding: \"0 12px 0 10px\", gap: 7, border: `1px dashed ${p.dashed}`, color: p.text, [\"--rf-add-hover\" as string]: p.hover, [\"--tw-ring-color\" as string]: p.focus }}\n        >\n          <svg width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" aria-hidden=\"true\">\n            <path d=\"M12 5v14M5 12h14\" />\n          </svg>\n          {addLabel}\n        </motion.button>\n        <span className=\"text-[12px] tabular-nums\" style={{ color: p.faint }} aria-live=\"polite\">\n          {shown.length} of {max}\n        </span>\n      </div>\n    </div>\n  );\n}\n\ninterface FieldRowProps {\n  row: Row;\n  index: number;\n  fields: RepeatableField[];\n  p: (typeof PALETTES)[\"dark\"];\n  reduce: boolean;\n  reorderable: boolean;\n  canRemove: boolean;\n  fieldStyle: React.CSSProperties;\n  fieldClass: string;\n  onEdit: (id: string, key: string, v: string) => void;\n  onRemove: (id: string) => void;\n  onEnter?: () => void;\n}\n\nfunction FieldRow({ row, index, fields, p, reduce, reorderable, canRemove, fieldStyle, fieldClass, onEdit, onRemove, onEnter }: FieldRowProps) {\n  const controls = useDragControls();\n  const [dragging, setDragging] = React.useState(false);\n  return (\n    <Reorder.Item\n      as=\"div\"\n      value={row}\n      data-row={row.id}\n      dragListener={false}\n      dragControls={controls}\n      onDragStart={() => setDragging(true)}\n      onDragEnd={() => setDragging(false)}\n      className=\"relative flex items-center\"\n      style={{ gap: 8, zIndex: dragging ? 2 : 1, background: p.panel, borderRadius: 10 }}\n      initial={reduce ? false : { opacity: 0, height: 0, y: -6 }}\n      animate={{ opacity: 1, height: 38, y: 0, scale: dragging ? 1.02 : 1 }}\n      exit={reduce ? { opacity: 0 } : { opacity: 0, height: 0, x: 24, transition: { duration: 0.2 } }}\n      transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 38 }}\n    >\n      {reorderable ? (\n        <button\n          type=\"button\"\n          aria-label={`Reorder row ${index + 1}`}\n          onPointerDown={(e) => {\n            e.preventDefault();\n            controls.start(e);\n          }}\n          className=\"flex shrink-0 cursor-grab touch-none items-center justify-center rounded-md outline-none hover:bg-[var(--rf-hover-bg)] focus-visible:ring-2 active:cursor-grabbing\"\n          style={{ width: 18, height: 30, color: dragging ? p.text : p.faint, [\"--rf-hover-bg\" as string]: p.hover, [\"--tw-ring-color\" as string]: p.focus }}\n        >\n          <svg width=\"10\" height=\"16\" viewBox=\"0 0 10 16\" fill=\"currentColor\" aria-hidden=\"true\">\n            {[3, 8, 13].flatMap((y) => [<circle key={`a${y}`} cx=\"2.5\" cy={y} r=\"1.4\" />, <circle key={`b${y}`} cx=\"7.5\" cy={y} r=\"1.4\" />])}\n          </svg>\n        </button>\n      ) : (\n        <span aria-hidden=\"true\" style={{ width: 18 }} />\n      )}\n      {fields.map((f) => {\n        const v = row.values[f.key] ?? \"\";\n        const aria = `${f.label}, row ${index + 1}`;\n        return (\n          <div key={f.key} className=\"relative flex items-center\" style={{ flex: f.grow ?? 1, minWidth: 0, boxShadow: dragging ? \"0 10px 24px -10px rgba(0,0,0,0.6)\" : \"none\", borderRadius: 10 }}>\n            {f.type === \"select\" ? (\n              <>\n                <select aria-label={aria} value={v} onChange={(e) => onEdit(row.id, f.key, e.target.value)} className={cn(fieldClass, \"cursor-pointer appearance-none\")} style={{ ...fieldStyle, padding: \"0 30px 0 11px\" }}>\n                  {f.options?.map((o) => (\n                    <option key={o.value} value={o.value} style={{ background: p.option, color: p.text }}>\n                      {o.label}\n                    </option>\n                  ))}\n                </select>\n                <svg className=\"pointer-events-none absolute right-2.5\" width=\"12\" height=\"12\" viewBox=\"0 0 12 12\" fill=\"none\" stroke={p.muted} strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                  <path d=\"M3 4.5 6 7.5 9 4.5\" />\n                </svg>\n              </>\n            ) : (\n              <input\n                type={f.type ?? \"text\"}\n                aria-label={aria}\n                value={v}\n                placeholder={f.placeholder}\n                onChange={(e) => onEdit(row.id, f.key, e.target.value)}\n                onKeyDown={(e) => {\n                  if (e.key === \"Enter\" && onEnter) {\n                    e.preventDefault();\n                    onEnter();\n                  }\n                }}\n                className={cn(fieldClass, \"placeholder:text-[var(--rf-ph)]\")}\n                style={{ ...fieldStyle, padding: \"0 11px\", [\"--rf-ph\" as string]: p.faint }}\n              />\n            )}\n          </div>\n        );\n      })}\n      <motion.button\n        type=\"button\"\n        aria-label={`Remove row ${index + 1}`}\n        disabled={!canRemove}\n        onClick={() => onRemove(row.id)}\n        whileTap={canRemove && !reduce ? { scale: 0.88 } : undefined}\n        className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-[9px] outline-none hover:bg-[var(--rf-del-bg)] hover:text-[var(--rf-del)] focus-visible:ring-2 disabled:cursor-not-allowed disabled:opacity-30\"\n        style={{ width: 28, height: 28, color: p.muted, [\"--rf-del\" as string]: p.danger, [\"--rf-del-bg\" as string]: `color-mix(in srgb, ${p.danger} 12%, transparent)`, [\"--tw-ring-color\" as string]: p.focus }}\n      >\n        <svg width=\"15\" height=\"15\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n          <path d=\"M4.5 7h15M10 11v6M14 11v6M6.5 7l.8 11.2A2 2 0 0 0 9.3 20h5.4a2 2 0 0 0 2-1.8L17.5 7M9.5 7V5a1 1 0 0 1 1-1h3a1 1 0 0 1 1 1v2\" />\n        </svg>\n      </motion.button>\n    </Reorder.Item>\n  );\n}\n\nexport default RepeatableFieldGroup;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}