Angular Stepper - Cancel a Step Change

You can cancel a step change in the Stepper onSelectionChanging event handler. To do this, assign a Boolean value or Promise that returns a Boolean to SelectionChangingEvent.cancel. The code snippet below implements a DevExtreme confirm dialog that asks users for confirmation before moving the component's selected step back.

app.component.html
app.component.ts
  • <dx-stepper (onSelectionChanging)="handleSelectionChanging($event)" >
  • <dxi-stepper-item
  • *ngFor="let step of steps"
  • [label]="step.label"
  • [icon]="step.icon"
  • [optional]="step.optional"
  • ></dxi-stepper-item>
  • </dx-stepper>
  • import { DxStepperModule } from 'devextreme-angular';
  • import { DxStepperTypes } from "devextreme-angular/ui/stepper";
  • import { confirm } from 'devextreme/ui/dialog';
  •  
  • // ...
  • export class AppComponent {
  • handleSelectionChanging(e: DxStepperTypes.SelectionChangingEvent) {
  • const { component, addedItems, removedItems } = e;
  • const { items = [] } = component.option();
  •  
  • const removedIndex = items.findIndex((item) => item === removedItems[0]);
  • const addedIndex = items.findIndex((item) => item === addedItems[0]);
  •  
  • const isMoveBack = addedIndex < removedIndex;
  • if (isMoveBack) {
  • e.cancel = confirm(`<div>Are you sure you want to go back?</div>`, '')
  • .then((isConfirmed) => {
  • return !isConfirmed;
  • });
  • }
  • }
  • }
NOTE
Configure Stepper with unique items objects to ensure the findIndex method always returns correct indexes.