forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.tsx
37 lines (35 loc) · 1019 Bytes
/
Form.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { zodResolver } from '@hookform/resolvers/zod';
import clsx from 'clsx';
import * as React from 'react';
import { useForm, UseFormReturn, SubmitHandler, UseFormProps } from 'react-hook-form';
import { ZodType } from 'zod';
type FormProps<TFormValues, Schema> = {
className?: string;
onSubmit: SubmitHandler<TFormValues>;
children: (methods: UseFormReturn<TFormValues>) => React.ReactNode;
options?: UseFormProps<TFormValues>;
id?: string;
schema?: Schema;
};
export const Form = <
TFormValues extends Record<string, any> = Record<string, any>,
Schema extends ZodType<any, any, any> = ZodType<any, any, any>
>({
onSubmit,
children,
className,
options,
id,
schema,
}: FormProps<TFormValues, Schema>) => {
const methods = useForm<TFormValues>({ ...options, resolver: schema && zodResolver(schema) });
return (
<form
className={clsx('space-y-6', className)}
onSubmit={methods.handleSubmit(onSubmit)}
id={id}
>
{children(methods)}
</form>
);
};