// src/components/forms/FormRenderer.tsx — CLIENT part: renders ANY form built in the panel. // Logic from ipal-kit, markup + classes are the project's (restyle freely, keep the wiring). 'use client' import { useEffect, useMemo, type ReactNode } from 'react' import { useRouter } from 'next/navigation' import { useForm, buildValidator, required, email, checked, honeypotInputProps, useTurnstile, formAnalyticsOnSuccess, type FormAnalyticsConfig, } from '@intecion/ipal-kit/client' type Option = { label?: string | null; value: string } export type BuilderField = { blockType: 'text' | 'textarea' | 'email' | 'number' | 'select' | 'checkbox' | 'country' | 'state' | 'message' | string name?: string | null label?: string | null required?: boolean | null width?: number | null defaultValue?: unknown options?: Option[] | null message?: unknown } export type BuilderForm = { id: string | number fields?: BuilderField[] | null submitButtonLabel?: string | null confirmationType?: 'message' | 'redirect' | null confirmationMessage?: unknown redirect?: { url?: string | null } | null analytics?: FormAnalyticsConfig } export type FormAction = (values: Record, turnstileToken?: string) => Promise<{ success: boolean; message?: string }> type Props = { form: BuilderForm action: FormAction messages: Record confirmation: ReactNode /** Notifications → form.validation, may contain {field}. */ validationText: string } export function FormRenderer({ form, action, messages, confirmation, validationText }: Props) { const router = useRouter() const ts = useTurnstile() // needs in the layout (siteKey may be empty → disabled) const fields = (form.fields ?? []).filter((f) => f.blockType !== 'message' && f.name) const msg = (f: BuilderField) => validationText.replace('{field}', f.label || f.name || '') const initialValues = useMemo( () => Object.fromEntries(fields.map((f) => [f.name as string, f.blockType === 'checkbox' ? Boolean(f.defaultValue) : (f.defaultValue as string) ?? ''])), // eslint-disable-next-line react-hooks/exhaustive-deps [form.id], ) const validate = useMemo( () => buildValidator>( Object.fromEntries( fields.map((f) => { const rules = [] if (f.required) rules.push(f.blockType === 'checkbox' ? checked(msg(f)) : required(msg(f))) if (f.blockType === 'email') rules.push(email(msg(f))) return [f.name as string, rules] }), ), ), // eslint-disable-next-line react-hooks/exhaustive-deps [form.id, validationText], ) const f = useForm({ initialValues, validate, submitAction: action, requireTurnstile: ts.enabled, onSuccess: (result, values) => { formAnalyticsOnSuccess(form.analytics)(result, values) if (form.confirmationType === 'redirect' && form.redirect?.url) router.push(form.redirect.url) }, }) // Turnstile token lives in the provider; the form engine needs it too. useEffect(() => f.setTurnstileToken(ts.token), [ts.token]) // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { if (f.errorMessage) ts.reset() }, [f.errorMessage]) // eslint-disable-line react-hooks/exhaustive-deps if (f.isSuccess && form.confirmationType !== 'redirect') return
{confirmation}
return (
{(form.fields ?? []).map((field, i) => { if (field.blockType === 'message') return
{messages[i]}
const name = field.name as string const id = `f-${String(form.id)}-${name}` const error = f.errors[name] const span = field.width && field.width <= 50 ? 'col-span-12 md:col-span-6' : 'col-span-12' const common = { id, name, required: Boolean(field.required), 'aria-invalid': Boolean(error) || undefined, 'aria-describedby': error ? `${id}-err` : undefined, } const value = f.values[name] return (
{field.blockType === 'checkbox' ? ( ) : ( <> {field.blockType === 'textarea' ? (