Stepper
Use the Stepper component for a multistep flow in a fixed order, such as a creation wizard. It gives users an overview of all steps and guides them through the steps one by one. Create it with createStepper.
Import
import {
createStepper,
StepperRefusal,
} from '@dynatrace/strato-components/navigation';
Demo
The Stepper shows the steps as a list you can navigate, the content of the current step, and a footer with Back, Next, and Finish buttons.
The example above is a plain click-through flow without validation. Its Escalation step is disabled until the user enters an owner email. The user cannot select a disabled step, and Next skips it. The sections below cover validation.
createStepper returns the Stepper component and the useStepper and useStep hooks. They all share one store of step values and statuses.
- Call
createStepperat module level, not inside a render function. Inside a render function, every render would create a new store and lose the state. - Render at most one mounted
StepperpercreateSteppercall. Two would share the same state, and unmounting one would clear it for both. - Use
useStepperfor the flow, such as the current step orgoToStep. - Use
useStepfor the values and the status of a single step.
Validate a step
The Stepper does not validate values. Your app owns the validation rules and the errors. To report a result, write a status to the step with setStatus from useStep. The status has a validity, such as valid or invalid, and the errors that apply. An invalid status shows an error indicator in the step list. If the step has a Stepper.ErrorMessage, the step list shows that message in place of Stepper.Hint.
Steps are required by default. While a required step is invalid, the user cannot go to the next step. Set required={false} to make a step optional. An optional step never stops the user from moving on, and the step list marks it with an "Optional" label.
When the user presses Finish, the Stepper marks every required step that the user never visited as invalid. It then navigates to the first step that needs attention.
Validate a field
To validate a field, use the validation of your form component and report its result to the step. In the Owner step of the example below, the email field is required and checks its format with pattern. When the field loses focus, its onValidityChange handler writes the result to the step with setStatus, and FormFieldMessages.Item shows the error next to the field.
A field the user never touched reports nothing, so the step has no status and the user can still go to the next step. To cover that case, the Owner step also checks the value in onNext and throws a StepperRefusal when the email is empty. The next section explains refusals. The Notes step is optional, so it never blocks Next.
Validate when the user moves on
Some checks can only run when the user moves on, such as asking a server whether a name is taken. To keep the user on the step after such a check fails, throw a StepperRefusal from onNext or onComplete. The Stepper sets the status of the step to invalid and tells screen reader users that the step needs attention.
The message you pass to the refusal is added to errors in the step status. The Stepper does not display that message itself. To show it, render status.errors from useStep as FormFieldMessages.Item entries next to the field.
The invalid status stays until you change it. When the user edits the value, clear the status with setStatus, or the error stays visible.
For checks that take time, such as a request to a server, return a promise from onNext or onComplete. While the promise is pending, the Stepper sets the status of the step to pending. The Next or Finish button shows a spinner and ignores presses, so you do not set the status or guard against a second press. Back stays active. When the promise settles, the Stepper restores the status the step had before. If the promise rejects with a StepperRefusal, the step becomes invalid as described above. If you write a new status with setStatus while the promise is pending, the Stepper keeps your status.
The example below has two checks. The Name step refuses the name "High CPU" right away. The Entity selector step returns a promise that asks a fake server whether the selector matches any entities, and refuses the selector when nothing matches.
Use React Hook Form
If your form already uses React Hook Form, let it own the values and the validation, and use the Stepper only for the flow. In onNext, call trigger for the fields of the current step. When it reports a failure, throw a StepperRefusal so the Stepper keeps the user on the step.
Mirror every result to the step with setStatus, also when the check succeeds. The Stepper keeps a refused step invalid until you write a new status, and it reads that status after onNext has run. A valid status written during onNext lets the same press go to the next step. Without it, a step refused once keeps refusing, even after the user fixes the fields.
The example below is an alert wizard with two steps. React Hook Form validates the alert name on the first step and the owner email on the second.
The snapshot passed to onNext and onComplete only contains fields that are wrapped in Stepper.Control or written with setStepData. Fields that only React Hook Form knows do not appear in it, so read those values from React Hook Form instead.
Keep step data between mounts
By default, the Stepper clears its step data when it unmounts. That fits a flow that starts fresh every time, such as a creation wizard in a dialog.
Pass persist: true to createStepper when the user can leave and come back to the same data, for example when the Stepper is on a page the user can navigate away from. The Stepper then keeps values, statuses, and the current step when it unmounts, so the user returns to where they left off.
You are now responsible for clearing the data. Call resetStepData from useStepper when the data is no longer needed, for example after the flow finishes. It returns every step to its initialState and clears the statuses. Pass a step id to reset only that step. resetStepData does not navigate, so the user stays on the current step.
Hide and show the Stepper in the example. The alert name stays until you press Reset step data.