Wizard

A form with more than one step is a wizard. The config decides everything about it: the steps and their order, which steps are skipped and how freely a visitor jumps between them. A view recipe draws the controls (Previous, Next and the submit button). The Registration wizard example has the files of both renderers; this page covers the config, and how to read and move the wizard from a component of your own.

Define the steps

view.$type names the recipe that draws the wizard, and each step lists its own elements. This checkout has three steps, and the company step is shown only when the buyer is a company. Both renderers draw the same config.

import type { FormConfig } from '@schema-engine/forms';

/**
 * A three-step checkout that both renderers draw.
 *
 * @remarks
 * `view.$type` names the wizard recipe, which draws Previous, Next and the submit button. The `company` step is
 * skipped unless the buyer is a company, and `navigation: 'linear'` lets a visitor jump only to steps already visited.
 */
export const checkout: FormConfig = {
	id: 'checkout',
	view: { $type: 'wizard' },
	navigation: 'linear',
	steps: [
		{
			id: 'contact',
			title: 'Contact',
			elements: [
				{
					$type: 'input',
					id: 'email',
					name: 'email',
					label: 'Email',
					inputType: 'email',
					rules: [{ $type: 'required', message: 'Enter your email' }],
				},
				{
					$type: 'select',
					id: 'buyer',
					name: 'buyer',
					label: 'Buying as',
					defaultValue: 'person',
					options: [
						{ value: 'person', label: 'A person' },
						{ value: 'company', label: 'A company' },
					],
				},
			],
		},
		{
			id: 'company',
			title: 'Company',
			constraints: [{ $type: 'show', field: 'buyer', operator: 'equals', value: 'company' }],
			elements: [
				{
					$type: 'input',
					id: 'vat',
					name: 'vat',
					label: 'VAT number',
					rules: [{ $type: 'required', message: 'Enter the VAT number' }],
				},
			],
		},
		{
			id: 'payment',
			title: 'Payment',
			elements: [
				{
					$type: 'input',
					id: 'card',
					name: 'card',
					label: 'Card number',
					rules: [{ $type: 'required', message: 'Enter the card number' }],
				},
			],
		},
	],
};

The config decides how the wizard behaves:

In the configEffect
navigation: 'linear'The default. A visitor jumps forward only to steps already visited, and the steps on the way are validated.
navigation: 'free'A visitor jumps to any step that takes part, without validation. Next still validates the step it leaves.
constraints on a stepshow or hide, with the same logic as fields, on non-row fields of earlier steps. The first step cannot have constraints.
  • Next validates. It checks the fields of the current step and moves to the next step that takes part. A step with errors shows them, focuses the first and stays put.
  • Previous never validates. It moves to the previous step that takes part.
  • Submit before the last step is a Next. On the last step it checks every step that takes part, runs the actions and onSubmit, and on an error moves to the first step that has one.
  • A skipped step is passed over. Next and Previous skip it and a jump refuses it. Progress such as "step 2 of 3" counts only the steps that take part.
  • A skipped step takes no part. Its fields are not validated, do not block and are not submitted, exactly like hidden fields. Their values are kept and return when the step is shown again.
  • The current step can become skipped, for example after an action. The wizard then moves back to the nearest step before it that takes part, and never forward without validating.
  • A server error on an earlier step moves the wizard there, so it is never invisible. See Server errors.

Step triggers step_leave, step_change and step_enter fire when the current step changes. They cannot block a move: the validation of the step is the guard.

What a command returns

Next, Previous and a jump each resolve to one outcome string, and a submit adds submitted. A component that moves the wizard reads it to say what happened: moved when the step changed, submitted when the submission succeeded, invalid when validation found errors (they are shown and focused), failed when a submission failed or a step check could not run (the cause is logged), and refused when there was nowhere to go. overtaken, busy and cancelled mean the command did nothing: an edit outran the check, another command was running, or a reset or an unmount revoked it. Why a submission failed is in submission.failures, which useFormContext().formState.submission reads in React. The Form runtime page has the full table.

The step state

Both renderers give a component the same list, every step in the order of the config. Each entry has the id and the title of the step, as the config wrote it (a form config requires one, so title is set for every step it validated), and skipped (its constraints hide it), visited, complete (Next validated it and moved on), invalid (an error is recorded at one of its fields) and reachable (a jump can move there from the current step). The Form runtime page has the type.

In React

