How to show that the last mat-step is complete in mat-stepper?

Andrew Medhurst

Okay. I can't believe that I'm posting a question for the first time over something that should be so simple to accomplish, but here we are.

I would like for the final step in my mat-horizontal-stepper to display a checkmark icon and green background once a specific button is clicked, just like the icons for the steps prior to it. That button would be the 'Yes, confirm' button in the following image.

Confirmation page

Once clicked, I would like the blue icon with the number three to change into the checkmark icon that I previously described, indicating that all steps are now completed. Steps 1 & 2 do it automatically because it seems as if the 'mat-step-icon-state-done' class gets applied to them once a button marked as 'matStepperNext' is pressed. Sadly, Step 3 does not have such a button, so it must be done manually.

Now, I've tried everything that would come up for a search regarding this. Many posts suggest using custom icons with states using <ng-template></ng-template>, but that has not worked. Others have suggested marking the step with completed=true; editable=false;, but this only works when moving to the next step as well, which means it won't apply to the final step. My hypothesis is that there must be some way to add the 'mat-step-icon-state-done' class to the mat-icon somehow, but I'm not really sure how to go about doing that. Also, please feel free to point me in the correct direction if my hunch is completely off.

nash11

There doesn't seem to be a direct way to do this as per the docs. There is a completed and state input though which we can use on the final mat-step.

If you see the code for the stepper on GitHub, you can see the following condition

if (step._showError && step.hasError && !isCurrentStep) {
    return STEP_STATE.ERROR;
} else if (step.completed && !isCurrentStep) {
    return STEP_STATE.DONE;
} else if (step.completed && isCurrentStep) {
    return state;
}

This shows that setting completed alone is not sufficient since the final step will still be the current step. But as we can see from the condition, all we need to do now is change state as well.

In your last mat-step, add completed and state

<mat-step [completed]="completed" [state]="state">
    <ng-template matStepLabel>Done</ng-template>
        You are now done.
    <div>
        <button mat-button matStepperPrevious>Back</button>
        <button mat-button (click)="done()">Confirm</button>
    </div>
</mat-step>

Then control this in your component when the button is clicked.

completed: boolean = false;
state: string;

done() {
    this.completed = true;
    this.state = 'done';
    console.log(this.firstFormGroup.valid);
    console.log(this.secondFormGroup.valid);
}

Note: You can use the validity of the forms to conditionally set these two variables or show an error message to prompt the user to complete the rest of the steppers.

Here is a working example on StackBlitz.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to hide mat-stepper header in my mat-stepper?

mat-horizontal-stepper not accepting style for the active step

How to test mat-stepper with Cypress

How to show default icons instead 'done' icon in angular mat-stepper

mat-step content vanishes when animating - mat-horizontal-stepper

mat stepper bar is not in the npm registry

How to change the angular material mat stepper label color?

How to replace next button with mat-chip in Angular Material stepper?

How to Disable/Stop HostListener if any document is opened before mat stepper?

Angular Material Stepper causes mat-formfield to validate for dynamic forms when returning to an older step

Angular Material: How to set each mat-horizontal-stepper stepper icon with different background color in TS

How not to show selected value in mat-select

How to show Tooltip on click of mat-icon

How to prevent show mat-menu on click?

Mat stepper done icon is out of circle

Disable animation of mat-stepper while in transition

How to change the font size of mat-step's label?

Mat auto complete not filtering the values

Angular Material Mat-Stepper: How to use the same formgroup for multiple steps?

How to control mat-stepper navigation between steps without Linear mode?

How to show mat-spinner for each row in mat-table in angular 6

How to show mat-table in Expanded Mat-row rather than in Main table

how to hide mat-toolbar in login page and then show mat-toolbar in home page?

Allow space (key) in Mat-Input inside mat-step

Switch To the last mat-tab with if(){}

How to Show Input box based on Mat-Select Option

How to make <mat-autocomplete> show when click on input

how to show only current year in mat-datepicker

How can I make mat-select show appropriate option?