Forms: conditional fields and steps (Show only when)
Every form block (built-in, Checkbox group, Step, message) gets a showWhen group: visible only when another field has one of the given values. A hidden Step drops out with its fields; browser and server share visibleFields, so a hidden field is neither required nor stored. GDPR consent is always enforced. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import type { Field, Plugin } from 'payload'
|
||||
import type { Block, Field, Plugin } from 'payload'
|
||||
|
||||
import { formBuilderPlugin } from '@payloadcms/plugin-form-builder'
|
||||
import { fields as builtinBlocks, formBuilderPlugin } from '@payloadcms/plugin-form-builder'
|
||||
|
||||
import type { FormsOption } from './types.js'
|
||||
|
||||
import { hasMinimumRole, isAdmin } from '../access/predicates.js'
|
||||
import { checkboxGroupBlock, stepBlock } from './builderBlocks.js'
|
||||
import { checkboxGroupBlock, stepBlock, withShowWhen } from './builderBlocks.js'
|
||||
|
||||
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)
|
||||
|
||||
/** REST/GraphQL: only logged-in users. Local API (submitForm) is unaffected. */
|
||||
const submissionsCreateAccess = ({ req }: AccessArgs) => Boolean(req.user)
|
||||
/**
|
||||
@@ -57,20 +61,22 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
// panel choices only — a form uses what the editor adds, nothing changes on
|
||||
// existing forms. Switch any off with forms.fields.<type>: false.
|
||||
fields: {
|
||||
checkbox: true,
|
||||
// every block carries "Show only when" (showWhen) — conditional fields and steps
|
||||
checkbox: builtin('checkbox'),
|
||||
checkboxGroup: forms.fields?.checkboxGroup === false ? false : checkboxGroupBlock,
|
||||
date: true,
|
||||
email: true,
|
||||
message: true,
|
||||
number: true,
|
||||
date: builtin('date'),
|
||||
email: builtin('email'),
|
||||
message: builtin('message'),
|
||||
number: builtin('number'),
|
||||
payment: false,
|
||||
radio: true,
|
||||
select: true,
|
||||
radio: builtin('radio'),
|
||||
select: builtin('select'),
|
||||
step: forms.fields?.step === false ? false : stepBlock,
|
||||
text: true,
|
||||
textarea: true,
|
||||
text: builtin('text'),
|
||||
textarea: builtin('textarea'),
|
||||
...Object.fromEntries(
|
||||
Object.entries(forms.fields ?? {}).filter(([k]) => k !== 'checkboxGroup' && k !== 'step'),
|
||||
// `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),
|
||||
),
|
||||
},
|
||||
...(forms.redirectRelationships
|
||||
|
||||
Reference in New Issue
Block a user