61 lines
2.6 KiB
TypeScript
61 lines
2.6 KiB
TypeScript
import { type FormSubmitAction } from './useForm.js';
|
|
/**
|
|
* Field values of the STANDARD contact form. This is a preset shape — projects
|
|
* that need a different field set (or dynamic builder fields) should use
|
|
* `useForm` directly instead, where the shape is fully open.
|
|
*/
|
|
export type ContactFormValues = {
|
|
consent: boolean;
|
|
email: string;
|
|
message: string;
|
|
name: string;
|
|
phone: string;
|
|
topic: string;
|
|
};
|
|
export type ContactFormErrors = Partial<Record<keyof ContactFormValues, string>>;
|
|
export type SubmitAction = FormSubmitAction<ContactFormValues>;
|
|
type UseContactFormOptions = {
|
|
/** Generic error message shown when the action fails/throws. */
|
|
errorFallback?: string;
|
|
initialTopic?: string;
|
|
/** Override the per-field error message (shown via `errors.<field>`). */
|
|
messages?: Partial<Record<keyof ContactFormValues, string>>;
|
|
/** Minimum message length. Default 10. */
|
|
minMessageLength?: number;
|
|
/** Whether consent is required (project passes true when consentText shown). */
|
|
requireConsent?: boolean;
|
|
/** Whether a topic must be chosen (project passes this when topics exist). */
|
|
requireTopic?: boolean;
|
|
/** Require a Turnstile token before submit (when the widget is shown). */
|
|
requireTurnstile?: boolean;
|
|
submitAction: SubmitAction;
|
|
};
|
|
/**
|
|
* Zero-config preset over `useForm` for the everyday contact form: standard
|
|
* fields (name/email/phone/topic/message/consent) and standard rules, wired
|
|
* for you. Markup and styling stay 100% in the project — this only removes the
|
|
* duplicated logic.
|
|
*
|
|
* const f = useContactForm({ submitAction, requireTurnstile: Boolean(siteKey),
|
|
* requireTopic: topics.length > 0, requireConsent: Boolean(consentText) })
|
|
*
|
|
* Need custom or dynamic fields? Drop the preset and call `useForm` + validators.
|
|
*/
|
|
export declare function useContactForm(options: UseContactFormOptions): {
|
|
values: ContactFormValues;
|
|
errors: Partial<Record<keyof ContactFormValues, string>>;
|
|
isSubmitting: boolean;
|
|
isSuccess: boolean;
|
|
errorMessage: string | null;
|
|
turnstileToken: string | null;
|
|
setField: <K extends keyof ContactFormValues>(key: K, value: ContactFormValues[K]) => void;
|
|
setValues: import("react").Dispatch<import("react").SetStateAction<ContactFormValues>>;
|
|
setErrors: import("react").Dispatch<import("react").SetStateAction<Partial<Record<keyof ContactFormValues, string>>>>;
|
|
setTurnstileToken: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
|
handleSubmit: (e?: {
|
|
preventDefault?: (() => void) | undefined;
|
|
} | undefined) => Promise<void>;
|
|
reset: () => void;
|
|
};
|
|
export {};
|