Crustack
@crustack/form

Multi-step Forms

Multi-step forms are a way to break down a form into smaller, more manageable steps.

Multi-step forms are easily implemented with Crustack form's flexible architecture.

Each step can be handled independently by using the Form component's onSubmit event handler. By calling event.stopPropagation(), you can prevent the event from reaching the root form and triggering a premature submission.


In-memory steps

Below is an example of a three-step form that allows users to navigate between steps using back and next buttons.

import {  } from '@crustack/form'

type  = 'step1' | 'step2' | 'step3'
type  = { :  }
type  = 

const  = <, any, any, >()

const  = () => {
  return (
    <.
      ={{ : 'step1' }}
      ={(, ) => {
        // jump to step
        if (..) {
          return .('step', ..)
        }
        // step1 => step2
        if (. === 'step1') {
          return .('step', 'step2')
        }
        // step2 => step3
        if (. === 'step2') {
          return .('step', 'step3')
        }
        // step3 => submit
        ()
      }}
    >
      {() => (
        <>
          {.. === 'step1' && (
            <.>
              {/* Use the `tryToSubmit` method to move to a specific step. */}
              <
                ="button"
                // Jump to step3 with validation of the current values
                ={() => .('step3')}
                ="Skip and jump to Step 3"
              />
              ...
              {/*
                Make use of the native 'submit' button to move to the next step.
                This will try to submit the form, touch & validate the mounted fields.
              */}
              < ="Move to Step 2" />
            </.>
          )}

          {.. === 'step2' && (
            <.>
              <
                ="button"
                // no need for validation when going back
                ={() => .('step', 'step1')}
                ="Back to Step 1"
              />
              ...
              {/* Same as "Move to Step 2" */}
              < ="Move to Step 3" />
            </.>
          )}

          {.. === 'step3' && (
            <.>
              <
                ="button"
                // no need for validation when going back
                ={() => .('step', 'step2')}
                ="Back to Step 2"
              />
              ...
              < ="Submit the form" />
            </.>
          )}
        </>
      )}
    </.>
  )
}

On this page