useFormWizard() from @schema-engine/forms-react/hooks returns current (the id of the current step), steps, navigating (a command is running) and the commands next(), back() and goTo(id). It has to run inside the form, so render the component as a child of FormRenderer. This stepper lists the steps that take part and jumps to one the form lets the visitor reach:

import type { FormValues } from '@schema-engine/forms';
import { FormRenderer } from '@schema-engine/forms-react';
import { useFormWizard } from '@schema-engine/forms-react/hooks';
import { checkout } from './checkout';
import { engine } from './react-wizard.helper.react';

/** Lists the steps that take part and jumps to one the form lets the visitor reach. */
function Stepper() {
	const { current, steps, goTo } = useFormWizard();
	return (
		<ol aria-label="Steps">
			{steps
				.filter((step) => !step.skipped)
				.map((step) => (
					<li key={step.id}>
						<button
							type="button"
							aria-current={step.id === current ? 'step' : undefined}
							disabled={!step.reachable}
							onClick={() => void goTo(step.id)}
						>
							{step.title ?? step.id}
							{step.invalid ? ' (fix)' : ''}
						</button>
					</li>
				))}
		</ol>
	);
}

/** The checkout with a stepper of its own beside the wizard's Previous and Next. */
export function Checkout({ onSubmit }: { onSubmit: (values: FormValues) => void }) {
	return (
		<FormRenderer engine={engine} config={checkout} onSubmit={onSubmit}>
			<Stepper />
		</FormRenderer>
	);
}

A recipe's own blocks read the same state as useViewContext().navigation, as View recipes shows.

In Radiant

new FormController(this) in an element inside the form gives it wizard, a signal over the form's wizard: current, steps, navigating and the commands next(), back() and goTo(id). It reads undefined while the form has no runtime and on the server. Subscribe to it to render again when the wizard changes, and stop in disconnectedCallback. The element has to sit inside the form, so pass it to FormRenderer as a child:

import { customElement, RadiantElement } from '@ecopages/radiant';
import type { JsxRenderable } from '@ecopages/jsx';
import type { FormValues } from '@schema-engine/forms';
import {
	createRadiantFormEngine,
	FormController,
	FormRenderer,
	schemaFormRegistrations,
} from '@schema-engine/forms-radiant';
import { checkout } from './checkout';

/**
 * Lists the steps that take part and jumps to one the form lets the visitor reach.
 *
 * @remarks
 * `FormController.wizard` is a signal over the form's wizard, and the element renders again each time it changes. The
 * element has to sit inside the form, so it is passed to `FormRenderer` as a child.
 */
@customElement('checkout-stepper')
export class CheckoutStepper extends RadiantElement {
	private readonly form = new FormController(this);
	private readonly wizard = this.form.wizard;
	private stop?: () => void;

	protected override onConnected(): void {
		this.stop = this.wizard.subscribe(() => this.requestUpdate());
	}

	override disconnectedCallback(): void {
		this.stop?.();
		super.disconnectedCallback();
	}

	override render(): JsxRenderable {
		const wizard = this.wizard.get();
		if (!wizard) return null;
		return (
			<ol aria-label="Steps">
				{wizard.steps
					.filter((step) => !step.skipped)
					.map((step) => (
						<li key={step.id}>
							<button
								type="button"
								aria-current={step.id === wizard.current ? 'step' : undefined}
								prop:disabled={!step.reachable}
								on:click={() => void wizard.goTo(step.id)}
							>
								{step.title ?? step.id}
								{step.invalid ? ' (fix)' : ''}
							</button>
						</li>
					))}
			</ol>
		);
	}
}

export const engine = createRadiantFormEngine({ elements: schemaFormRegistrations });

/** The checkout with a stepper of its own beside the wizard's Previous and Next. */
export function Checkout({ onSubmit }: { onSubmit: (values: FormValues) => void }) {
	return (
		<FormRenderer engine={engine} config={checkout} onSubmit={onSubmit}>
			<checkout-stepper />
		</FormRenderer>
	);
}

The same controller gives the element submission, a signal over the form's submission, and clearSubmission() to hide its outcome, as the container and select do for the form's state in Form controls. The instance also emits the se-action-submit-start, se-action-submit-invalid and se-action-submit-complete events for the submission.

Reset

useFormContext().reset, an action's form.reset, a native reset button and FormController.container.reset() all return the form to its first step with an idle submission and no step trigger fired.

Where to go next