'use client' import type { ReactNode } from 'react' import { useEffect, useRef } from 'react' import type { TurnstileTheme } from '../turnstile/Turnstile.js' import type { BuilderField } from './builderTypes.js' import type { BuilderFormApi, UseBuilderFormOptions } from './useBuilderForm.js' import { formatFileSize } from './attachments/fileTypes.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' | 'file' | 'fileItem' | 'fileList' | 'fileRemove' | 'fixed' | 'form' | 'formError' | 'help' | '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 /** Space kept for the Turnstile widget before it is drawn (default 73 px; 0 for an invisible widget). */ turnstileReserveHeight?: number /** Widget theme. Default 'page' — follows the site's light/dark theme. */ turnstileTheme?: TurnstileTheme } & UseBuilderFormOptions /** A page-supplied value (always a string from `values`) as text. */ const fixedText = (value: unknown) => (typeof value === 'string' ? value : '') 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, turnstileReserveHeight, turnstileTheme, ...options }: BuilderFormProps) { const f = useBuilderForm(options) const stepRef = useRef(null) const prevStep = useRef(f.stepIndex) // Moving between steps: focus the new step, so keyboard and screen-reader users land on it. // Compare with the previous step, not a "first render" flag: React re-runs effects // without a remount (Next restoring a visited page from , StrictMode), // and a stale flag would focus the first field and scroll the page to the form. useEffect(() => { if (prevStep.current === f.stepIndex) {return} prevStep.current = f.stepIndex 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]}
} if (f.isFixed(field)) { // Filled by the page (`values`): visible, not editable, sent with the form. const id = f.fieldId(field) return ( <> {field.label} {fixedText(f.values[field.name ?? ''])} ) } 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 'file': { // A native file input: keyboard, touch and screen readers work as everywhere else. const props = f.getFileProps(field) const files = f.getFiles(field) const remove = options.texts.fileRemove ?? 'Remove {file}' return ( <> {field.help ?

{field.help}

: null}
    {files.map((file, i) => (
  • {file.name} ({formatFileSize(file.size)}){' '}
  • ))}
{errorOf(field)} ) } case 'select': { const props = f.getSelectProps(field) return ( <> {errorOf(field)} ) } case 'textarea': { const props = f.getTextareaProps(field) return ( <>