@pega/dx-component-builder-sdk
Version:
Utility for building custom UI components
77 lines (64 loc) • 2.58 kB
text/typescript
import { Component, OnInit, Input, forwardRef, OnDestroy } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { MatTabsModule } from '@angular/material/tabs';
import { AngularPConnectData, AngularPConnectService } from '@pega/angular-sdk-components';
import { getTransientTabs, getVisibleTabs, tabClick } from '@pega/angular-sdk-components';
import { CommonModule } from '@angular/common';
import { ComponentMapperComponent } from '@pega/angular-sdk-components';
export class SubTabsComponent implements OnInit, OnDestroy {
pConn$: typeof PConnect;
formGroup$: FormGroup;
angularPConnectData: AngularPConnectData = {};
arChildren$: any[];
defaultTabIndex = 0;
currentTabId = this.defaultTabIndex.toString();
tabItems: any[];
availableTabs: any;
constructor(private angularPConnect: AngularPConnectService) {}
ngOnInit(): void {
// First thing in initialization is registering and subscribing to the AngularPConnect service
this.angularPConnectData = this.angularPConnect.registerAndSubscribeComponent(this, this.onStateChange);
this.checkAndUpdate();
}
ngOnDestroy() {
if (this.angularPConnectData.unsubscribeFn) {
this.angularPConnectData.unsubscribeFn();
}
}
onStateChange() {
this.checkAndUpdate();
}
checkAndUpdate() {
// Should always check the bridge to see if the component should
// update itself (re-render)
const bUpdateSelf = this.angularPConnect.shouldComponentUpdate(this);
// ONLY call updateSelf when the component should update
if (bUpdateSelf) {
this.updateSelf();
}
}
updateSelf() {
const children = this.pConn$?.getChildren();
const deferLoadedTabs = children[0];
this.availableTabs = getVisibleTabs(deferLoadedTabs, 'tabsSubs');
this.updateTabContent();
}
updateTabContent() {
const tempTabItems = getTransientTabs(this.availableTabs, this.currentTabId, this.tabItems);
this.tabItems = tempTabItems;
}
handleTabClick(event) {
const { index } = event;
this.currentTabId = index.toString();
tabClick(index, this.availableTabs, this.currentTabId, this.tabItems);
const tempTabItems = getTransientTabs(this.availableTabs, this.currentTabId, this.tabItems);
this.tabItems[index].content = tempTabItems[index].content;
}
}