I was trying to prevent tab change of mat-tab
, if the form in currently active tab is dirty.
But I couldn't find a way to intercept the tab change event.
<mat-tab-group>
<mat-tab label="Tab 0" >
// Tab 0 Content
</mat-tab>
<mat-tab label="Tab 1" >
// Tab 1 Content
</mat-tab>
<mat-tab label="Tab 2" >
// Tab 2 Content
</mat-tab>
</mat-tab-group>
Even though there is a selectedTabChange
event, we can't prevent tab change. we can only switch tab programatically after tab change.
I have got a hack to make it possible. Just posting here to help if someone encounters the same.
This solution is just a hack and has its flaws. It is mentioned below.
Steps :
In the template :
<mat-tab label="Tab 0" disabled>
Provide a click event handler on mat-tab-group. Also set the
selectedIndex
property using a variable from the component class.<mat-tab-group (click)="tabClick($event)" [selectedIndex]="selectedTabIndex">
In the Component class :
Declare the variable
selectedTabIndex
selectedTabIndex = 0;
Create a method to get the tab Index , provided the tab label.
We can get the tab-label text from a property of the click event
clickEventName.toElement.innerText
Create the method for handling the click event on mat-tab-group.
}
In my case each tab content was in separate components. So I used
@ViewChild
to access them and check whether any of the form is dirty or not.Flaws :
Since this method uses the text of clicked element for switching tabs, there is a chance that one of your tab may contain some element with text content same as the heading of other tabs.
So it may trigger a tab change. You can look for other properties in click event to prevent this scenario if possible. I used following code in
tabClick()
method's beginningYou may need to override the
css
ofdisabled
state ofmat-tab
to make it look naturalTemplate :