"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { XIcon } from "lucide-react" import { Controller, useFieldArray, useForm } from "react-hook-form" import { toast } from "sonner" import * as z from "zod" 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 = z.object({ emails: z .array( z.object({ address: z.string().email("Enter a valid email address."), }) ) .min(1, "Add at least one email address.") .max(5, "You can add up to 5 email addresses."), }) export default function FormRhfArray() { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { emails: [{ address: "" }, { address: "" }], }, }) const { fields, append, remove } = useFieldArray({ control: form.control, name: "emails", }) function onSubmit(data: z.infer) { toast("You submitted the following values:", { description: (
          {JSON.stringify(data, 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.
Email Addresses Add up to 5 email addresses where we can contact you. {fields.map((field, index) => ( ( {fields.length > 1 && ( remove(index)} aria-label={`Remove email ${index + 1}`} > )} {fieldState.invalid && ( )} )} /> ))} {form.formState.errors.emails?.root && ( )}
) }