unicorn-components
Version:
<a target="_blank" href="https://getunicorn.io"><img src="https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2017/Jul/07/2615006260-5-nitsnetsstudios-ondemand-UNI_avatar.png" align="left"></a>
57 lines (48 loc) • 1.95 kB
text/typescript
import { AfterContentInit, Component, ContentChildren, ElementRef, EventEmitter, HostBinding, Input, OnChanges, Output, QueryList } from '@angular/core';
import { UniOption } from '../../../models/option';
import { UniTabsItemComponent } from './item/item.component';
export class UniTabsComponent implements OnChanges, AfterContentInit {
componentClass = true;
size: 'small';
tabs: UniOption[];
tabSelected: string | number;
tabSelectedChange = new EventEmitter<string | number>();
tabsElements: QueryList<UniTabsItemComponent>;
private inited = false;
constructor(private elementRef: ElementRef) { }
ngOnChanges(changes) {
this.applySelected();
}
ngAfterContentInit() {
this.inited = true;
if (this.tabsElements.length) {
this.tabs = this.tabsElements.map(t => new UniOption({ label: t.label, value: t.value }));
}
this.applySelected();
}
applySelected() {
if (!this.inited) { return; }
if (!this.tabs || !this.tabs.length) {
this.selectTab(null, -1); return;
}
let tab: UniOption = null;
tab = this.tabs.find((t, i) => t.value && t.value === this.tabSelected || !t.value && i === this.tabSelected);
this.selectTab(tab, this.tabs.indexOf(tab));
}
selectTab(tab: UniOption, i: number) {
if (tab && tab.value) {
if (this.tabSelected === tab.value) { return; }
this.tabSelected = tab.value;
this.tabSelectedChange.emit(tab.value);
} else {
if (this.tabSelected === i) { return; }
this.tabSelected = i;
this.tabSelectedChange.emit(i);
}
}
}