Added better support for GTM, improved accesibility
This commit is contained in:
Vendored
+30
-16
@@ -11,23 +11,37 @@ export type FormErrors<T> = Partial<Record<keyof T, string>>;
|
||||
* plus an optional `message`; richer shapes (SubmitFormResult) satisfy it.
|
||||
*/
|
||||
export type FormSubmitAction<T> = (values: T, turnstileToken?: string) => Promise<{
|
||||
success: boolean;
|
||||
message?: string;
|
||||
success: boolean;
|
||||
}>;
|
||||
/** 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>> = {
|
||||
/** Clear a field's error as soon as the user edits it. Default true. */
|
||||
clearErrorOnChange?: boolean;
|
||||
/** Generic error message shown when the action fails/throws with no message. */
|
||||
errorFallback?: string;
|
||||
/** Defines the field set AND the reset state. Any shape you like. */
|
||||
initialValues: T;
|
||||
/**
|
||||
* Called once after a SUCCESSFUL submit, with the submitted values and the
|
||||
* action's result. The place to fire analytics — e.g. a GTM `form_submit`
|
||||
* event so a Meta Pixel / GA "Lead" tag can trigger:
|
||||
*
|
||||
* onSuccess: (_r, v) => pushDataLayer('form_submit', { form_name: v.topic || 'kontakt' })
|
||||
*
|
||||
* Runs client-side after the server action resolves; errors thrown here are
|
||||
* swallowed so a broken analytics call can't blank out a successful submit.
|
||||
*/
|
||||
onSuccess?: (result: {
|
||||
message?: string;
|
||||
success: boolean;
|
||||
}, values: T) => void;
|
||||
/** Require a Turnstile token before submit (when the widget is shown). */
|
||||
requireTurnstile?: boolean;
|
||||
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,
|
||||
@@ -52,19 +66,19 @@ type UseFormOptions<T extends Record<string, unknown>> = {
|
||||
* <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>>;
|
||||
errors: Partial<Record<keyof T, string>>;
|
||||
handleSubmit: (e?: {
|
||||
preventDefault?: () => void;
|
||||
}) => Promise<void>;
|
||||
isSubmitting: boolean;
|
||||
isSuccess: boolean;
|
||||
reset: () => void;
|
||||
setErrors: import("react").Dispatch<import("react").SetStateAction<Partial<Record<keyof T, string>>>>;
|
||||
setField: <K extends keyof T>(key: K, value: T[K]) => void;
|
||||
setTurnstileToken: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
||||
setValues: import("react").Dispatch<import("react").SetStateAction<T>>;
|
||||
turnstileToken: string | null;
|
||||
values: T;
|
||||
};
|
||||
export {};
|
||||
|
||||
Reference in New Issue
Block a user