"use client"; import * as React from "react"; import { cn } from "./lib/utils"; export interface FieldArrayRow { id: string; value: string; } export interface FieldArrayProps extends React.HTMLAttributes { label?: string; rows: FieldArrayRow[]; onRowsChange?: (rows: FieldArrayRow[]) => void; placeholder?: string; maxRows?: number; } /** Dynamic form field array: add/remove/reorder list items. */ export function FieldArray({ label = "Fields", rows, onRowsChange, placeholder = "Value…", maxRows = 10, className, ...props }: FieldArrayProps) { const update = (idx: number, value: string) => onRowsChange?.(rows.map((r, i) => (i === idx ? { ...r, value } : r))); const remove = (idx: number) => onRowsChange?.(rows.filter((_, i) => i !== idx)); const add = () => onRowsChange?.([...rows, { id: `row-${Date.now()}`, value: "" }]); const move = (idx: number, dir: -1 | 1) => { const target = idx + dir; if (target < 0 || target >= rows.length) return; const next = [...rows]; [next[idx], next[target]] = [next[target], next[idx]]; onRowsChange?.(next); }; return (

{label} · {rows.length}

{rows.map((row, idx) => (
{idx + 1} update(idx, e.target.value)} placeholder={placeholder} className="min-w-0 flex-1 rounded-md border border-input bg-background px-2.5 py-1.5 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring transition-colors motion-reduce:transition-none" />
))}
); }