/** * Error map: a field key → its error message. A field is "in error" iff it has * a key here, so `errors.email` is both the truthy flag (for a red border) AND * the message (to display). Empty object = valid. */ export type FormErrors = Partial>; /** * The project's server action that calls submitForm (server-only, holds the * Turnstile secret). This hook is a client hook, so it can't call submitForm * directly — the project injects its action. The result only needs `success` * plus an optional `message`; richer shapes (SubmitFormResult) satisfy it. */ export type FormSubmitAction = (values: T, turnstileToken?: string) => Promise<{ success: boolean; message?: string; }>; /** Turns the current values into an error map. Empty map = valid. */ export type Validator = (values: T) => FormErrors; type UseFormOptions> = { /** Defines the field set AND the reset state. Any shape you like. */ initialValues: T; submitAction: FormSubmitAction; /** Project-owned validation. Compose one with `buildValidator` + rules, or write your own. */ validate?: Validator; /** Require a Turnstile token before submit (when the widget is shown). */ requireTurnstile?: boolean; /** Generic error message shown when the action fails/throws with no message. */ errorFallback?: string; /** Clear a field's error as soon as the user edits it. Default true. */ clearErrorOnChange?: boolean; }; /** * Fully generic form engine: state, validation, Turnstile token, submit, * success/error — WITHOUT any markup. The project owns 100% of the look AND * the field set: there is no baked-in shape here. Replaces the identical * ~150 lines of form plumbing copied into every project. * * The field set comes from `initialValues`; the rules come from `validate` * (so dynamic/builder-driven forms just build both at runtime). For the * everyday contact form use the `useContactForm` preset instead — it wires * the standard fields and rules for you. * * const f = useForm({ * initialValues: { email: '', message: '' }, * submitAction, // your server action * validate: buildValidator({ email: [required(), email()], message: [required()] }), * requireTurnstile: Boolean(siteKey), * }) * f.setField('email', e.target.value)} * className={f.errors.email ? 'border-red-500' : '…'} /> *
…
*/ export declare function useForm>(options: UseFormOptions): { values: T; errors: Partial>; isSubmitting: boolean; isSuccess: boolean; errorMessage: string | null; turnstileToken: string | null; setField: (key: K, value: T[K]) => void; setValues: import("react").Dispatch>; setErrors: import("react").Dispatch>>>; setTurnstileToken: import("react").Dispatch>; handleSubmit: (e?: { preventDefault?: () => void; }) => Promise; reset: () => void; }; export {};