"use client"; import React from "react"; import { useForm, useFieldArray, SubmitHandler } from "react-hook-form"; import { Input } from "@/components/ui/input"; import { Icon } from "@/components/ui/icon"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Link } from '@/i18n/routing'; interface FormValues { test: { firstName: string; lastName: string; phone: string; }[]; } const Repeater = () => { const { register, handleSubmit, watch, formState: { errors }, control, } = useForm({ defaultValues: { test: [{ firstName: "Bill", lastName: "Luo", phone: "123456" }], }, }); const onSubmit: SubmitHandler = (data) => console.log(data); console.log(watch("test")); const { fields, append, remove } = useFieldArray({ control, name: "test", }); return (
Items info-500
{fields.map((item, index) => (
{errors.test?.[index]?.firstName && ( This field is required )}
{errors.test?.[index]?.lastName && ( This field is required )}
{errors.test?.[index]?.phone && ( This field is required )}
{index > 0 && (
)}
))}
append({ firstName: "", lastName: "", phone: "" })} > Add New
); }; export default Repeater;