Theme switcher, table of contents, missing-page redirect, form placeholders and time field; audit and revalidate fixes
- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate - BuilderForm focuses only on a real step change (no scroll to the form on remount) - mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images - buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option - Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name) - ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme - extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField - createPageRoute onMissing: 'redirectHome' - Form builder: localized placeholder on text/email/textarea/number, new time field - ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
23e033d7d7
commit
faa4ad679c
@@ -4,6 +4,7 @@ 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'
|
||||
|
||||
@@ -48,7 +49,8 @@ export type BuilderFormProps = {
|
||||
renderField?: (args: BuilderFieldRenderArgs) => ReactNode | undefined
|
||||
/** Appended to required labels, e.g. "*". Default: nothing. */
|
||||
requiredMark?: ReactNode
|
||||
turnstileTheme?: 'auto' | 'dark' | 'light'
|
||||
/** 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. */
|
||||
@@ -77,14 +79,15 @@ export function BuilderForm({
|
||||
}: BuilderFormProps) {
|
||||
const f = useBuilderForm(options)
|
||||
const stepRef = useRef<HTMLDivElement>(null)
|
||||
const firstRender = useRef(true)
|
||||
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 <Activity>, StrictMode),
|
||||
// and a stale flag would focus the first field and scroll the page to the form.
|
||||
useEffect(() => {
|
||||
if (firstRender.current) {
|
||||
firstRender.current = false
|
||||
return
|
||||
}
|
||||
if (prevStep.current === f.stepIndex) {return}
|
||||
prevStep.current = f.stepIndex
|
||||
const el = stepRef.current
|
||||
const target = el?.querySelector<HTMLElement>('[aria-invalid="true"], input, select, textarea') ?? el
|
||||
target?.focus()
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { JSXConvertersFunction } from '@payloadcms/richtext-lexical/react'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import type { ResolveLinkHrefOptions } from '../links/resolveLinkHref.js'
|
||||
import type { TurnstileTheme } from '../turnstile/Turnstile.js'
|
||||
import type { BuilderFormClassNames } from './BuilderForm.js'
|
||||
import type { BuilderField, BuilderFormDoc, BuilderFormTexts } from './builderTypes.js'
|
||||
import type { SubmitActionResult } from './createSubmitAction.js'
|
||||
@@ -37,7 +38,8 @@ export type RenderFormProps = {
|
||||
richTextClassName?: string
|
||||
/** Notifications → form texts in this locale (getNotificationTexts(...).form). */
|
||||
texts: BuilderFormTexts
|
||||
turnstileTheme?: 'auto' | 'dark' | 'light'
|
||||
/** Widget theme. Default 'page' — follows the site's light/dark theme. */
|
||||
turnstileTheme?: TurnstileTheme
|
||||
/** Values of fields the page already knows, by field name — read-only for the visitor, sent with the form. */
|
||||
values?: Record<string, string>
|
||||
}
|
||||
|
||||
@@ -8,6 +8,10 @@ import type { Block, Field } from 'payload'
|
||||
* Carries no value.
|
||||
* - `checkboxGroup` — a list of options where the visitor ticks several
|
||||
* ("Services you're interested in"). Value: string[].
|
||||
* - `time` — a time of day (<input type="time">). Value: "HH:MM".
|
||||
*
|
||||
* Built-in text · email · textarea · number get a localized "Placeholder"
|
||||
* (withPlaceholder) — the hint inside the empty field, e.g. "e.g. 10:00–12:00".
|
||||
*
|
||||
* Plain English labels (the panel is EN). Rendered by BuilderForm /
|
||||
* useBuilderForm, validated by submitForm like every other field.
|
||||
@@ -61,6 +65,29 @@ export function withShowWhen<B extends Block>(block: B): B {
|
||||
return { ...block, fields: [...block.fields, showWhenField] }
|
||||
}
|
||||
|
||||
/** Hint inside the empty field. Localized — it is visible text. */
|
||||
export const placeholderField: Field = {
|
||||
name: 'placeholder',
|
||||
type: 'text',
|
||||
admin: { description: 'Hint shown inside the empty field, e.g. "e.g. 10:00–12:00". Empty → none.' },
|
||||
label: 'Placeholder',
|
||||
localized: true,
|
||||
}
|
||||
|
||||
/** Adds a localized "Placeholder" to a block (built-in text, email, textarea, number). */
|
||||
export function withPlaceholder<B extends Block>(block: B): B {
|
||||
return { ...block, fields: [...block.fields, placeholderField] }
|
||||
}
|
||||
|
||||
const timeBase: Block = {
|
||||
slug: 'time',
|
||||
fields: [
|
||||
{ type: 'row', fields: [name, label] },
|
||||
{ type: 'row', fields: [width, required] },
|
||||
],
|
||||
labels: { plural: 'Times', singular: 'Time' },
|
||||
}
|
||||
|
||||
const stepBase: Block = {
|
||||
slug: 'step',
|
||||
fields: [
|
||||
@@ -112,3 +139,4 @@ const checkboxGroupBase: Block = {
|
||||
|
||||
export const stepBlock: Block = withShowWhen(stepBase)
|
||||
export const checkboxGroupBlock: Block = withShowWhen(checkboxGroupBase)
|
||||
export const timeBlock: Block = withShowWhen(timeBase)
|
||||
|
||||
@@ -15,7 +15,7 @@ export type FieldCondition = { field?: null | string; values?: null | string }
|
||||
|
||||
export type BuilderField = {
|
||||
blockName?: null | string
|
||||
/** text · textarea · email · number · date · select · radio · checkbox · checkboxGroup · country · state · message · step · a project's own type */
|
||||
/** text · textarea · email · number · date · time · select · radio · checkbox · checkboxGroup · country · state · message · step · a project's own type */
|
||||
blockType: string
|
||||
defaultValue?: unknown
|
||||
/** step */
|
||||
|
||||
@@ -6,7 +6,7 @@ import { fields as builtinBlocks, formBuilderPlugin } from '@payloadcms/plugin-f
|
||||
import type { FormsOption } from './types.js'
|
||||
|
||||
import { hasMinimumRole, isAdmin } from '../access/predicates.js'
|
||||
import { checkboxGroupBlock, stepBlock, withShowWhen } from './builderBlocks.js'
|
||||
import { checkboxGroupBlock, stepBlock, timeBlock, withPlaceholder, withShowWhen } from './builderBlocks.js'
|
||||
import { buildEmailLayoutHook } from './emailLayout.js'
|
||||
|
||||
type AccessArgs = { req: { user?: unknown } }
|
||||
@@ -14,6 +14,8 @@ type AccessArgs = { req: { user?: unknown } }
|
||||
/** A built-in form-builder block with "Show only when" added (full block — form-builder merges arrays by replacing). */
|
||||
const builtin = (key: 'checkbox' | 'date' | 'email' | 'message' | 'number' | 'radio' | 'select' | 'text' | 'textarea') =>
|
||||
withShowWhen(builtinBlocks[key] as Block)
|
||||
/** Text-like built-ins also get a localized "Placeholder" (select already has one). */
|
||||
const withHint = (key: 'email' | 'number' | 'text' | 'textarea') => withShowWhen(withPlaceholder(builtinBlocks[key] as Block))
|
||||
|
||||
/** REST/GraphQL: only logged-in users. Local API (submitForm) is unaffected. */
|
||||
const submissionsCreateAccess = ({ req }: AccessArgs) => Boolean(req.user)
|
||||
@@ -68,7 +70,7 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
: undefined
|
||||
return formBuilderPlugin({
|
||||
...(beforeEmail ? { beforeEmail } : {}),
|
||||
// Built-in types + ipal's `step` and `checkboxGroup` (builderBlocks). All are
|
||||
// Built-in types + ipal's `step`, `checkboxGroup` and `time` (builderBlocks). All are
|
||||
// panel choices only — a form uses what the editor adds, nothing changes on
|
||||
// existing forms. Switch any off with forms.fields.<type>: false.
|
||||
fields: {
|
||||
@@ -76,18 +78,19 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
checkbox: builtin('checkbox'),
|
||||
checkboxGroup: forms.fields?.checkboxGroup === false ? false : checkboxGroupBlock,
|
||||
date: builtin('date'),
|
||||
email: builtin('email'),
|
||||
email: withHint('email'),
|
||||
message: builtin('message'),
|
||||
number: builtin('number'),
|
||||
number: withHint('number'),
|
||||
payment: false,
|
||||
radio: builtin('radio'),
|
||||
select: builtin('select'),
|
||||
step: forms.fields?.step === false ? false : stepBlock,
|
||||
text: builtin('text'),
|
||||
textarea: builtin('textarea'),
|
||||
text: withHint('text'),
|
||||
textarea: withHint('textarea'),
|
||||
time: forms.fields?.time === false ? false : timeBlock,
|
||||
...Object.fromEntries(
|
||||
// `true` keeps ipal's block (with "Show only when"); false / an object override it
|
||||
Object.entries(forms.fields ?? {}).filter(([k, v]) => k !== 'checkboxGroup' && k !== 'step' && v !== true),
|
||||
Object.entries(forms.fields ?? {}).filter(([k, v]) => k !== 'checkboxGroup' && k !== 'step' && k !== 'time' && v !== true),
|
||||
),
|
||||
},
|
||||
...(forms.redirectRelationships
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Panel field types: step-by-step forms + multiple choice (enabled by default).
|
||||
export { checkboxGroupBlock, stepBlock } from './builderBlocks.js'
|
||||
export { checkboxGroupBlock, placeholderField, stepBlock, timeBlock, withPlaceholder } from './builderBlocks.js'
|
||||
// Pure panel-form logic (steps, values, validation) — the engine of useBuilderForm.
|
||||
export { initialBuilderValues, inputFields, splitSteps, stepOfField, validateBuilderFields, visibleFields } from './builderLogic.js'
|
||||
export type { BuilderField, BuilderFormDoc, BuilderFormTexts, BuilderStep, FieldCondition } from './builderTypes.js'
|
||||
|
||||
@@ -62,6 +62,8 @@ export type FormsOption = {
|
||||
step?: boolean
|
||||
text?: boolean
|
||||
textarea?: boolean
|
||||
/** Time of day, <input type="time"> (ipal). Default on. */
|
||||
time?: boolean
|
||||
}
|
||||
/**
|
||||
* Override the forms collection. The plugin stays opinion-free about what a
|
||||
|
||||
@@ -189,7 +189,7 @@ export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref
|
||||
return typeof value === 'string' ? value : value == null ? '' : String(value as number)
|
||||
}, [f.values])
|
||||
|
||||
/** Everything an <input> needs, by field type: text · email · number · date · tel · checkbox. */
|
||||
/** Everything an <input> needs, by field type: text · email · number · date · time · tel · checkbox. */
|
||||
const getInputProps = useCallback(
|
||||
(field: BuilderField) => {
|
||||
const name = field.name ?? ''
|
||||
@@ -201,6 +201,7 @@ export function useBuilderForm({ action, form, navigate, onSuccess, redirectHref
|
||||
field.blockType === 'email' ? 'email'
|
||||
: field.blockType === 'number' ? 'number'
|
||||
: field.blockType === 'date' ? 'date'
|
||||
: field.blockType === 'time' ? 'time'
|
||||
: base.autoComplete === 'tel' ? 'tel'
|
||||
: 'text'
|
||||
return {
|
||||
|
||||
@@ -46,6 +46,9 @@ export type FormValidationResult =
|
||||
/** Field block types that don't carry a submittable value. */
|
||||
const NON_DATA_BLOCKS = new Set(['message', 'step'])
|
||||
|
||||
/** HH:MM or HH:MM:SS (a time input), 00:00–23:59. */
|
||||
const TIME = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/
|
||||
|
||||
/** YYYY-MM-DD (a date input) or a full ISO timestamp. */
|
||||
const DATE = /^\d{4}-\d{2}-\d{2}(?:T[\d:.]+(?:Z|[+-]\d{2}:?\d{2})?)?$/
|
||||
|
||||
@@ -123,6 +126,8 @@ function checkValue(field: FormField, value: unknown): Checked {
|
||||
}
|
||||
case 'textarea':
|
||||
return typeof value === 'string' ? { value: value.replace(CONTROL, '') } : { error: 'invalid_format' }
|
||||
case 'time':
|
||||
return typeof value === 'string' && TIME.test(value.trim()) ? { value: value.trim() } : { error: 'invalid_format' }
|
||||
default:
|
||||
// text, country, state and custom blocks: one line of plain text.
|
||||
if (typeof value === 'number') {return { value }}
|
||||
|
||||
Reference in New Issue
Block a user