@nestledjs/forms
Version:
A flexible React form library supporting both declarative and imperative usage patterns with TypeScript support
35 lines (34 loc) • 1.12 kB
TypeScript
import { UseFormReturn, FieldValues } from 'react-hook-form';
export declare const FormContext: import('react').Context<UseFormReturn<FieldValues> | null>;
/**
* Hook to access the form context and methods within a Form component.
*
* Provides access to react-hook-form's useForm return value, including
* form state, validation methods, and field registration.
*
* Must be used within a Form component.
*
* @template T - The type of the form values object
* @returns The form context object with methods like register, setValue, getValues, formState, etc.
* @throws Error if used outside of a Form component
*
* @example
* ```tsx
* function CustomField() {
* const form = useFormContext<{ username: string }>()
*
* return (
* <input
* {...form.register('username', { required: true })}
* className={form.formState.errors.username ? 'error' : ''}
* />
* )
* }
*
* // Usage within a Form
* <Form id="my-form" submit={handleSubmit}>
* <CustomField />
* </Form>
* ```
*/
export declare function useFormContext<T extends FieldValues = FieldValues>(): UseFormReturn<T>;