import React from 'react'; import { useForm, UseFormProps, SubmitHandler, FieldValues, FieldPath, FieldErrors, } from 'react-hook-form'; import './Form.scss'; interface FormProps extends UseFormProps { onSubmit: SubmitHandler; children: (props: { register: (name: FieldPath, options?: any) => any; errors: FieldErrors; handleBlur: (field: FieldPath) => void; getValues: () => T; watch: (field?: FieldPath) => any; setValue: (field: FieldPath, value: any, options?: object) => void; }) => React.ReactNode; } const Form = ({ onSubmit, children, ...rest }: FormProps) => { const { register, handleSubmit, formState: { errors }, trigger, getValues, watch, setValue } = useForm(rest); const handleBlur = async (field: FieldPath) => { await trigger(field); }; return (
{children({ register, errors, handleBlur, getValues, watch, setValue, })}
); }; export default Form;