The Stepper component allows us to traverse through content 1 step at a time.
The Stepper component allows us to traverse through content 1 step at a time.
Implementation
To begin, add the MatStepperModule to the project. Go into the app module file and add the import statement. Then add it to the app.
Using the Stepper
To add the stepper, use the mat-horizontal-stepper or the mat-vertical-stepper element.
<mat-horizontal-stepper></mat-horizontal-stepper >
<mat-vertical-stepper></mat-vertical-stepper>
To add the steps, place the mat-step element inside the stepper. Use 1 for each step.
<mat-horizontal-stepper>
To label the steps, set the label attribute with some text.
<mat-step label="Step 1">...</mat-step>
Instead of displaying the labels next to the steps icon, we can place them underneath. Apply the labelPosition attribute to the stepper and set it to bottom.
<mat-horizontal-stepper labelPosition="bottom">
Custom Labels
For more complex labels, remove the label attribute and add a ng-template element inside the step. Then apply the matStepLabel property.
<mat-horizontal-stepper labelPosition="bottom">
Inside the template, we can place the contents for our label.
Adding Content
To add the content for each step, place your code inside the step element as you would do for a div element.
Next and Back Buttons
The stepper module also comes with 2 button directives that we can add to the buttons inside the step so we can traverse back and forth between them.
Use the matStepperNext directive for the next button and the matStepperPrevious directive for the back button.
To stop the user from going back, apply the linear attribute to the stepper. Then apply the editable property to the step and set it to false.
<mat-horizontal-stepper linear>
This will stop the user from going back to the step once they move on from it.
Using FormGroup Validation
Import AbstractControl, FormBuilder, FormGroup, ValidatorFn, and Validators from @angular forms and then declare a FormGroup.
TypeScript
Inject FormBuilder in the constructor and create the group.
Then attach the group to the step. In the HTML page, apply the stepControl attribute to the step and pass in the group object.
HTML
<mat-horizontal-stepper linear>
Do not forget to add the Forms and ReactiveForms modules to your project.
Optional Steps
We can also use the optional attribute to enable the user to skip the step even though there is a FormGroup or the completed attribute attached to it.
<mat-horizontal-stepper linear>
Displaying Error Messages
<mat-horizontal-stepper>
Custom Stepper Icons
We can also change the icon of the steps. Set the displayDefaultIndicatorType of the stepper to false in the stepper configuration.
Then create a template to replace the icon using the ng-template element in the stepper element. You can put whatever you want inside the template. For example, we can use an image
HTML
<mat-horizontal-stepper>
Then apply the matStepperIcon attribute to the template and give it a name.
Lastly, add apply the state attribute to the step you want to replace the icon with and set it to the name of the template that you want to use as the icon.
<mat-horizontal-stepper>