Import instead of code: panel forms (steps, choice lists, dates), layout, page route, GEO routes, links, section fields
- Forms: step and checkboxGroup field types (radio, date on), server validation for them, useBuilderForm (logic) + BuilderForm (unstyled default) + RenderForm (/rsc: rich text, bound action, public part of the form only — the Emails tab never reaches the browser) - Notifications: next / back / stepOf texts - createContentHelpers: getSiteShell, getFormTexts, getLinkOptions, resolveLink, resolveNav, resolvePageRedirect, one-line route handlers (llms, llms-full, md, mcp, health, indexnow, manifest) - /rsc: SiteProviders, createPageRoute, createNotFoundPage - resolveLink / resolveNav / homeSlugFrom, buildSectionHeaderField / buildSectionSettingsField - Dev sandbox frontend built on these imports; tests (unit + real Payload) Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
'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<number, ReactNode>
|
||||
/**
|
||||
* 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<HTMLDivElement>(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<HTMLElement>('[aria-invalid="true"], input, select, textarea') ?? el
|
||||
target?.focus()
|
||||
}, [f.stepIndex])
|
||||
|
||||
if (f.isSuccess && options.form.confirmationType !== 'redirect') {
|
||||
return (
|
||||
<div className={cn.success} data-ipal="form-success" role="status">
|
||||
{confirmation ?? f.resultMessage ?? options.texts.success}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const label = (field: BuilderField) => (
|
||||
<>
|
||||
{field.label}
|
||||
{field.required && requiredMark ? <span aria-hidden className={cn.required}>{requiredMark}</span> : null}
|
||||
</>
|
||||
)
|
||||
const errorOf = (field: BuilderField) => {
|
||||
const error = field.name ? f.errors[field.name] : undefined
|
||||
return error ? <p className={cn.error} id={f.errorId(field)}>{error}</p> : null
|
||||
}
|
||||
|
||||
const defaultField = (field: BuilderField, index: number): ReactNode => {
|
||||
if (field.blockType === 'message') {return <div className={cn.message}>{messages[index]}</div>}
|
||||
switch (field.blockType) {
|
||||
case 'checkbox': {
|
||||
const props = f.getInputProps(field)
|
||||
return (
|
||||
<>
|
||||
<label className={cn.checkboxLabel} htmlFor={props.id}>
|
||||
<input {...props} className={cn.checkbox} />
|
||||
<span>{label(field)}</span>
|
||||
</label>
|
||||
{errorOf(field)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
case 'checkboxGroup':
|
||||
case 'radio':
|
||||
return (
|
||||
<fieldset
|
||||
aria-describedby={f.getFieldProps(field)['aria-describedby']}
|
||||
aria-invalid={f.getFieldProps(field)['aria-invalid']}
|
||||
className={cn.choices}
|
||||
id={f.fieldId(field)}
|
||||
>
|
||||
<legend className={cn.legend}>{label(field)}</legend>
|
||||
{(field.options ?? []).map((option, i) => {
|
||||
const o = f.getOptionProps(field, option, i)
|
||||
return (
|
||||
<label className={cn.choice} htmlFor={o.id} key={option.value}>
|
||||
<input {...o} className={cn.choiceInput} />
|
||||
<span className={cn.choiceLabel}>{option.label ?? option.value}</span>
|
||||
</label>
|
||||
)
|
||||
})}
|
||||
{errorOf(field)}
|
||||
</fieldset>
|
||||
)
|
||||
case 'select': {
|
||||
const props = f.getSelectProps(field)
|
||||
return (
|
||||
<>
|
||||
<label className={cn.label} htmlFor={props.id}>{label(field)}</label>
|
||||
<select {...props} className={cn.select}>
|
||||
<option value="">{field.placeholder ?? ''}</option>
|
||||
{(field.options ?? []).map((o) => <option key={o.value} value={o.value}>{o.label ?? o.value}</option>)}
|
||||
</select>
|
||||
{errorOf(field)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
case 'textarea': {
|
||||
const props = f.getTextareaProps(field)
|
||||
return (
|
||||
<>
|
||||
<label className={cn.label} htmlFor={props.id}>{label(field)}</label>
|
||||
<textarea {...props} className={cn.textarea} rows={5} />
|
||||
{errorOf(field)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
default: {
|
||||
const props = f.getInputProps(field)
|
||||
return (
|
||||
<>
|
||||
<label className={cn.label} htmlFor={props.id}>{label(field)}</label>
|
||||
<input {...props} className={cn.input} />
|
||||
{errorOf(field)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { step } = f
|
||||
const progress = options.texts.stepOf?.replace('{current}', String(f.stepIndex + 1)).replace('{total}', String(f.total))
|
||||
|
||||
return (
|
||||
<form className={cn.form} data-ipal="form" data-step={f.stepIndex + 1} noValidate onSubmit={f.submit}>
|
||||
<input {...honeypotInputProps()} />
|
||||
{f.total > 1 && progress ? <p aria-live="polite" className={cn.progress}>{progress}</p> : null}
|
||||
<div className={cn.step} data-ipal="form-step" ref={stepRef} tabIndex={-1}>
|
||||
{step.title ? <p className={cn.stepTitle}>{step.title}</p> : null}
|
||||
{step.description ? <p className={cn.stepDescription}>{step.description}</p> : null}
|
||||
<div className={cn.fields}>
|
||||
{step.fields.map(({ field, index }) => {
|
||||
const half = typeof field.width === 'number' && field.width <= 50
|
||||
const content = renderField?.({ defaultRender: () => defaultField(field, index), field, form: f, index }) ?? defaultField(field, index)
|
||||
return (
|
||||
<div
|
||||
className={cx(cn.field, half ? cn.fieldHalf : cn.fieldFull)}
|
||||
data-type={field.blockType}
|
||||
data-width={field.width ?? 100}
|
||||
key={field.id ?? `${field.blockType}-${index}`}
|
||||
>
|
||||
{content}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
{f.isLast ? <div className={cn.turnstile}><f.TurnstileWidget theme={turnstileTheme} /></div> : null}
|
||||
{f.errorMessage ? <p className={cn.formError} role="alert">{f.errorMessage}</p> : null}
|
||||
<div className={cn.actions}>
|
||||
{!f.isFirst ? <button className={cn.back} onClick={f.back} type="button">{options.texts.back ?? 'Back'}</button> : null}
|
||||
<button className={f.isLast ? cn.submit : (cn.next ?? cn.submit)} disabled={f.isSubmitting} type="submit">
|
||||
{f.isLast ? options.form.submitButtonLabel || 'Send' : options.texts.next ?? 'Next'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user