import React, { forwardRef } from 'react' import type { ForwardedRef } from 'react' import { useForm, FormProvider } from 'react-hook-form' import type { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form' import { ServerErrorsContext } from './ServerErrorsContext.js' export interface FormProps< TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues = TFieldValues, > extends Omit, 'onSubmit'> { error?: any /** * The methods returned by `useForm`. * This prop is only necessary if you've called `useForm` yourself to get * access to one of its functions, like `reset`. * * @example * * ```typescript * const formMethods = useForm() * * const onSubmit = (data: FormData) => { * sendDataToServer(data) * formMethods.reset() * } * * return ( *
* ) * ``` */ formMethods?: UseFormReturn /** * Configures how React Hook Form performs validation, among other things. * * @example * * ```jsx * * ``` * * @see {@link https://react-hook-form.com/docs/useform} */ config?: UseFormProps onSubmit?: ( value: TTransformedValues, event?: React.BaseSyntheticEvent, ) => void } /** * Renders a `` with the required context. */ function FormInner< TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues = TFieldValues, >( { config, error: errorProps, formMethods: propFormMethods, onSubmit, children, ...rest }: FormProps, ref: ForwardedRef, ) { const hookFormMethods = useForm( config, ) const formMethods = propFormMethods || hookFormMethods return ( onSubmit?.(data, event), )} > {children} ) } // Sorry about the `as` type assertion (type cast) here. Normally I'd redeclare // forwardRef to only return a plain function, allowing us to use TypeScript's // Higher-order Function Type Inference. But that gives us problems with the // ForwardRefExoticComponent type we use for our InputComponents. So instead // of changing that type (because it's correct) I use a type assertion here. // forwardRef is notoriously difficult to use with UI component libs. // Chakra-UI also says: // > To be honest, the forwardRef type is quite complex [...] I'd recommend // > that you cast the type // https://github.com/chakra-ui/chakra-ui/issues/4528#issuecomment-902566185 export const Form = forwardRef(FormInner) as < TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues = TFieldValues, >( props: FormProps & React.RefAttributes, ) => React.ReactElement | null