Skip to content

Commit a4aa97e

Browse files
Merge pull request #73 from Travelopia/fix/tabs-component-nested-tabs
Fix tabs web components nested tabs issues
2 parents 77f026b + edc7df0 commit a4aa97e

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

src/tabs/tp-tabs.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ export class TPTabsElement extends HTMLElement {
5959
return;
6060
}
6161

62+
// Get current nested tab if has.
63+
const currentNestedTab = this.getCurrentNestedTab( currentTab );
64+
6265
// Update nav items.
6366
const navItems: NodeListOf<TPTabsNavItemElement> = this.querySelectorAll( 'tp-tabs-nav-item' );
6467
if ( navItems ) {
6568
navItems.forEach( ( navItem: TPTabsNavItemElement ): void => {
66-
if ( navItem.isCurrentTab( currentTab ) ) {
69+
if ( navItem.isCurrentTab( currentTab ) || ( currentNestedTab && navItem.isCurrentTab( currentNestedTab ) ) ) {
6770
navItem.setAttribute( 'active', 'yes' );
6871
} else {
6972
navItem.removeAttribute( 'active' );
@@ -75,7 +78,7 @@ export class TPTabsElement extends HTMLElement {
7578
const tabs: NodeListOf<TPTabsTabElement> = this.querySelectorAll( 'tp-tabs-tab' );
7679
if ( tabs ) {
7780
tabs.forEach( ( tab: TPTabsTabElement ): void => {
78-
if ( currentTab === tab.getAttribute( 'id' ) ) {
81+
if ( currentTab === tab.getAttribute( 'id' ) || ( currentNestedTab && currentNestedTab === tab.getAttribute( 'id' ) ) ) {
7982
tab.setAttribute( 'open', 'yes' );
8083
} else {
8184
tab.removeAttribute( 'open' );
@@ -92,10 +95,8 @@ export class TPTabsElement extends HTMLElement {
9295
return;
9396
}
9497

95-
const urlHash: string = window.location.hash;
96-
if ( '' !== urlHash ) {
97-
this.setAttribute( 'current-tab', urlHash.replace( '#', '' ) );
98-
}
98+
// Set current assiociated tab.
99+
this.setCurrentTab();
99100
}
100101

101102
/**
@@ -104,6 +105,35 @@ export class TPTabsElement extends HTMLElement {
104105
* @param {string} tabId Tab ID.
105106
*/
106107
setCurrentTab( tabId: string = '' ): void {
107-
this.setAttribute( 'current-tab', tabId );
108+
if ( '' !== tabId ) {
109+
this.setAttribute( 'current-tab', tabId );
110+
}
111+
112+
// Set current tab based on current url hash.
113+
const urlHash: string = window.location.hash;
114+
if ( '' !== urlHash ) {
115+
const currentLink: HTMLAnchorElement | null = this.querySelector( `a[href="${ urlHash }"]` );
116+
const currentTab = currentLink?.closest( 'tp-tabs' );
117+
currentTab?.setAttribute( 'current-tab', urlHash.replace( '#', '' ) );
118+
}
119+
}
120+
121+
/**
122+
* Get current nested tab.
123+
*
124+
* @param {string} currentTab Tab ID.
125+
*
126+
* @return {string} If has Nested current tab or empty.
127+
*/
128+
getCurrentNestedTab( currentTab: string = '' ): string {
129+
if ( '' === currentTab ) {
130+
return '';
131+
}
132+
133+
const currentTabElement = this.querySelector( `tp-tabs-tab[id="${ currentTab }"]` );
134+
const currentNestedTabElement = currentTabElement?.querySelector( 'tp-tabs' );
135+
const currentNestedTab = currentNestedTabElement?.getAttribute( 'current-tab' );
136+
137+
return currentNestedTab ?? '';
108138
}
109139
}

0 commit comments

Comments
 (0)