"use client" import * as React from "react" import { FieldArray, Form, Field as FormischField, insert, remove, reset, useForm, } from "@formisch/react" import type { SubmitHandler } from "@formisch/react" import { XIcon } from "lucide-react" import { toast } from "sonner" import * as v from "valibot" import { Button } from "@/registry/new-york-v4/ui/button" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/registry/new-york-v4/ui/card" import { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLegend, FieldSet, } from "@/registry/new-york-v4/ui/field" import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, } from "@/registry/new-york-v4/ui/input-group" const FormSchema = v.object({ emails: v.pipe( v.array( v.object({ address: v.pipe( v.string(), v.nonEmpty("Enter an email address."), v.email("Enter a valid email address.") ), }) ), v.minLength(1, "Add at least one email address."), v.maxLength(5, "You can add up to 5 email addresses.") ), }) export default function FormFormischArray() { const form = useForm({ schema: FormSchema, initialInput: { emails: [{ address: "" }, { address: "" }], }, }) const handleSubmit: SubmitHandler = (output) => { toast("You submitted the following values:", { description: (
          {JSON.stringify(output, null, 2)}
        
), position: "bottom-right", classNames: { content: "flex flex-col gap-2", }, style: { "--border-radius": "calc(var(--radius) + 4px)", } as React.CSSProperties, }) } return ( Contact Emails Manage your contact email addresses.
{(fieldArray) => (
Email Addresses Add up to 5 email addresses where we can contact you. {fieldArray.items.map((item, index) => ( {(field) => ( {fieldArray.items.length > 1 && ( remove(form, { path: ["emails"], at: index, }) } aria-label={`Remove email ${index + 1}`} > )} {field.errors && ( ({ message, }))} /> )} )} ))} {fieldArray.errors && ( ({ message }))} /> )}
)}
) }