Skip to content

Commit 512a992

Browse files
authored
[Tab] Fix activating tabs by URL hash (#135)
1 parent 7ac3b40 commit 512a992

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

src/js/tab/tab.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
return;
4242
}
4343

44-
if (this.findAncestor(this, 'joomla-tab')) {
44+
if (this.parentNode.closest('joomla-tab')) {
4545
this.isNested = true;
4646
}
4747

@@ -72,11 +72,11 @@
7272

7373
// Add the parent tab to the array for activation
7474
if (element) {
75-
const currentTabSet = this.findAncestor(element, 'joomla-tab');
76-
const parentTabSet = this.findAncestor(currentTabSet, 'joomla-tab');
75+
const currentTabSet = element.closest('joomla-tab');
76+
77+
if (this.isNested) {
78+
const parentTab = currentTabSet.closest('section');
7779

78-
if (parentTabSet) {
79-
const parentTab = this.findAncestor(currentTabSet, 'section');
8080
if (parentTab) {
8181
tabLinkHash.push(`#tab-${parentTab.id}`);
8282
}
@@ -151,11 +151,11 @@
151151

152152
if (element) {
153153
// Activate any parent tabs (nested tables)
154-
const currentTabSet = this.findAncestor(element, 'joomla-tab');
155-
const parentTabSet = this.findAncestor(currentTabSet, 'joomla-tab');
154+
const currentTabSet = element.closest('joomla-tab');
156155

157-
if (parentTabSet) {
158-
const parentTab = this.findAncestor(currentTabSet, 'section');
156+
if (this.isNested) {
157+
const parentTabSet = currentTabSet.closest('joomla-tab');
158+
const parentTab = currentTabSet.closest('section');
159159
parentTabSet.showTab(parentTab);
160160
// Now activate the given tab
161161
this.show(element);
@@ -273,7 +273,7 @@
273273
}
274274

275275
showTab(tab) {
276-
const tabLink = document.querySelector(`#${tab.id}`);
276+
const tabLink = document.querySelector(`#tab-${tab.id}`);
277277
tabLink.click();
278278
}
279279

@@ -383,18 +383,6 @@
383383
}
384384
}
385385

386-
findAncestor(el, tagName) {
387-
let element = el;
388-
while (element.nodeName.toLowerCase() !== tagName) {
389-
// Ensure we haven't reached the top of the dom tree
390-
if (element.parentElement === null) {
391-
return false;
392-
}
393-
element = element.parentElement;
394-
}
395-
return element;
396-
}
397-
398386
/* Method to dispatch events */
399387
dispatchCustomEvent(eventName, element, related) {
400388
const OriginalCustomEvent = new CustomEvent(eventName, { bubbles: true, cancelable: true });

0 commit comments

Comments
 (0)