From db96e8cc07e2b73e9a2a3b3023b1dc572daeeb89 Mon Sep 17 00:00:00 2001 From: Gyulai Levente <47368351+gyulus3@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:57:19 +0100 Subject: [PATCH] fix(tabs): set selectedIndex only if differ from previous selectedIndex --- .changeset/small-pumpkins-peel.md | 5 +++++ packages/ui/components/tabs/src/LionTabs.js | 4 ++++ packages/ui/components/tabs/test/lion-tabs.test.js | 8 ++++++++ 3 files changed, 17 insertions(+) create mode 100644 .changeset/small-pumpkins-peel.md diff --git a/.changeset/small-pumpkins-peel.md b/.changeset/small-pumpkins-peel.md new file mode 100644 index 0000000000..0fcb13cae9 --- /dev/null +++ b/.changeset/small-pumpkins-peel.md @@ -0,0 +1,5 @@ +--- +'@lion/ui': patch +--- + +fix(tabs): set selectedIndex only if value differ from previous value diff --git a/packages/ui/components/tabs/src/LionTabs.js b/packages/ui/components/tabs/src/LionTabs.js index 493ef171f0..bb9849acc9 100644 --- a/packages/ui/components/tabs/src/LionTabs.js +++ b/packages/ui/components/tabs/src/LionTabs.js @@ -356,7 +356,11 @@ export class LionTabs extends LitElement { * @param {number} value The new index */ set selectedIndex(value) { + if (value === this.__selectedIndex) { + return; + } const stale = this.__selectedIndex; + /** @type {number | undefined} */ this.__selectedIndex = value; this.__updateSelected(false); this.dispatchEvent(new Event('selected-changed')); diff --git a/packages/ui/components/tabs/test/lion-tabs.test.js b/packages/ui/components/tabs/test/lion-tabs.test.js index d3f8b4979e..6511a0566d 100644 --- a/packages/ui/components/tabs/test/lion-tabs.test.js +++ b/packages/ui/components/tabs/test/lion-tabs.test.js @@ -75,6 +75,14 @@ describe('', () => { expect(spy).to.have.been.calledOnce; }); + it('does not send event "selected-changed" after selecting currently selected tab', async () => { + const el = /** @type {LionTabs} */ (await fixture(basicTabs)); + const spy = sinon.spy(); + el.addEventListener('selected-changed', spy); + el.selectedIndex = 0; + expect(spy).not.to.have.been.called; + }); + it('logs warning if unequal amount of tabs and panels', async () => { const stub = sinon.stub(console, 'warn'); await fixture(html`