From 4df060f2b59f892767631bd896963eb55f02f53c Mon Sep 17 00:00:00 2001 From: mruane1 Date: Wed, 18 Dec 2024 12:07:05 +0000 Subject: [PATCH] refactor(selected-index): add change event for programmatic tab change (#1843) This change introduces an event to allow programmatic changes to the tab index improving the flexibility of the tab management --- web-components/src/[sandbox]/examples/tabs.ts | 60 ++++++++++++++++++- web-components/src/components/tabs/Tabs.ts | 9 ++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/web-components/src/[sandbox]/examples/tabs.ts b/web-components/src/[sandbox]/examples/tabs.ts index 2f8737c3f..1df8cb35a 100644 --- a/web-components/src/[sandbox]/examples/tabs.ts +++ b/web-components/src/[sandbox]/examples/tabs.ts @@ -17,7 +17,7 @@ import { Tabs } from "@/components/tabs/Tabs"; import "@/components/toggle-switch/ToggleSwitch"; import "@/components/tooltip/Tooltip"; import svgWxm from "@img/wxm.svg"; -import { css, customElement, html, internalProperty, LitElement } from "lit-element"; +import { css, customElement, html, internalProperty, LitElement, property } from "lit-element"; import { TemplateResult } from "lit-html"; import { repeat } from "lit-html/directives/repeat"; import { unsafeHTML } from "lit-html/directives/unsafe-html"; @@ -195,6 +195,7 @@ export class TabsTemplateSandbox extends LitElement { @internalProperty() private currentTabsOrder = this.defaultTabsOrder; @internalProperty() private isSingleButtonResetEnabled = false; closeTabName = ""; + @property({ type: Number }) selectedTabIndex = 0; private setUpTabs() { this.tabs = { @@ -364,6 +365,10 @@ export class TabsTemplateSandbox extends LitElement { if (draggableTabsOrder) this.currentTabsOrder = draggableTabsOrder.split(","); } + handleTabIndexChange() { + this.selectedTabIndex = this.selectedTabIndex === 0 ? 1 : 0; + } + render() { return html`
@@ -509,6 +514,59 @@ export class TabsTemplateSandbox extends LitElement { `} +

Allows the tab index to be changed programmatically without requiring a mouse click event on the tab item

+ this.handleTabIndexChange()}> + Programmatically change tab index + +
+ + + Consult + + + + +
+
+

Alan Johnson

+

1234434

+
+
+
+ +
+
+

Mark Corrigan

+

43454334

+
+
+
+
+
+ + Transfer + + + + +
+
+

Jermey Usborne

+

83498347

+
+
+
+ +
+
+

Sophie Chapman

+

22384758

+
+
+
+
+
+

diff --git a/web-components/src/components/tabs/Tabs.ts b/web-components/src/components/tabs/Tabs.ts index 69646a967..5c71ad026 100644 --- a/web-components/src/components/tabs/Tabs.ts +++ b/web-components/src/components/tabs/Tabs.ts @@ -55,7 +55,7 @@ export namespace Tabs { @property({ type: Boolean, attribute: "draggable" }) draggable = false; @property({ type: String }) direction: "horizontal" | "vertical" = "horizontal"; @property({ type: Number, attribute: "more-items-scroll-limit" }) moreItemsScrollLimit = Number.MAX_SAFE_INTEGER; - + @property({ type: Number, reflect: true, attribute: 'selected-index' }) selectedIndex = 0; @property({ type: Number }) delay = 0; @property({ type: Number }) animation = 100; @property({ type: String, attribute: "ghost-class" }) ghostClass = ""; @@ -602,6 +602,9 @@ export namespace Tabs { private updateSelectedTab(newSelectedIndex: number) { const { tabs, panels } = this; const oldSelectedIndex = this.tabs.findIndex((element) => element.hasAttribute("selected")); + if (oldSelectedIndex === -1) { + return; + } if (tabs && panels) { [oldSelectedIndex, newSelectedIndex].forEach((index) => { @@ -1082,6 +1085,10 @@ export namespace Tabs { if (changedProperties.has("tabsId")) { this.selectTabFromStorage(); } + + if (changedProperties.has("selectedIndex")) { + this.updateSelectedTab(this.selectedIndex); + } } render() {