Have created a tab component that allows the user to add multiple tabs and the individual tab information will get populated upon clicking the tab header which is working fine without any issue.
I would like to control/Manage the tab click using the dropdown select options.
app.component.ts
import { Component, ViewChild } from '@angular/core';
import { TabsComponent } from './tabs/tabs.component';
@Component({
selector: 'my-app',
template: `
{{ message }}
<select
id="select-menu-0"
class="services-select"
autoComplete="address-level1"
(change)="handleDropdown($event)"
>
<option *ngFor="let item of options; index as i">
{{item}}
</option>
</select>
<my-tabs (onSelect)="handleTabClick($event)" [selectedIndex]="selectedTab">
<my-tab [tabTitle]="'Tab 1'">
Tab 1 content
</my-tab>
<my-tab tabTitle="Tab 2">
Tab 2 content
</my-tab>
<my-tab tabTitle="Tab 3">
Tab 3 content
</my-tab>
</my-tabs>
`
})
export class AppComponent {
message: string;
selectedTab: number = 2;
options: Array<string> = ['Tab 1', 'Tab 2', 'Tab 3'];
handleTabClick(event) {
console.log('$evnt', event);
this.message = `${event.title} is clicked`;
}
handleDropdown(event) {
console.log('EVENT', event.target.value, this.options.indexOf(event.target.value));
this.selectedTab = this.options.indexOf(event.target.value);
this.message = `${event.target.value} is clicked`;
// this.handleTabClick(event);
}
}
tabs.component.ts
import {
Component,
ContentChildren,
QueryList,
AfterContentInit,
ViewChild,
ComponentFactoryResolver,
ViewContainerRef,
Input,
Output,
EventEmitter
} from '@angular/core';
import { TabComponent } from './tab.component';
import { DynamicTabsDirective } from './dynamic-tabs.directive';
@Component({
selector: 'my-tabs',
template: `
<ul class="nav nav-tabs">
<li *ngFor="let tab of tabs" (click)="sendMessage(tab)" [class.active]="tab.active">
<a href="#">{{tab.title}}</a>
</li>
</ul>
<ng-content></ng-content>
`,
styles: [
`
.tab-close {
color: gray;
text-align: right;
cursor: pointer;
}
`
]
})
export class TabsComponent implements AfterContentInit {
@Input() selectedIndex: number;
@Output() onSelect = new EventEmitter<string>();
sendMessage(tab: Tab) {
console.log('this.tabs', this.tabs.first, this.tabs.last);
this.onSelect.emit(tab);
this.tabs.toArray().forEach(tab => tab.active = false);
tab.active = true;
}
@ContentChildren(TabComponent) tabs: QueryList<TabComponent>;
// contentChildren are set
ngAfterContentInit() {
// get all active tabs
console.log('selectedIndex', this.selectedIndex, typeof this.selectedIndex);
console.log('ngAfter', this.tabs, this.tabs.toArray());
let activeTabs = this.tabs.filter((tab)=>{ console.log('TBB', tab); return tab.active});
console.log('activeTabs', activeTabs);
if (this.selectedIndex) {
console.log('SELECEELTLLT', this.tabs.toArray()[this.selectedIndex]);
this.selectTab(this.tabs.toArray()[this.selectedIndex]);
} else {
this.selectTab(this.tabs.first);
}
// if there is no active tab set, activate the first
// if(activeTabs.length === 0) {
// this.selectTab(this.tabs.first);
// }
}
selectTab(tab: Tab){
// deactivate all tabs
console.log('tab', tab);
this.tabs.toArray().forEach(tab => tab.active = false);
// activate the tab the user has clicked on.
tab.active = true;
}
}

