Angular Stepper - Implement a Wizard

This tutorial uses a Stepper widget to create a wizard-like application. The created application integrates the Form and MultiView components.

This wizard includes three key parts:

  1. The Stepper component.
  2. Step content where the MultiView component shows content for each step. Every view with input fields contains a Form.
  3. A navigation panel with an active step caption ("Step 1 of 5") and buttons for moving between steps ("Next" and "Back").

Stepper wizard elements

To view the complete code of this example, refer to the following demo:

Stepper - Form Integration Demo

Set Up App Layout

To get started, add a Stepper and a MultiView to your project. Create a navigation panel - a container with child items. Child items are a <div> element that displays the active step caption and two Buttons:

app.component.html
app.component.ts
  • <dx-stepper ... >
  • <dxi-stepper-item ...
  • *ngFor="let step of steps"
  • ></dxi-stepper-item>
  • </dx-stepper>
  • <div class="content">
  • <dx-multi-view ...
  • [animationEnabled]="false"
  • [focusStateEnabled]="false"
  • [swipeEnabled]="false"
  • >
  • </dx-multi-view>
  • <div class="nav-panel">
  • <div class="current-step">
  • <span ... >
  • Step <span class="selected-index">1</span> of {{ steps.length }}
  • </span>
  • </div>
  • <div class="nav-buttons">
  • <dx-button ...
  • id="prevButton"
  • [visible]="false"
  • text="Back"
  • >
  • </dx-button>
  • <dx-button ...
  • id="nextButton"
  • text="Next"
  • >
  • </dx-button>
  • </div>
  • </div>
  • </div>
  • import { DxStepperModule, DxButtonModule, DxMultiViewModule, DxFormModule } from 'devextreme-angular';
  • import { AppService } from './app.service';
  • // ...
  • export class AppComponent {
  • steps: Item[];
  •  
  • constructor(private readonly appService: AppService) {
  • this.steps = this.appService.getInitialSteps();
  • }
  • }

The "Back" button is initially hidden. A later step in this tutorial implements dynamic visibility logic (see Synchronize Steps and Navigation Panel).

Configure forms for each step with MultiView items[].template:

app.component.html
app.component.ts
dates-form.component.html
  • <div class="content">
  • <dx-multi-view ... >
  • <dxi-multi-view-item>
  • <div *dxTemplate>
  • <dates-form ... ></dates-form>
  • </div>
  • </dxi-multi-view-item>
  • <!-- Configure the rest of the forms -->
  • </dx-multi-view>
  • </div>
  • // ...
  • import { DatesFormComponent } from "./dates-form/dates-form.component";
  • import { BookingFormData } from './app.types';
  •  
  • // ...
  • export class AppComponent {
  • formData: BookingFormData;
  •  
  • constructor(private readonly appService: AppService) {
  • // ...
  • this.formData = this.appService.getInitialFormData();
  • }
  • }
  •  
  • @NgModule({
  • // ...
  • declarations: [
  • // ...
  • DatesFormComponent,
  • ]
  • })
  • <dx-form ... >
  • <dxi-form-item ... ></dxi-form-item>
  • </dx-form>

This tutorial implements custom components as MultiView item templates and a custom type for form data. For more details on how this example is configured, refer to the Stepper - Form Integration demo.

Synchronize Steps and Navigation Panel

Use a separate variable and two-way data binding syntax to synchronize selectedIndex properties of Stepper and MultiView:

app.component.html
app.component.ts
  • <dx-stepper ...
  • [(selectedIndex)]="selectedIndex"
  • >
  • </dx-stepper>
  • <div class="content">
  • <dx-multi-view ...
  • [(selectedIndex)]="selectedIndex"
  • >
  • </dx-multi-view>
  • </div>
  • // ...
  • export class AppComponent {
  • selectedIndex: number;
  •  
  • constructor(private readonly appService: AppService) {
  • // ...
  • this.selectedIndex = 0;
  • }
  • }

This example utilizes the selectedIndex variable to implement navigation panel functionality:

  • The onPrevButtonClick and onNextButtonClick handlers move to the previous/next step by modifying selectedIndex.
  • The getNextButtonText function changes the text of the "Next" button to "Confirm" on the last step.

The "Confirm" button submits the form and disables user interactions with the Stepper. For details on how to disable Stepper interactions, refer to the following help topic: Configure a Readonly Stepper.

After users submit the form, they can reset the wizard and start over.

