60

I want to navigate the stepper only through the next and back buttons.

I can't get this to work since users can also click each step label to navigate to any step. I can't use linear, since it requires each step to have a formArray or FormGroup.

I have tried <mat-step (click)="$event.stopPropagation()">.

Mel
  • 5,837
  • 10
  • 37
  • 42
Shaniqwa
  • 1,924
  • 2
  • 17
  • 29

14 Answers14

124

Add this to your style sheet. I was trying to disable the header navigation. Tried many things but this hack worked. You can try this till Angular Material Team support this feature.

::ng-deep .mat-horizontal-stepper-header{
    pointer-events: none !important;
}
muhammad kashif
  • 2,566
  • 3
  • 26
  • 49
Shahzaib Shahid
  • 1,341
  • 1
  • 8
  • 6
74

Use a linear stepper with completed=false steps. When the user presses your button, programattically complete the step and move to the next one.

This way you don't need to mess with CSS pointer events. In our app, that resulted in accessibility problems with NVDA.

    <mat-horizontal-stepper linear #stepper>
      <mat-step completed="false">
        <ng-template matStepLabel>Step 1</ng-template>
        <app-some-child (nextClicked)="nextClicked($event)" ></app-some-child>
      </mat-step>
      <mat-step>
        <ng-template matStepLabel>Step 2</ng-template>
        <app-some-other-child></app-some-other-child>
      </mat-step>
    </mat-horizontal-stepper>
    
    export class AppComponent implements OnInit {

      @ViewChild('stepper') stepper: MatStepper;

      nextClicked(event) {
        // complete the current step
        this.stepper.selected.completed = true;
        // move to next step
        this.stepper.next();
      }
    }

raaaay
  • 496
  • 7
  • 14
James Young
  • 1,372
  • 13
  • 16
  • 11
    This is the proper way to do it. – Mickers Apr 19 '19 at 13:50
  • 8
    Cool solution! Note that it will not prevent the user to go back to first step once he is at the second step (and that may be something you need) For this you can add `editable="false"` to your `mat-step`s – dekajoo Oct 24 '19 at 12:45
  • Nice solution here! Thank you – AGrammerPro Sep 08 '20 at 12:55
  • 1
    Bad solution, it doesn't disable "next" steps, previous steps don't become grayed out. Css is more right solution, – roma2341 Feb 22 '22 at 08:30
  • Great answer and proper way to implement – Kumaresan Sd Mar 25 '22 at 04:42
  • Seems obvious but if you don't set the selected step as completed the stepper won't move. If you want to disable the previous step you can also set the `this.stepper.selected.editable = false` – Low Oct 27 '22 at 06:02
8

Does not work without ::ng-deep

    ::ng-deep .mat-horizontal-stepper-header{
      pointer-events: none !important;
    }
raaaay
  • 496
  • 7
  • 14
Suliman Farzat
  • 1,197
  • 11
  • 12
8

Don't use ::ng-deep as it is deprecated.

https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep

Instead, if you are using Angular Material, use the theme guide from the documentation.

https://material.angular.io/guide/theming

Example of a implementation of the style:

my-custom-elements.scss

@import '~@angular/material/theming';

@mixin custom-stepper-theme() {
  .mat-horizontal-stepper-header {
    pointer-events: none;
  }
}

global-material-theme.scss

@import '~@angular/material/theming';
// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core();

@import './material/my-custom-elements.scss';

@include custom-stepper-theme();

angular.json

...
"styles": ["src/styles.scss", "src/app/global-material-theme.scss"]
...
Jorge Mussato
  • 2,266
  • 2
  • 11
  • 19
  • How can I custom for only one component is my angular app? – Duc Nguyen Apr 20 '20 at 07:38
  • Put a different class on it. And use the class specification (then you can define the mixin inside the scss of that component, its a better code practice to say it is a specific style) – Jorge Mussato Apr 20 '20 at 18:38
8

For anyone that's still looking for an alternate solution if ::ng-deep does not work.

Furthermore, ::ng-deep is deprecated and setting ViewEncapsulation to none is the preferred way to go if you want to do it using CSS.

Import ViewEncapsulation and set to None in your

compnent.ts:

import { Component, OnInit, ViewEncapsulation } from "@angular/core";

@Component({
  selector: "stepper-overview-example",
  templateUrl: "stepper-overview-example.html",
  styleUrls: ["stepper-overview-example.css"],
  encapsulation: ViewEncapsulation.None
})
export class StepperOverviewExample implements OnInit {
  isLinear = false;

  constructor() {}

  ngOnInit() {}
}

set pointer-events to none in your

component.css:

.mat-horizontal-stepper-header { 
  pointer-events: none !important; 
}

Here's a DEMO.

