Multi-step Wizard

A three-step registration wizard built entirely with section composition. All steps share one RHF form instance - no state reset between steps, and full cross-step validation on submit.

Preview & Code

Preview
Code
Account
Profile
Preferences

How it works

Step state lives outside the form. step is plain useState. The form has no concept of steps; it only sees three section fields whose visible function reads step:

account: {
  type: "section",
  visible: () => step === 0,
  props: { nested: true, component: AccountStep },
},

A visible function that reads React state re-runs whenever the parent re-renders, so setStep is all the navigation needs.

"Next" validates only the current step. The children render prop exposes trigger. Pass the paths of the current step, and move on only when they are valid:

{({ trigger }) => (
  <button
    type="button"
    onClick={async () => {
      if (await trigger([...STEPS[step].paths])) setStep((s) => s + 1);
    }}
  >
    Next →
  </button>
)}

One RHF instance for all steps. Data entered in step 1 is still there on step 3, and going back never resets values. A hidden step unmounts its fields, but with the default shouldUnregister: false their values stay in the form state and are submitted. With shouldUnregister: true, only the visible step's values would be submitted.

Submit sees the whole model. Fields of steps that were already shown keep their rules, so submitting from the last step still validates every step that was filled in. Fields of a step that was never shown were never registered and are not validated: the per-step "Next" check is what guarantees each step was valid.

Section fills the boundary. Each step component renders its own typed config fragment:

function AccountStep() {
  return <Section<WizardData["account"]> config={accountConfig} />;
}
Tip

Prefer a schema? Use schema={WizardSchema} on the form and call trigger(paths) the same way: the resolver validates the whole schema, and RHF reports the errors for the requested paths. Unlike field rules, a schema also validates steps that were never shown when the form is submitted.