Skip to content

Commit ba69c17

Browse files
authored
Fix/1843 [tabs] tab using btn more does not trigger routerlink (#1856)
* trying to improve clicking on a tab when clicking menu item * Clicking menu item with link, partially working * tabs improvements, almost working with links * fixing selecting tab without links from menu * links with menu button tests * cleanup * changeset * removed unnecessary keydown
1 parent 2c3534f commit ba69c17

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

.changeset/cruel-years-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sl-design-system/tabs': patch
3+
---
4+
5+
Fix triggering tabs from `sl-menu-button` with links (routerLink).

packages/components/tabs/src/tab-group.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,47 @@ describe('sl-tab-group', () => {
328328
expect(menuButton).to.exist;
329329
});
330330
});
331+
332+
describe('links with menu button', () => {
333+
let tab: Tab;
334+
let link: HTMLAnchorElement;
335+
336+
beforeEach(async () => {
337+
el = await fixture(html`
338+
<sl-tab-group style="width: 50px">
339+
<sl-tab href="javascript:void(0)">One</sl-tab>
340+
<sl-tab href="javascript:void(0)">Two</sl-tab>
341+
<sl-tab disabled href="javascript:void(0)">Disabled</sl-tab>
342+
<sl-tab href="javascript:void(0)">Four</sl-tab>
343+
</sl-tab-group>
344+
`);
345+
346+
// We need to wait for the RovingTabindexController to do its thing
347+
await new Promise(resolve => setTimeout(resolve, 50));
348+
349+
tab = el.querySelector('sl-tab')!;
350+
link = tab.renderRoot.querySelector('a')!;
351+
});
352+
353+
it('should have a menu button', async () => {
354+
await el.updateComplete;
355+
356+
const menuButton = el.renderRoot.querySelector('sl-menu-button');
357+
358+
expect(menuButton).to.exist;
359+
});
360+
361+
it('should click the tab link when the user clicks the menu item', () => {
362+
const onClick = spy();
363+
364+
link.addEventListener('click', onClick);
365+
366+
const menuButton = el.renderRoot.querySelector('sl-menu-button'),
367+
menuItems = menuButton!.querySelectorAll('sl-menu-item');
368+
369+
menuItems[0].click();
370+
371+
expect(onClick).to.have.been.called;
372+
});
373+
});
331374
});

packages/components/tabs/src/tab-group.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export class TabGroup extends ScopedElementsMixin(LitElement) {
295295

296296
#onClick(event: Event & { target: HTMLElement }): void {
297297
const tab = event.target.closest('sl-tab');
298+
298299
if (!tab) {
299300
return;
300301
}
@@ -317,6 +318,10 @@ export class TabGroup extends ScopedElementsMixin(LitElement) {
317318
}
318319

319320
#onMenuItemClick(tab: Tab): void {
321+
if (tab.href) {
322+
tab.renderRoot.querySelector('a')?.click();
323+
}
324+
320325
this.#updateSelectedTab(tab);
321326
}
322327

0 commit comments

Comments
 (0)