Multi-page formsMIT
Split long forms into pages with conditional pages, a progress bar, per-page validation, page navigation from code and resumable drafts.
Long forms are easier to complete one step at a time. Give the schema a pages array and the form shows one page at a time, with Back and Next buttons, a progress bar, and validation of each page before the user moves on.
Pages#
import type { FormSchema } from '@birchui/forms';
export const jobApplication: FormSchema = {
title: 'Apply: Field Service Engineer, Manchester',
completedMessage: 'Thanks for applying. We review every application and reply within ten working days.',
submitText: 'Send application',
pages: [
{
name: 'about',
title: 'About you',
elements: [
{ type: 'text', name: 'fullName', title: 'Full name', required: true, autocomplete: 'name' },
{ type: 'email', name: 'email', title: 'Email', required: true, width: 'half' },
{ type: 'text', name: 'phone', title: 'Phone', inputType: 'tel', autocomplete: 'tel', width: 'half' },
{
type: 'radio',
name: 'rightToWork',
title: 'Do you have the right to work in the UK?',
required: true,
inline: true,
choices: ['Yes', 'No'],
},
],
},
{
name: 'sponsorship',
title: 'Visa sponsorship',
visibleIf: "{rightToWork} = 'No'",
elements: [
{ type: 'textarea', name: 'visaDetails', title: 'Tell us about the visa you would need', required: true },
],
},
{
name: 'experience',
title: 'Experience',
elements: [
{ type: 'number', name: 'yearsExperience', title: 'Years of field service experience', min: 0, max: 50, integer: true },
{
type: 'checkboxgroup',
name: 'certifications',
title: 'Certifications',
choices: ['F-Gas Category 1', 'IPAF', 'CSCS', '18th Edition wiring regulations'],
},
{ type: 'file', name: 'cv', title: 'CV', required: true, accept: '.pdf,.docx', maxSize: 5_000_000 },
],
},
{
name: 'consent',
title: 'Before you send',
elements: [
{ type: 'markdown', content: 'We keep applications for **12 months**. Read our [privacy notice](https://example.com/privacy).' },
{ type: 'checkbox', name: 'privacyConsent', title: 'I agree to the processing of my application data.', required: true },
],
},
],
};Each page has a unique name, and optionally a title, a description and a visibleIf. Single-page forms can use elements instead of pages.
Conditional pages#
A page with visibleIf is skipped while its condition is false. In the example, the visa page only appears for applicants who answer "No". Skipped pages are not counted in the progress bar, their fields are not validated, and with the default clearHiddenValues their answers are left out of the submitted data.
The condition is re-evaluated as answers change, so a page can appear or disappear while the user is still on an earlier page.
Navigation and validation#
| Action | What happens |
|---|---|
| Next | Validates the current page. If it is valid, moves to the next visible page; otherwise shows the error summary and focuses the first invalid field. |
| Back | Moves to the previous visible page without validating, so users can always go back. |
| Submit | Shown on the last visible page. Validates every visible page and, if valid, emits formSubmit. |
Enter in a text field |
The same as Next, or Submit on the last page. |
Change the button labels with nextText, prevText and submitText in the schema, which also accept translations. With readOnly: true, the Submit button is hidden, which suits reviewing a submitted application.
When the page changes, focus moves to the new page's heading, so keyboard and screen reader users start at the top of the page.
Progress bar#
Multi-page forms show "Page 2 of 4" with the page title and a bar, exposed to assistive technology as a progressbar. Hide it with showProgressBar: false in the schema. showPageTitles: false hides the page headings as well.
Navigate from code#
| Method | Behaviour |
|---|---|
nextPage() |
Like pressing Next: validates, then moves. Resolves to true if it moved. |
prevPage() |
Like pressing Back. Returns true if it moved. |
goToPage(page) |
Jumps to a page by name or index without validating. Returns false for a hidden or unknown page. |
getCurrentPage() |
{ name, index, visibleIndex, visibleCount } for the page on screen. |
index counts every page in the schema, visibleIndex and visibleCount only the visible ones, which is what a custom step indicator needs:
form.on('pageChange', ({ page }) => {
const { visibleIndex, visibleCount } = form.getCurrentPage();
stepLabel.textContent = `Step ${visibleIndex + 1} of ${visibleCount}: ${page}`;
});The pageChange event carries from and to page indexes and the new page's name.
To open the form on a specific page, pass initialPage with a page name or index. A hidden or unknown page falls back to the first page, with a console warning.
Save and resume#
onDraft is called with the current data and page shortly after the user stops typing, and after each page change. Save both, and pass them back as data and initialPage to resume:
const KEY = 'application-draft';
const draft = JSON.parse(localStorage.getItem(KEY) ?? 'null') as { data: Record<string, unknown>; page: string } | null;
const form = createForm(container, {
schema: jobApplication,
data: draft?.data,
initialPage: draft?.page,
draftDelay: 800,
onDraft: (data, { page }) => localStorage.setItem(KEY, JSON.stringify({ data, page })),
});
form.on('formSubmit', async ({ data }) => {
await submitApplication(data);
localStorage.removeItem(KEY);
});File fields hold File objects, which JSON.stringify cannot save; ask users to attach files again, or upload them as soon as they are picked and store a reference in a hidden field instead.
After submission#
When the schema has a completedMessage, a successful submission replaces the form with that message and moves focus to it. Without one, the form stays on screen with the user's answers, which suits forms that save and stay open. Call reset() to restore the initial data and default values, clear errors and return to the first page, for example to take another application at a kiosk.