@juspay/svelte-ui-components 2.22.0 → 2.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,20 +1,45 @@
1
1
  <script lang="ts">
2
- import type { TabsProperties } from './properties';
2
+ import type { TabItem, TabsProperties } from './properties';
3
3
  import chevronLeftSvg from '../assets/chevron-left.svg?raw';
4
4
  import chevronRightSvg from '../assets/chevron-right.svg?raw';
5
5
 
6
6
  let {
7
7
  items,
8
- activeIndex = 0,
8
+ activeIndex = $bindable(0),
9
+ activeKey,
9
10
  disabled = false,
10
11
  testId,
11
12
  scrollLeftIcon,
12
13
  scrollRightIcon,
13
14
  tab,
14
15
  classes,
15
- onchange
16
+ onchange,
17
+ onkeychange
16
18
  }: TabsProperties = $props();
17
19
 
20
+ const isObjectMode = $derived(items.length > 0 && typeof items.at(0) === 'object');
21
+
22
+ function toTabItem(item: string | TabItem): TabItem | null {
23
+ return typeof item === 'object' ? item : null;
24
+ }
25
+
26
+ function toStringLabel(item: string | TabItem): string {
27
+ return typeof item === 'string' ? item : item.label;
28
+ }
29
+
30
+ const resolvedActiveIndex = $derived(
31
+ isObjectMode
32
+ ? items.findIndex((item) => {
33
+ const tabItem = toTabItem(item);
34
+ return tabItem !== null && tabItem.key === activeKey;
35
+ })
36
+ : activeIndex
37
+ );
38
+
39
+ function isActiveItem(index: number): boolean {
40
+ return index === resolvedActiveIndex;
41
+ }
42
+
18
43
  let scrollContainer: HTMLDivElement | null = null;
19
44
  let canScrollLeft = $state(false);
20
45
  let canScrollRight = $state(false);
@@ -40,11 +65,30 @@
40
65
  }
41
66
 
42
67
  function handleTabClick(index: number): void {
43
- if (disabled || index === activeIndex) {
68
+ if (disabled) {
44
69
  return;
45
70
  }
46
- activeIndex = index;
47
- onchange?.(index, items[index]);
71
+ if (isObjectMode) {
72
+ const rawItem = items.at(index);
73
+ if (typeof rawItem !== 'object') {
74
+ return;
75
+ }
76
+ const tabItem = toTabItem(rawItem)!;
77
+ if (tabItem.key === activeKey) {
78
+ return;
79
+ }
80
+ onkeychange?.(tabItem.key);
81
+ } else {
82
+ if (index === activeIndex) {
83
+ return;
84
+ }
85
+ const rawLabel = items.at(index);
86
+ if (typeof rawLabel !== 'string') {
87
+ return;
88
+ }
89
+ activeIndex = index;
90
+ onchange?.(index, rawLabel);
91
+ }
48
92
  }
49
93
 
50
94
  function handleKeydown(event: KeyboardEvent, index: number): void {
@@ -90,23 +134,26 @@
90
134
  use:initOverflow
91
135
  onscroll={updateOverflow}
92
136
  >
93
- {#each items as label, index (index)}
137
+ {#each items as item, index (isObjectMode ? (toTabItem(item)?.key ?? index) : index)}
138
+ {@const tabItem = toTabItem(item)}
139
+ {@const label = toStringLabel(item)}
94
140
  <div
95
141
  class="tabs-item"
96
- class:active={index === activeIndex}
142
+ class:active={isActiveItem(index)}
97
143
  role="tab"
98
- aria-selected={index === activeIndex}
144
+ aria-selected={isActiveItem(index)}
99
145
  aria-disabled={disabled ? true : null}
100
- tabindex={index === activeIndex ? 0 : -1}
146
+ tabindex={isActiveItem(index) ? 0 : -1}
147
+ data-pw={tabItem?.testId}
101
148
  onclick={() => handleTabClick(index)}
102
- onkeydown={(e) => handleKeydown(e, index)}
149
+ onkeydown={(event) => handleKeydown(event, index)}
103
150
  >
104
- {#if tab}
105
- {@render tab({ label, index, active: index === activeIndex })}
151
+ {#if typeof tab === 'function'}
152
+ {@render tab({ label, index, active: isActiveItem(index), subtitle: tabItem?.subtitle })}
106
153
  {:else}
107
154
  {label}
108
155
  {/if}
109
- {#if index === activeIndex}
156
+ {#if isActiveItem(index)}
110
157
  <span class="tabs-indicator"></span>
111
158
  {/if}
112
159
  </div>
@@ -1,4 +1,4 @@
1
1
  import type { TabsProperties } from './properties';
2
- declare const Tabs: import("svelte").Component<TabsProperties, {}, "">;
2
+ declare const Tabs: import("svelte").Component<TabsProperties, {}, "activeIndex">;
3
3
  type Tabs = ReturnType<typeof Tabs>;
4
4
  export default Tabs;
@@ -1,10 +1,17 @@
1
1
  import type { Snippet } from 'svelte';
2
+ export type TabItem = {
3
+ key: string;
4
+ label: string;
5
+ testId?: string;
6
+ subtitle?: string;
7
+ };
2
8
  export type TabsProperties = MandatoryTabsProperties & OptionalTabsProperties & TabsEventProperties;
3
9
  export type MandatoryTabsProperties = {
4
- items: string[];
10
+ items: string[] | TabItem[];
5
11
  };
6
12
  export type OptionalTabsProperties = {
7
13
  activeIndex?: number;
14
+ activeKey?: string;
8
15
  disabled?: boolean;
9
16
  testId?: string;
10
17
  scrollLeftIcon?: Snippet;
@@ -13,9 +20,11 @@ export type OptionalTabsProperties = {
13
20
  label: string;
14
21
  index: number;
15
22
  active: boolean;
23
+ subtitle?: string;
16
24
  }]>;
17
25
  classes?: string;
18
26
  };
19
27
  export type TabsEventProperties = {
20
28
  onchange?: (index: number, label: string) => void;
29
+ onkeychange?: (key: string) => void;
21
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/svelte-ui-components",
3
- "version": "2.22.0",
3
+ "version": "2.22.1",
4
4
  "description": "A themeable Svelte 5 UI component library with CSS custom property driven styling",
5
5
  "keywords": [
6
6
  "svelte",