'use client' import type { ReactNode } from 'react' import { useEffect, useRef } from 'react' import type { BuilderField } from './builderTypes.js' import type { BuilderFormApi, UseBuilderFormOptions } from './useBuilderForm.js' import { honeypotInputProps } from './honeypot.js' import { useBuilderForm } from './useBuilderForm.js' /** * Class names for every element — the plugin ships none. Set them once in the * project (e.g. src/components/forms/formClasses.ts) and every panel form looks * the same. Plain strings, so a server component can pass them. */ export type BuilderFormClassNames = Partial< Record< | 'actions' | 'back' | 'checkbox' | 'checkboxLabel' | 'choice' | 'choiceInput' | 'choiceLabel' | 'choices' | 'error' | 'field' | 'fieldFull' | 'fieldHalf' | 'fields' | 'form' | 'formError' | 'input' | 'label' | 'legend' | 'message' | 'next' | 'progress' | 'required' | 'select' | 'step' | 'stepDescription' | 'stepTitle' | 'submit' | 'success' | 'textarea' | 'turnstile', string > > export type BuilderFieldRenderArgs = { /** The default markup for this field — return it to keep it, or wrap it. */ defaultRender: () => ReactNode field: BuilderField form: BuilderFormApi /** Position in the panel's field list (key of `messages`). */ index: number } export type BuilderFormProps = { classNames?: BuilderFormClassNames /** Shown after success instead of the form (the form's confirmation message, rendered by RenderForm). */ confirmation?: ReactNode /** Rendered `message` blocks, keyed by their index in form.fields (RenderForm fills it). */ messages?: Record /** * Client-side only (use BuilderForm from a 'use client' file): replace the * markup of chosen fields — a project's own field type, option cards, a * slider. Return undefined to keep the default. */ renderField?: (args: BuilderFieldRenderArgs) => ReactNode | undefined /** Appended to required labels, e.g. "*". Default: nothing. */ requiredMark?: ReactNode turnstileTheme?: 'auto' | 'dark' | 'light' } & UseBuilderFormOptions const cx = (...c: (false | null | string | undefined)[]) => c.filter(Boolean).join(' ') || undefined /** * Renders ANY panel form — single screen or step-by-step, text, e-mail, number, * date, select, radio, checkbox, checkbox group, message blocks — with plain, * accessible HTML and no styles. Look: `classNames` (+ `data-*` hooks: * data-ipal="form", data-type, data-width, data-step). Different markup: * `renderField`, or your own component on useBuilderForm. * * In a block, use RenderForm (/rsc) — it renders the rich text, binds the * action and passes only the public part of the form to the browser. */ export function BuilderForm({ classNames: cn = {}, confirmation, messages = {}, renderField, requiredMark, turnstileTheme, ...options }: BuilderFormProps) { const f = useBuilderForm(options) const stepRef = useRef(null) const firstRender = useRef(true) // Moving between steps: focus the new step, so keyboard and screen-reader users land on it. useEffect(() => { if (firstRender.current) { firstRender.current = false return } const el = stepRef.current const target = el?.querySelector('[aria-invalid="true"], input, select, textarea') ?? el target?.focus() }, [f.stepIndex]) if (f.isSuccess && options.form.confirmationType !== 'redirect') { return (
{confirmation ?? f.resultMessage ?? options.texts.success}
) } const label = (field: BuilderField) => ( <> {field.label} {field.required && requiredMark ? {requiredMark} : null} ) const errorOf = (field: BuilderField) => { const error = field.name ? f.errors[field.name] : undefined return error ?

{error}

: null } const defaultField = (field: BuilderField, index: number): ReactNode => { if (field.blockType === 'message') {return
{messages[index]}
} switch (field.blockType) { case 'checkbox': { const props = f.getInputProps(field) return ( <> {errorOf(field)} ) } case 'checkboxGroup': case 'radio': return (
{label(field)} {(field.options ?? []).map((option, i) => { const o = f.getOptionProps(field, option, i) return ( ) })} {errorOf(field)}
) case 'select': { const props = f.getSelectProps(field) return ( <> {errorOf(field)} ) } case 'textarea': { const props = f.getTextareaProps(field) return ( <>