import React from 'react'; import { Control, Controller, FieldErrors, FieldValues, Path } from 'react-hook-form'; import { spacing } from '@/lib/spacing'; interface FormFieldProps { control: Control; errors: FieldErrors; name: Path; label: string; description?: string | React.ReactNode; required?: boolean; component: React.ElementType; [key: string]: unknown; } const get = (obj: Record, path: string) => { return path.split('.').reduce((acc, part) => acc && acc[part], obj); }; const FormField = ({ control, errors, name, label, description, required, component: Component, ...props }: FormFieldProps) => { const error = get(errors, name); return (
{description && (

{description}

)} { const describedBy = [ description ? `${name}-description` : undefined, error ? `${name}-error` : undefined, ] .filter(Boolean) .join(' '); return ( 0 ? describedBy : undefined} /> ); }} /> {error && (

{String(error.message)}

)}
); }; export default FormField;