aboutsummaryrefslogtreecommitdiffstats
path: root/src/angular/tabs/tabs.component.ts
blob: 595f3040fad77b0e2db780402ba9f636cb25c760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Component, Input, AfterContentInit, ContentChildren, QueryList, HostBinding } from '@angular/core';
import { TabComponent } from './children/tab.component';
import { SvgIconComponent } from "./../../../src/angular/svg-icon/svg-icon.component";
import { Mode, Placement, Size } from './../common/enums';
import template from "./tabs.component.html";

@Component({
    selector: 'sdc-tabs',
    template: template
})

export class TabsComponent implements AfterContentInit {

    @HostBinding('class') classes = 'sdc-tabs sdc-tabs-header';
    @ContentChildren(TabComponent) private tabs: QueryList<TabComponent>;

    public _size = Size.medium;

    public selectTab(tab: TabComponent) {
      // deactivate all tabs
      this.tabs.toArray().forEach((_tab: TabComponent) => {
        _tab.active = false;
        _tab.titleIconMode = Mode.secondary;
      });

      // activate the tab the user has clicked on.
      tab.active = true;
      tab.titleIconMode = Mode.primary;
    }

    public ngAfterContentInit() {
        // get all active tabs
        const activeTabs = this.tabs.filter((tab) => tab.active);

        // if there is no active tab set, activate the first
        if (activeTabs.length === 0) {
            this.selectTab(this.tabs.first);
        }
    }

  }