import { useState } from 'react' import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' const MAX_MEMBERS = 5 export interface MemberFieldsProps { members: string[] onChange: (next: string[]) => void } /** * Inline member entry: one text field per member with a "+" to append another * row below, and a "×" to remove a row. The parent store only ever sees the * trimmed, non-empty names; empty rows are a local editing affordance. */ export function MemberFields({ members, onChange }: MemberFieldsProps) { // Seed local rows from the parent (always at least one row to type into). const [rows, setRows] = useState(members.length ? members : ['']) const commit = (next: string[]) => { setRows(next) onChange(next.map((r) => r.trim()).filter(Boolean)) } const setRow = (i: number, value: string) => { const next = rows.slice() next[i] = value commit(next) } const addRow = () => { if (rows.length >= MAX_MEMBERS) return setRows([...rows, '']) // don't commit — empty row adds nothing to the store } const removeRow = (i: number) => { const next = rows.length > 1 ? rows.filter((_, idx) => idx !== i) : [''] commit(next) } const full = rows.length >= MAX_MEMBERS const filled = rows.filter((r) => r.trim()).length return (
{rows.map((row, i) => (
{i + 1} setRow(i, e.target.value)} className="font-mono text-sm" />
))}
{filled} / {MAX_MEMBERS}
) }