71 lines
3.2 KiB
TypeScript
71 lines
3.2 KiB
TypeScript
/**
|
|
* 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<T> = Partial<Record<keyof T, string>>;
|
|
/**
|
|
* 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<T> = (values: T, turnstileToken?: string) => Promise<{
|
|
success: boolean;
|
|
message?: string;
|
|
}>;
|
|
/** Turns the current values into an error map. Empty map = valid. */
|
|
export type Validator<T> = (values: T) => FormErrors<T>;
|
|
type UseFormOptions<T extends Record<string, unknown>> = {
|
|
/** Defines the field set AND the reset state. Any shape you like. */
|
|
initialValues: T;
|
|
submitAction: FormSubmitAction<T>;
|
|
/** Project-owned validation. Compose one with `buildValidator` + rules, or write your own. */
|
|
validate?: Validator<T>;
|
|
/** 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),
|
|
* })
|
|
* <input value={f.values.email}
|
|
* onChange={(e) => f.setField('email', e.target.value)}
|
|
* className={f.errors.email ? 'border-red-500' : '…'} />
|
|
* <form onSubmit={f.handleSubmit}>…</form>
|
|
*/
|
|
export declare function useForm<T extends Record<string, unknown>>(options: UseFormOptions<T>): {
|
|
values: T;
|
|
errors: Partial<Record<keyof T, string>>;
|
|
isSubmitting: boolean;
|
|
isSuccess: boolean;
|
|
errorMessage: string | null;
|
|
turnstileToken: string | null;
|
|
setField: <K extends keyof T>(key: K, value: T[K]) => void;
|
|
setValues: import("react").Dispatch<import("react").SetStateAction<T>>;
|
|
setErrors: import("react").Dispatch<import("react").SetStateAction<Partial<Record<keyof T, string>>>>;
|
|
setTurnstileToken: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
|
handleSubmit: (e?: {
|
|
preventDefault?: () => void;
|
|
}) => Promise<void>;
|
|
reset: () => void;
|
|
};
|
|
export {};
|