UNPKG

@explita/daily-toolset-form

Version:

A lightweight form toolkit for React built with developer ergonomics in mind. Includes a flexible Form component, useForm, useField, and useFormContext hooks for managing form state and validation with ease. Designed to simplify complex forms while remain

34 lines (33 loc) 1.69 kB
import { FormContextValue } from "../providers/form"; import z, { ZodObject } from "zod"; type CommonProps<TValues> = { errors?: Partial<Record<keyof TValues, string | undefined>>; }; /** * useFormContext is a hook that provides a form instance. The form instance * contains the form state and methods to update the form state. The hook is * generic and can be used with any schema type. * * The hook will use the schema and default values provided in the props to * update the form state. If the `schema` prop is provided, the hook will * validate the form state using the provided schema. If the `defaultValues` * prop is provided, the hook will update the form state with the provided * default values. * * The hook will also accept an `errors` prop, which should be an object with * the same shape as the form state. The hook will update the form errors with * the provided errors. * * The hook must be used within a Form component. * * @param {Object} [props] - The props for useFormContext. * @param {Schema} [props.schema] - The schema to use for form validation. * @param {DefaultValues} [props.defaultValues] - The default values for the form. * @param {Record<keyof DefaultValues, string | undefined>} [props.errors] - The errors for the form. * @returns {UseFormHook<DefaultValues>} The form instance. */ export declare function useFormContext<TSchema extends ZodObject | undefined = undefined, DefaultValues = TSchema extends undefined ? Record<string, any> : Partial<z.infer<TSchema>>>(props?: { schema?: TSchema; defaultValues?: Partial<DefaultValues>; } & CommonProps<DefaultValues>): FormContextValue<TSchema, DefaultValues>; export {};