app.component.html
app.component.ts
app.component.css
  • <dx-stepper ...
  • [focusStateEnabled]="!isStepperReadonly"
  • [class.readonly]="isStepperReadonly"
  • ></dx-stepper>
  • <div class="nav-panel">
  • <div class="current-step">
  • <span *ngIf="!isConfirmed">
  • Step <span class="selected-index">{{ selectedIndex + 1 }}</span> of {{ steps.length }}
  • </span>
  • </div>
  • <div class="nav-buttons">
  • <dx-button ...
  • id="prevButton"
  • [visible]="selectedIndex !== 0 && !isConfirmed"
  • (onClick)="onPrevButtonClick($event)"
  • ></dx-button>
  • <dx-button ...
  • id="nextButton"
  • [text]="getNextButtonText()"
  • (onClick)="onNextButtonClick($event)"
  • >
  • </dx-button>
  • </div>
  • </div>
  • // ...
  • export class AppComponent {
  • // ...
  • isConfirmed: boolean;
  • isStepperReadonly: boolean;
  •  
  • constructor(private readonly appService: AppService) {
  • // ...
  • this.isConfirmed = false;
  • this.isStepperReadonly = false;
  • }
  •  
  • getNextButtonText() {
  • if (this.selectedIndex < this.steps.length - 1) {
  • return 'Next';
  • }
  •  
  • return this.isConfirmed ? 'Reset' : 'Confirm';
  • }
  •  
  • onPrevButtonClick() {
  • this.selectedIndex -= 1;
  • }
  •  
  • moveNext() {
  • this.selectedIndex += 1;
  • }
  •  
  • reset(){
  • this.isConfirmed = false;
  • this.selectedIndex = 0;
  • this.isStepperReadonly = false;
  • // ...
  • }
  •  
  • confirm(){
  • this.isConfirmed = true;
  • this.isStepperReadonly = true;
  • }
  •  
  • onNextButtonClick() {
  • if (this.selectedIndex < this.steps.length - 1) {
  • this.moveNext();
  • } else if (this.isConfirmed) {
  • this.reset();
  • } else {
  • this.confirm();
  • }
  • }
  • }
  • ::ng-deep .readonly {
  • pointer-events: none;
  • }

Implement Validation

To configure validation, assign validation groups and specify validation rules in each MultiView form that requires validation:

dates-form.component.html
dates-form.component.ts
app.component.html
app.component.ts
  • <dx-form ...
  • [validationGroup]="validationGroup"
  • >
  • <dxi-form-item ...
  • [isRequired]="true"
  • ></dxi-form-item>
  • </dx-form>
  • // ...
  • export class DatesFormComponent {
  • @Input() validationGroup: string;
  • }
  • <div class="content">
  • <dx-multi-view ... >
  • <dxi-multi-view-item>
  • <div *dxTemplate>
  • <dates-form ...
  • [validationGroup]="validationGroups[0]"
  • ></dates-form>
  • </div>
  • </dxi-multi-view-item>
  • </dx-multi-view>
  • </div>
  • // ...
  • export class AppComponent {
  • validationGroups = ['dates', 'guests', 'roomAndMealPlan'];
  • }

This tutorial validates steps only when users move forward. If a step fails validation, navigation operation is cancelled:

app.component.html
app.component.ts
  • <dx-stepper ...
  • (onSelectionChanging)="onSelectionChanging($event)"
  • >
  • </dx-stepper>
  • import validationEngine from 'devextreme/ui/validation_engine';
  •  
  • // ...
  • export class AppComponent {
  • onSelectionChanging(e: SelectionChangingEvent) {
  • if (this.isConfirmed) {
  • e.cancel = true;
  •  
  • return;
  • }
  •  
  • const { component, addedItems, removedItems } = e;
  • const { items = [] } = component.option();
  •  
  • const addedIndex = items.findIndex((item: Item) => item === addedItems[0]);
  • const removedIndex = items.findIndex((item: Item) => item === removedItems[0]);
  • const isMoveForward = removedIndex > -1 && addedIndex > removedIndex;
  •  
  • if (isMoveForward) {
  • const isValid = this.getValidationResult(removedIndex);
  •  
  • this.setStepValidationResult(removedIndex, isValid);
  •  
  • if (isValid === false) {
  • e.cancel = true;
  • }
  • }
  • }
  •  
  • moveNext() {
  • const isValid = this.getValidationResult(this.selectedIndex);
  •  
  • this.setStepValidationResult(this.selectedIndex, isValid);
  •  
  • if (isValid) {
  • this.selectedIndex += 1;
  • }
  • }
  •  
  • getValidationResult(index: number){
  • if (index >= this.validationGroups.length) {
  • return true;
  • }
  •  
  • return validationEngine.validateGroup(this.validationGroups[index]).isValid;
  • }
  •  
  • setStepValidationResult(index: number, isValid: boolean | undefined){
  • this.steps[index].isValid = isValid;
  • }
  • }

Review the full code of this tutorial in the following demo:

Stepper - Form Integration Demo