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,109 @@
|
||||
import type { SerializedEditorState } from '@payloadcms/richtext-lexical/lexical'
|
||||
import type { JSXConvertersFunction } from '@payloadcms/richtext-lexical/react'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import type { ResolveLinkHrefOptions } from '../links/resolveLinkHref.js'
|
||||
import type { BuilderFormClassNames } from './BuilderForm.js'
|
||||
import type { BuilderField, BuilderFormDoc, BuilderFormTexts } from './builderTypes.js'
|
||||
import type { SubmitActionResult } from './createSubmitAction.js'
|
||||
|
||||
import { resolveLinkHref, safeHref } from '../links/resolveLinkHref.js'
|
||||
import { RenderRichText } from '../richtext/RenderRichText.js'
|
||||
import { BuilderForm } from './BuilderForm.js'
|
||||
|
||||
/** The action from createSubmitAction — `sendForm(formId, locale, values, token?)`. */
|
||||
export type SendFormAction = (
|
||||
formId: string,
|
||||
locale: string,
|
||||
values: Record<string, unknown>,
|
||||
turnstileToken?: string,
|
||||
) => Promise<SubmitActionResult>
|
||||
|
||||
export type RenderFormProps = {
|
||||
/** The project's `sendForm` server action (createSubmitAction). */
|
||||
action: SendFormAction
|
||||
classNames?: BuilderFormClassNames
|
||||
/** Rich text styling for message blocks and the confirmation (same as RenderRichText). */
|
||||
converters?: JSXConvertersFunction
|
||||
/** The `form` relationship of the block, populated (page query depth ≥ 1). An id or null renders nothing. */
|
||||
form: BuilderFormDoc | null | number | string | undefined
|
||||
/**
|
||||
* For redirect forms pointing at a page: `{ config: i18nConfig, homeSlug }`
|
||||
* (locale is added). Without it only a custom URL redirect works.
|
||||
*/
|
||||
linkOptions?: Omit<ResolveLinkHrefOptions, 'locale'>
|
||||
locale: string
|
||||
requiredMark?: ReactNode
|
||||
richTextClassName?: string
|
||||
/** Notifications → form texts in this locale (getNotificationTexts(...).form). */
|
||||
texts: BuilderFormTexts
|
||||
turnstileTheme?: 'auto' | 'dark' | 'light'
|
||||
}
|
||||
|
||||
/** Only what the browser needs. The Local API reads with overrideAccess, so the
|
||||
* document still carries Emails (recipient addresses) — those never leave the server. */
|
||||
function publicForm(form: BuilderFormDoc): BuilderFormDoc {
|
||||
return {
|
||||
id: form.id,
|
||||
analytics: form.analytics,
|
||||
confirmationType: form.confirmationType,
|
||||
fields: (form.fields ?? []).map(({ message: _message, ...field }: BuilderField) => field),
|
||||
redirect: form.redirect?.type === 'custom' || form.redirect?.url ? { url: safeHref(form.redirect?.url) } : null,
|
||||
submitButtonLabel: form.submitButtonLabel,
|
||||
title: form.title,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A panel form inside a block, in one line — server part of BuilderForm:
|
||||
*
|
||||
* <RenderForm form={form} action={sendForm} locale={locale} texts={formTexts} classNames={formClasses} />
|
||||
*
|
||||
* Renders `message` blocks and the confirmation with RenderRichText, binds the
|
||||
* action to the form and locale, resolves the redirect target, and passes the
|
||||
* client only the public part of the form (never the Emails tab).
|
||||
*/
|
||||
export function RenderForm({
|
||||
action,
|
||||
classNames,
|
||||
converters,
|
||||
form,
|
||||
linkOptions,
|
||||
locale,
|
||||
requiredMark,
|
||||
richTextClassName,
|
||||
texts,
|
||||
turnstileTheme,
|
||||
}: RenderFormProps) {
|
||||
if (!form || typeof form !== 'object') {return null} // never a placeholder form
|
||||
|
||||
const rich = (data: unknown) =>
|
||||
data ? <RenderRichText className={richTextClassName} converters={converters} data={data as SerializedEditorState} /> : null
|
||||
|
||||
const messages: Record<number, ReactNode> = {}
|
||||
form.fields?.forEach((field, i) => {
|
||||
if (field.blockType === 'message' && field.message) {messages[i] = rich(field.message)}
|
||||
})
|
||||
|
||||
const page = form.redirect?.type === 'reference' ? (form.redirect.reference as { value?: unknown } | null)?.value : undefined
|
||||
const redirectHref =
|
||||
form.confirmationType === 'redirect'
|
||||
? page && linkOptions
|
||||
? resolveLinkHref({ type: 'internal', page }, { ...linkOptions, locale })
|
||||
: safeHref(form.redirect?.url)
|
||||
: null
|
||||
|
||||
return (
|
||||
<BuilderForm
|
||||
action={action.bind(null, String(form.id), locale)}
|
||||
classNames={classNames}
|
||||
confirmation={rich(form.confirmationMessage)}
|
||||
form={publicForm(form)}
|
||||
messages={messages}
|
||||
redirectHref={redirectHref}
|
||||
requiredMark={requiredMark}
|
||||
texts={texts}
|
||||
turnstileTheme={turnstileTheme}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user