I am currently trying to implement a tab feature using Angular Material. I can for the life of me not get the button to click that is in the header. I have tried moving the click event around the tabs and it doesn't seem to get triggered anywhere.
I am trying to add the functionality that you can close a tab by clicking on the close icon. I have tried putting it in a div. I've tried using a div instead of a button.
Here is the UI:
<mat-tab-group animationDuration="0ms">
<div *ngIf="tabs">
<mat-tab *ngFor="let tab of tabs; let index = index" [ngSwitch]="tab.type">
<ng-template mat-tab-label>
<div style="display: flex; flex-direction: row; align-items: center;">
{{tab["title"]}}
<div>
<button style="color:black" mat-icon-button (click)="closeTab($event, index)">
<mat-icon>close</mat-icon>
</button>
</div>
</div>
</ng-template>
<div *ngSwitchCase="'Work'">
<p>Work</p>
</div>
<div *ngSwitchCase="'Case'">
<p>Case</p>
</div>
<div *ngSwitchCase="'Document'">
<app-base></app-base>
</div>
</mat-tab>
</div>
</mat-tab-group>
</div>
And the backend:
import {MatTab, MatTabGroup} from '@angular/material/tabs';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent {
@ViewChild(MatTabGroup, {read: MatTabGroup})
public tabGroup: MatTabGroup;
@ViewChildren(MatTab, {read: MatTab})
public tabNodes: QueryList<MatTab>;
tabs: Tab[] = [];
workCounter: number = 0;
caseCounter: number = 0;
documentCounter: number = 0;
closedTabs: number[] = [];
ngOnInit(): void {
}
addNewTab(type: string): void {
let tab = new Tab();
switch (type) {
case 'Work':
this.workCounter++;
tab.type = type;
tab.title = `${type} # ${this.workCounter}`
break;
case 'Document':
this.documentCounter++;
tab.type = type;
tab.title = `${type} # ${this.documentCounter}`
break;
case 'Case':
this.caseCounter++;
tab.type = type;
tab.title = `${type} # ${this.caseCounter}`
break;
}
this.tabs.push(tab);
}
closeTab(event: Event, index: number) {
console.log(index);
event.stopPropagation();
this.closedTabs.push(index);
this.tabGroup.selectedIndex = this.tabNodes.length - 1;
console.log(index);
}
}
class Tab {
type: string;
title: string;
index: number;
}