Skip to content

Commit bd57d23

Browse files
committed
feat(tabset): implement better support for dynamic tabs
1 parent ee9c9a7 commit bd57d23

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/elements/hx-tab/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HXElement } from '../../interfaces/HXElement/index.js';
2+
import { generateId } from '../../utils';
23

34
/**
45
* Fires when non-current tab is clicked.
@@ -22,6 +23,7 @@ export class HXTabElement extends HXElement {
2223
}
2324

2425
$onConnect () {
26+
this.$defaultAttribute('id', `tab-${generateId()}`);
2527
this.$upgradeProperty('current');
2628
this.$defaultAttribute('role', 'tab');
2729
this.setAttribute('aria-selected', this.current);

src/elements/hx-tabpanel/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HXElement } from '../../interfaces/HXElement/index.js';
2-
import { onScroll } from '../../utils';
2+
import { onScroll, generateId } from '../../utils';
33

44
/**
55
* Fires when the element's contents are concealed.
@@ -32,6 +32,7 @@ export class HXTabpanelElement extends HXElement {
3232
}
3333

3434
$onConnect () {
35+
this.$defaultAttribute('id', `tabpanel-${generateId()}`);
3536
this.$defaultAttribute('role', 'tabpanel');
3637
this.$upgradeProperty('open');
3738
this.setAttribute('aria-expanded', this.open);

src/elements/hx-tabset/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export class HXTabsetElement extends HXElement {
8484
}
8585
}
8686

87+
this._setupIds(); // account for dynamic tabs
8788
this.setAttribute('current-tab', idx);
8889
}
8990

@@ -155,6 +156,7 @@ export class HXTabsetElement extends HXElement {
155156
* elements changes after tabset connects to the DOM.
156157
*/
157158
update () {
159+
this._setupIds();
158160
this._activateTab(this.currentTab);
159161
}
160162

src/elements/hx-tabset/index.spec.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ describe('<hx-tabset> component tests', () => {
294294
expect(len).to.equal(0);
295295
});
296296

297-
it.skip(`[FEATURE] should add a dynamic tab with id to empty ${template}`, async () => {
297+
it(`[FEATURE] should add a dynamic tab with id to empty ${template}`, async () => {
298298
const mockup = `
299299
<hx-tabset>
300300
<hx-tablist>
@@ -308,26 +308,60 @@ describe('<hx-tabset> component tests', () => {
308308
const tabs = fragment.tabs;
309309
let firstTabCount = tabs.length;
310310

311-
312-
// add a tab
311+
// add a dyanmic tab
313312
const tab = document.createElement('hx-tab');
314313
tab.innerHTML = "dynamic tab";
315314
fragment.querySelector('hx-tablist').appendChild(tab);
316315

317316
const tabHasId = fragment.querySelector(elSelector).hasAttribute('id');
318-
//fragment.update(); // manually add tab
319317

320318
// add panel
321319
const tabpanel = document.createElement('hx-tabpanel');
322320
tabpanel.innerHTML = "dynamic tab panel";
323321
fragment.querySelector('hx-tabcontent').appendChild(tabpanel);
324322
const secoundTabCount = fragment.tabs.length;
325323

326-
//fragment.update(); // manually add tabpanel
324+
fragment.update();
327325

328326
expect(tabHasId).to.be.true;
329327
expect(firstTabCount).to.equal(0);
330328
expect(secoundTabCount).to.equal(1);
331329
});
330+
331+
it(`[FEATURE] should add a dynamic tab with id to a populated ${template}`, async () => {
332+
const mockup = `
333+
<hx-tabset>
334+
<hx-tablist>
335+
<hx-tab id="dynamicTestTab"></hx-tab>
336+
</hx-tablist>
337+
<hx-tabcontent>
338+
<hx-tabpanel id="dynamicTestTabpanel"></hx-tabpanel>
339+
</hx-tabcontent>
340+
</hx-tabset>`;
341+
342+
const elSelector = 'hx-tablist > hx-tab';
343+
const fragment = /** @type {HXTabsetElement} */ await fixture(mockup);
344+
const tabs = fragment.tabs;
345+
let firstTabCount = tabs.length;
346+
347+
// add a dyanmic tab
348+
const tab = document.createElement('hx-tab');
349+
tab.innerHTML = "dynamic tab";
350+
fragment.querySelector('hx-tablist').appendChild(tab);
351+
352+
const tabHasId = fragment.querySelector(elSelector).hasAttribute('id');
353+
354+
// add panel
355+
const tabpanel = document.createElement('hx-tabpanel');
356+
tabpanel.innerHTML = "dynamic tab panel";
357+
fragment.querySelector('hx-tabcontent').appendChild(tabpanel);
358+
const secoundTabCount = fragment.tabs.length;
359+
360+
fragment.update(); // update tab
361+
362+
expect(tabHasId).to.be.true;
363+
expect(firstTabCount).to.equal(1);
364+
expect(secoundTabCount).to.equal(2);
365+
});
332366
});
333367
});

0 commit comments

Comments
 (0)