kaiilim
  • 579
  • 12
  • 31
  • Thank you! I had a nested stepper and the encapuslation property you mention here made all the difference (y) – lucniner Feb 10 '21 at 08:08
  • ViewEncapsulation None is not a good idea. That should be reserved for the ultimate "no other possible option" scenario. The side effects will not be obvious, and you will find them in the form of bugs over a long period of time. Lots of weird issues with styles being picked up and/or applied in all the wrong places. Just do some serious spot-checking throughout your entire app before releasing it if you chose this route. – mwilson Oct 26 '22 at 20:05
5

Just to improve on the accepted answer, as it will not work if you have a vertical stepper.

To stop the user being able to click the header and navigate add the following code to you style.css file in the root:-

.mat-step-header {
  pointer-events: none !important;
}

This will ensure it works for mat-horizontal-stepper-header and mat-vertical-stepper-header

Web Nexus
  • 1,150
  • 9
  • 28
3

I found a somewhat hacky solution for this. The problem is that you cannot fully disable the header navigation, but you can prevent it from being active until a certain moment.

And that moment is form.invalid.

My situation was the following: User needs to fill the form inside the step, press 'save' and only then be able to use NEXT button and navigate the stepper further (also back).

What I did was introduce another hidden input which would be using angular's [required] dynamic attribute. It will only be required if the previous save condition wasn't successful. Once it succeeds this field won't be required and user can navigate further.

Together with mat-stepper (or md-stepper) attribute editable you should be able to achieve what you want.

Let me know if you fully understood the idea.

Deniss M.
  • 3,617
  • 17
  • 52
  • 100
2

This also did the trick for me

Requirement:

  • Allow navigation either from Navigation or Next Button only WHEN form is valid
  • Disabled Header navigation and keep button disabled till form is not valid.

 <mat-horizontal-stepper #stepper [linear]="true">

    <!----------------------------->
    <!---- STEP: 1:---->
    <!----------------------------->

    <mat-step #generalStep [completed]="formGroup1.valid">
      <ng-template matStepLabel>Step-1</ng-template>
      
      <form [formGroup]="formGroup1">

      // STEP-1 Content
      // matInput - with form control bindings

       <div class="container">
          <button mat-raised-button matStepperNext color="primary"[disabled]="!formGroup1.valid">Next Step</button>
        </div>

      </form>

    </mat-step>

    <!------------------------------->
    <!-- STEP: 2:-->
    <!------------------------------->


    <mat-step #generalStep [completed]="formGroup2.valid">
      <ng-template matStepLabel>Step-2</ng-template>

      <form [formGroup]="formGroup2">

      // STEP-2 Content
      // matInput - with form control bindings

      <button mat-raised-button matStepperNext color="primary"[disabled]="!formGroup.valid">Next Step</button>

      </form>
    </mat-step>

  
  </mat-horizontal-stepper>

  • FormGroup/FormControl initialization
  • Adding Validators
  formGroup1: FormGroup;
  formGroup2: FormGroup;

   this.formGroup1 = this.formBuilder.group({
      control1: new FormControl('', [Validators.required]),
      control2: new FormControl('', [Validators.required]),
    });

  this.formGroup2 = this.formBuilder.group({
    control3: new FormControl('', [Validators.required]),
    control4: new FormControl('', [Validators.required]),
  });

khizer
  • 1,242
  • 15
  • 13
2

Following works for me with click on previous step enabled:

::ng-deep .mat-vertical-stepper-header:not([ng-reflect-active="true"]){
    pointer-events: none !important;
}

For horizontal stepper do this:

::ng-deep .mat-horizontal-stepper-header:not([ng-reflect-active="true"]){
        pointer-events: none !important;
}
Hari Das
  • 10,145
  • 7
  • 62
  • 59
1

First you need add ViewEncapsulation.None in your component config

@Component({
 selector: 'app-example',
 encapsulation: `ViewEncapsulation.None`
 })

Then add this in your component CSS.

.mat-horizontal-stepper-header-container  {
  display: none !important;
 }
0

Here ::ng-deep .mat-horizontal-stepper-header-container { display: none ; }

Use this on your style sheet to remove stepper header...Like Step-1,Step-2

0
// so that the User cannot go directly to step 3
.mat-step-header.mat-accent[ng-reflect-index="2"] {
  pointer-events: none !important;
}

This is preventing the Users to go to step 3 from a 3 steps stepper. Change the index accordingly for your project, meaning that if you want to disable step 5, make the index equal to 4. Also, put this in some sort of a Material Overrides file, or directly in your styles.scss file.

Bullsized
  • 362
  • 4
  • 7
-4

You need to add an "linear" attribute (This will disable navigation)

<mat-vertical-stepper linear>

  • 1
    cause error `Cannot read property 'invalid' of undefined` when clicking next and back buttons, or the header labels. To use `linear` means to add forms which is the basic idea of the stepper... but I don't need a form for each step – Shaniqwa Oct 31 '17 at 09:22
-4

In the tag add [linear]="true"

<mat-horizontal-stepper labelPostion="botton" [linear]="true">
...
</mat-horizontal-stepper>