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,96 @@
|
||||
import type { BuilderField, BuilderFormTexts, BuilderStep } from './builderTypes.js'
|
||||
|
||||
import { NON_INPUT_TYPES } from './builderTypes.js'
|
||||
import { isEmail } from './validators.js'
|
||||
|
||||
/**
|
||||
* Pure logic of a panel form — steps, initial values, validation. No React, so
|
||||
* it's shared by useBuilderForm and tested on its own. The server re-checks
|
||||
* everything (validateSubmission); this is only for instant feedback.
|
||||
*/
|
||||
|
||||
/** Fields that hold a value (have a name and aren't message/step). */
|
||||
export function inputFields(fields: BuilderField[] | null | undefined): ({ name: string } & BuilderField)[] {
|
||||
return (fields ?? []).filter(
|
||||
(f): f is { name: string } & BuilderField => typeof f.name === 'string' && f.name !== '' && !NON_INPUT_TYPES.has(f.blockType),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the field list at `step` blocks. Fields before the first Step form
|
||||
* the first screen; a form without Steps is one screen.
|
||||
*/
|
||||
export function splitSteps(fields: BuilderField[] | null | undefined): BuilderStep[] {
|
||||
const steps: BuilderStep[] = []
|
||||
let current: BuilderStep | null = null
|
||||
;(fields ?? []).forEach((field, index) => {
|
||||
if (field.blockType === 'step') {
|
||||
current = { description: field.description ?? undefined, fields: [], index: steps.length, title: field.title ?? undefined }
|
||||
steps.push(current)
|
||||
return
|
||||
}
|
||||
if (!current) {
|
||||
current = { fields: [], index: 0 }
|
||||
steps.push(current)
|
||||
}
|
||||
current.fields.push({ field, index })
|
||||
})
|
||||
// Drop screens a trailing / doubled Step left empty.
|
||||
const kept = steps.filter((s) => s.fields.length > 0)
|
||||
return (kept.length ? kept : [{ fields: [], index: 0 }]).map((s, i) => ({ ...s, index: i }))
|
||||
}
|
||||
|
||||
/** Starting values: checkbox → boolean, checkboxGroup → string[], the rest → string. */
|
||||
export function initialBuilderValues(fields: BuilderField[] | null | undefined): Record<string, unknown> {
|
||||
return Object.fromEntries(
|
||||
inputFields(fields).map((f) => {
|
||||
if (f.blockType === 'checkbox') {return [f.name, f.defaultValue === true]}
|
||||
if (f.blockType === 'checkboxGroup') {return [f.name, Array.isArray(f.defaultValue) ? f.defaultValue : []]}
|
||||
const v = f.defaultValue
|
||||
return [f.name, typeof v === 'string' || typeof v === 'number' ? String(v) : '']
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const isBlank = (v: unknown) =>
|
||||
v == null || v === false || (typeof v === 'string' && v.trim() === '') || (Array.isArray(v) && v.length === 0)
|
||||
|
||||
/** Errors for the given fields (one message per field), empty object = valid. */
|
||||
export function validateBuilderFields(
|
||||
fields: BuilderField[],
|
||||
values: Record<string, unknown>,
|
||||
texts: Pick<BuilderFormTexts, 'validation'>,
|
||||
): Record<string, string> {
|
||||
const errors: Record<string, string> = {}
|
||||
for (const f of inputFields(fields)) {
|
||||
const v = values[f.name]
|
||||
const msg = texts.validation.replace('{field}', f.label || f.name)
|
||||
if (isBlank(v)) {
|
||||
if (f.required) {errors[f.name] = msg}
|
||||
continue
|
||||
}
|
||||
if (f.blockType === 'email' && !isEmail(v)) {errors[f.name] = msg}
|
||||
else if (f.blockType === 'number' && !Number.isFinite(Number(v))) {errors[f.name] = msg}
|
||||
else if (f.blockType === 'checkboxGroup' && Array.isArray(v)) {
|
||||
if ((f.minSelected && v.length < f.minSelected) || (f.maxSelected && v.length > f.maxSelected)) {errors[f.name] = msg}
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
/** Index of the step holding a field — to jump back to it after a failed submit. */
|
||||
export function stepOfField(steps: BuilderStep[], name: string): number {
|
||||
const i = steps.findIndex((s) => s.fields.some(({ field }) => field.name === name))
|
||||
return i < 0 ? 0 : i
|
||||
}
|
||||
|
||||
/** Browser autofill hint from the field type / a conventional name. */
|
||||
export function autoCompleteFor(field: BuilderField): string | undefined {
|
||||
if (field.blockType === 'email') {return 'email'}
|
||||
if (field.blockType === 'country') {return 'country-name'}
|
||||
const n = (field.name ?? '').toLowerCase()
|
||||
if (/^(?:full_?)?name$|^imie/.test(n)) {return 'name'}
|
||||
if (/phone|tel/.test(n)) {return 'tel'}
|
||||
if (/company|organi[sz]ation|firma/.test(n)) {return 'organization'}
|
||||
return undefined
|
||||
}
|
||||
Reference in New Issue
Block a user