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

33 lines (32 loc) 1.38 kB
import { z, ZodObject } from "zod"; import { UseFormHook } from "../types"; type SetValues<T> = (values: Partial<T>, options?: { overwrite?: boolean; }) => void; type SetErrors<T> = (errors: Partial<Record<keyof T, string>>) => void; type BaseCommonProps<TValues, TData = any, TError = Error> = { errors?: Partial<Record<keyof TValues, string>>; mode?: "controlled" | "uncontrolled"; resetOnSuccess?: boolean; errorParser?: (message: string) => string; onSubmit?: (values: TValues, formData: FormData) => Promise<TData>; onSuccess?: (data: Awaited<TData>, ctx: { reset: () => void; values: TValues; formData: FormData; }) => void; onError?: (error: TError, ctx: { setErrors: SetErrors<TValues>; setValues: SetValues<TValues>; }) => void; }; type CommonProps<TValues, TData, TError> = (BaseCommonProps<TValues, TData, TError> & { persistKey: string; }) | (BaseCommonProps<TValues, TData, TError> & { persistKey?: never; }); export declare function useForm<TSchema extends ZodObject | undefined = undefined, DefaultValues = TSchema extends undefined ? Record<string, any> : Partial<z.infer<TSchema>>, TData = any, TError = Error>(props?: { schema?: TSchema; defaultValues?: DefaultValues; } & CommonProps<DefaultValues, TData, TError>): UseFormHook<DefaultValues, TData>; export {};