Can you cehck this component for error? : #// --- Contact Person Schema ---
export const ContactPersonSchema = z.object({
id: z.string().optional(),
first_name: z.string().min(1, "First name is required"),
last_name: z.string().min(1, "Last name is required"),
email: z.string().email("Invalid email address").min(1, "Email is required"),
phone: z.string().optional(),
role: z.literal(["admin", "user", "superuser"]),
});
export type ContactPerson = z.infer;
import React from 'react';
import { useFormContext, useFieldArray } from 'react-hook-form';
import { FullFormData, ContactPerson } from '../types/form';
import { v4 as uuidv4 } from 'uuid'; // For unique keys for dynamically added items
const ContactPersonsForm: React.FC = () => {
// We need to specify the path to the array within the FullFormData
const { control, register, formState: { errors } } = useFormContext();
const { fields, append, remove } = useFieldArray({
control,
name: 'contact_persons',
});
return (
Contact Persons
{fields.map((field, index) => (
<div key={field.id} className="border p-4 rounded-md shadow-sm bg-gray-50 relative">
<h3 className="text-lg font-medium mb-2">Contact Person #{index + 1}</h3>
{fields.length > 1 && (
<button
type="button"
onClick={() => remove(index)}
className="absolute top-2 right-2 text-red-500 hover:text-red-700 p-1"
aria-label={`Remove contact person ${index + 1}`}
>
×
</button>
)}
<div>
<label htmlFor={`contact_persons.${index}.first_name`} className="block text-sm font-medium text-gray-700">First Name</label>
<input
type="text"
id={`contact_persons.${index}.first_name`}
{...register(`contact_persons.${index}.first_name`)}
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2"
/>
{errors.contact_persons?.[index]?.first_name && (
<p className="text-red-500 text-sm mt-1">{errors.contactPersons[index]?.firstName?.message}</p>
)}
</div>
<div>
<label htmlFor={`contact_persons.${index}.last_name`} className="block text-sm font-medium text-gray-700">Last Name</label>
<input
type="text"
id={`contact_persons.${index}.last_name`}
{...register(`contact_persons.${index}.last_name`)}
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2"
/>
{errors.contact_persons?.[index]?.last_name && (
<p className="text-red-500 text-sm mt-1">{errors.contact_persons[index]?.last_name?.message}</p>
)}
</div>
<div>
<label htmlFor={`contact_persons.${index}.email`} className="block text-sm font-medium text-gray-700">Email</label>
<input
type="email"
id={`contact_persons.${index}.email`}
{...register(`contact_persons.${index}.email`)}
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2"
/>
{errors.contact_persons?.[index]?.email && (
<p className="text-red-500 text-sm mt-1">{errors.contact_persons[index]?.email?.message}</p>
)}
</div>
<div>
<label htmlFor={`contact_persons.${index}.phone`} className="block text-sm font-medium text-gray-700">Phone (Optional)</label>
<input
type="tel"
id={`contact_persons.${index}.phone`}
{...register(`contact_persons.${index}.phone`)}
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2"
/>
{errors.contact_persons?.[index]?.phone && (
<p className="text-red-500 text-sm mt-1">{errors.contact_persons[index]?.phone?.message}</p>
)}
</div>
<div>
<label htmlFor={`contact_persons.${index}.role`} className="block text-sm font-medium text-gray-700">Role (user by default)</label>
<input
type="text"
id={`contact_persons.${index}.role`}
{...register(`contact_persons.${index}.role`)}
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2"
/>
{errors.contact_persons?.[index]?.role && (
<p className="text-red-500 text-sm mt-1">{errors.contact_persons[index]?.role?.message}</p>
)}
</div>
</div>
))}
<button
type="button"
onClick={() => append({ id: uuidv4(), first_name: '', last_name: '', email: '', phone:'', role:'' })}
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
>
Add Another Contact Person
</button>
{errors.contact_persons?.root && (
<p className="text-red-500 text-sm mt-1">{errors.contact_persons.root.message}</p>
)}
</div>