File

packages/ui/src/lib/utility/tabs/tabs.component.ts

Description

The TabsComponent holds serveral instances of TabComponent. https://components.entrecode.de/ui/tabs?e=1

Implements

AfterContentInit

Metadata

selector ec-tabs
templateUrl ./tabs.component.html

Index

Properties
Methods
Inputs

Constructor

constructor(router: Router, route: ActivatedRoute)
Parameters :
Name Type Optional
router Router No
route ActivatedRoute No

Inputs

selected
Type : TabComponent

You can set the initially selected tab by passing a TabComponent in (e.g. via #variable)

Methods

initTabs
initTabs()
Returns : void
ngAfterContentInit
ngAfterContentInit()
Returns : void
select
select(tab: TabComponent, skipRoute)

Selects the given tab (Component).

Parameters :
Name Type Optional Default value
tab TabComponent No
skipRoute No false
Returns : void
selectByUrl
selectByUrl(url: string)

Selects the tab associated with the route present in the given url

Parameters :
Name Type Optional
url string No
Returns : void

Properties

tabs
Type : QueryList<TabComponent>
Decorators :
@ContentChildren(TabComponent)

The nested Tabs

Tabs

This is how you use Tabs:

<ec-tabs>
  <ec-tab label="Tab A">
    <img src="https://unsplash.it/300/400">
  </ec-tab>
  <ec-tab label="Tab B">
    <img src="https://unsplash.it/200/300">
  </ec-tab>
  <ec-tab label="Tab C" selected>
    <img src="https://unsplash.it/400/500">
  </ec-tab>
  <ec-tab label="Tab D">
    <img src="https://unsplash.it/200/100">
  </ec-tab>
</ec-tabs>

TODO:

  • *ngIf
  • Slot for templated Tab Header

import { AfterContentInit, Component, ContentChildren, Input, QueryList } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { TabComponent } from '../tab/tab.component';

/** The TabsComponent holds serveral instances of TabComponent.
 * <example-url>https://components.entrecode.de/ui/tabs?e=1</example-url>
 */
@Component({
  selector: 'ec-tabs',
  templateUrl: './tabs.component.html',
})
export class TabsComponent implements AfterContentInit {
  /** The nested Tabs */
  @ContentChildren(TabComponent) tabs: QueryList<TabComponent>;
  /** You can set the initially selected tab by passing a TabComponent in (e.g. via #variable) */
  @Input() selected: TabComponent;

  constructor(private router: Router, private route: ActivatedRoute) {
    this.router.events.subscribe((event) => {
      if (event instanceof NavigationEnd) {
        this.selectByUrl(event.url);
      }
    });
  }

  /** Selects the tab associated with the route present in the given url */
  selectByUrl(url: string) {
    if (!url || !this.tabs) {
      return;
    }
    const paths = new URL(url, window.location.origin).pathname.split('/');
    const match = this.tabs.find((tab) => paths[paths.length - 1].indexOf(tab.route) !== -1);
    if (match) {
      this.select(match, true);
    }
  }

  initTabs() {
    this.tabs.forEach((tab) => {
      tab.parent = this;
      if (tab.el.nativeElement.getAttribute('selected') !== null) {
        this.select(tab);
      }
    });
    this.selectByUrl(this.router.url);
  }

  ngAfterContentInit() {
    this.initTabs();
  }

  /** Selects the given tab (Component). */
  select(tab: TabComponent, skipRoute = false) {
    if (this.selected) {
      this.selected.deactivate();
    }
    this.selected = tab;
    tab.activate();
    if (tab.route && !skipRoute) {
      this.router.navigate(['../' + tab.route], { relativeTo: this.route });
    }
  }
}
<div class="ec-tabs">
  <div class="ec-tabs-nav">
    <div *ngFor="let tab of tabs; let i = index" class="ec-tabs-nav__item">
      <a (click)="select(tab)" class="ec-tabs-nav__link" [class.is-active]="tab.selected">{{ tab.label }}</a>
    </div>
  </div>
  <ng-content select="ec-tab" #tab></ng-content>
</div>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""