import { CheckboxField } from '@components/common/form/CheckboxField.js'; import { EmailField } from '@components/common/form/EmailField.js'; import { Form, useFormContext } from '@components/common/form/Form.js'; import { InputField } from '@components/common/form/InputField.js'; import { TelField } from '@components/common/form/TelField.js'; import { TextareaField } from '@components/common/form/TextareaField.js'; import { Editable, isPageBuilderActive, useWidgetUid } from '@components/common/page-builder/index.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import { useForm } from 'react-hook-form'; export interface ContactFormProps { contactFormWidget: { title: string | null; subtitle: string | null; submitLabel: string | null; successMessage: string | null; showPhone: boolean | null; showSubject: boolean | null; consentEnabled: boolean | null; consentText: string | null; }; } /** * `widget_uuid` tells the API which instance produced the message; `website` is * the honeypot. Both ride in the form state so they are part of the same JSON * body `
` posts — no separate payload assembly. */ function HiddenFields({ widgetUid }: { widgetUid: string | null }) { const { register } = useFormContext(); return ( <> ); } export default function ContactForm({ contactFormWidget }: ContactFormProps) { const { title, subtitle, submitLabel, successMessage, showPhone, showSubject, consentEnabled, consentText } = contactFormWidget; // The widget instance uuid. Available on the live storefront too — Area tags // every widget with its uuid and WidgetChrome provides the context even // outside the page-builder iframe. const widgetUid = useWidgetUid(); // Owned here rather than left to so the fields can be cleared after a // successful send. `shouldUnregister` keeps hidden optional fields out of the // payload entirely when the merchant has toggled them off. const form = useForm({ shouldUnregister: true, shouldFocusError: false }); const [status, setStatus] = React.useState<{ type: 'success' | 'error'; text: string; } | null>(null); // Inside the editor the form is a design surface, not a live one — a // merchandiser tabbing through their own layout should not mail the store. const [isClient, setIsClient] = React.useState(false); React.useEffect(() => { setIsClient(true); }, []); const inPageBuilder = isClient && isPageBuilderActive(); return (
{title && ( {title} )} {subtitle && ( {subtitle} )} {/* Announced when it appears, without stealing focus. */}
{status && (
{status.text}
)}
undefined : undefined} onSuccess={() => { setStatus({ type: 'success', text: successMessage || _('Thanks! Your message has been sent.') }); form.reset(); }} onError={(error) => { setStatus({ type: 'error', text: error || _('Something went wrong. Please try again.') }); }} > {/* wraps children in a
, so the spacing wrapper has to live in here rather than on the form element. */}
{/* Name and email share a row on tablet and up; they are short fields and pairing them keeps the form from reading as a long column. They stack below `md`. */}
{showPhone && ( )} {showSubject && ( )} {consentEnabled && ( )}
); } export const query = ` query Query( $title: String $subtitle: String $submitLabel: String $successMessage: String $showPhone: Boolean $showSubject: Boolean $consentEnabled: Boolean $consentText: String ) { contactFormWidget( title: $title subtitle: $subtitle submitLabel: $submitLabel successMessage: $successMessage showPhone: $showPhone showSubject: $showSubject consentEnabled: $consentEnabled consentText: $consentText ) { title subtitle submitLabel successMessage showPhone showSubject consentEnabled consentText } } `; export const variables = `{ title: getWidgetSetting("title"), subtitle: getWidgetSetting("subtitle"), submitLabel: getWidgetSetting("submitLabel"), successMessage: getWidgetSetting("successMessage"), showPhone: getWidgetSetting("showPhone"), showSubject: getWidgetSetting("showSubject"), consentEnabled: getWidgetSetting("consentEnabled"), consentText: getWidgetSetting("consentText") }`;