import { IconX } from "@tabler/icons-react"; import cn from "classnames"; import { useFormikContext } from "formik"; import { useState } from "react"; import type { AccessListClient } from "src/api/backend"; import { T } from "src/locale"; interface Props { initialValues: AccessListClient[]; name?: string; } export function AccessClientFields({ initialValues, name = "clients" }: Props) { const [values, setValues] = useState(initialValues || []); const { setFieldValue } = useFormikContext(); const blankClient: AccessListClient = { directive: "allow", address: "" }; if (values?.length === 0) { setValues([blankClient]); } const handleAdd = () => { setValues([...values, blankClient]); }; const handleRemove = (idx: number) => { const newValues = values.filter((_: AccessListClient, i: number) => i !== idx); if (newValues.length === 0) { newValues.push(blankClient); } setValues(newValues); setFormField(newValues); }; const handleChange = (idx: number, field: string, fieldValue: string) => { const newValues = values.map((v: AccessListClient, i: number) => i === idx ? { ...v, [field]: fieldValue } : v, ); setValues(newValues); setFormField(newValues); }; const setFormField = (newValues: AccessListClient[]) => { const filtered = newValues.filter((v: AccessListClient) => v?.address?.trim() !== ""); setFieldValue(name, filtered); }; return ( <>

{values.map((client: AccessListClient, idx: number) => (
handleChange(idx, "address", e.target.value)} placeholder="192.168.1.100 or 192.168.1.0/24 or 2001:0db8::/32" />
{ e.preventDefault(); handleRemove(idx); }} >
))}

); }