Skip to content

Commit b8b710c

Browse files
authored
Added Generic param to TabBar
1 parent b4ee7eb commit b8b710c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

packages/tab-bar/src/TabBar.svelte

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@
4141
import type { TabScrollerComponentDev } from '@smui/tab-scroller';
4242
import TabScroller from '@smui/tab-scroller';
4343
44+
type PrimitiveKey = string | number;
45+
type TabKey = $$Generic<Object | PrimitiveKey>;
46+
interface $$Slots {
47+
default: { tab: TabKey };
48+
}
4449
const forwardEvents = forwardEventsBuilder(get_current_component());
4550
4651
// Remember to update types file if you add/remove/rename props.
4752
export let use: ActionArray = [];
4853
let className = '';
4954
export { className as class };
50-
export let tabs: any[] = [];
51-
export let key: (tab: any) => string | number = (tab) => tab;
55+
export let tabs: TabKey[] = [];
56+
export let key: (tab: TabKey) => PrimitiveKey = (tab) => tab as PrimitiveKey;
5257
export let focusOnActivate = true;
5358
export let focusOnProgrammatic = false;
5459
export let useAutomaticActivation = true;
55-
export let active: any | undefined = undefined;
60+
export let active: TabKey | undefined = undefined;
5661
5762
let element: HTMLDivElement;
5863
let instance: MDCTabBarFoundation;
5964
let tabScroller: TabScrollerComponentDev;
60-
let activeIndex = tabs.indexOf(active);
61-
let tabAccessorMap: {
62-
[k: string]: SMUITabAccessor;
63-
[k: number]: SMUITabAccessor;
64-
} = {};
65+
let activeIndex = tabs.indexOf(active!);
66+
let tabAccessorMap: Record<PrimitiveKey, SMUITabAccessor> = {};
6567
let tabAccessorWeakMap = new WeakMap<Object, SMUITabAccessor>();
6668
let skipFocus = false;
6769
6870
setContext('SMUI:tab:focusOnActivate', focusOnActivate);
6971
setContext('SMUI:tab:initialActive', active);
7072
7173
$: if (active !== tabs[activeIndex]) {
72-
activeIndex = tabs.indexOf(active);
74+
activeIndex = tabs.indexOf(active!);
7375
if (instance) {
7476
skipFocus = !focusOnProgrammatic;
7577
instance.activateTab(activeIndex);
@@ -82,7 +84,7 @@
8284
const accessor =
8385
tabs[0] instanceof Object
8486
? tabAccessorWeakMap.get(tabs[0])
85-
: tabAccessorMap[tabs[0]];
87+
: tabAccessorMap[tabs[0] as PrimitiveKey];
8688
8789
if (accessor) {
8890
accessor.forceAccessible(activeIndex === -1);
@@ -134,7 +136,7 @@
134136
const activeElement = document.activeElement;
135137
return tabElements.indexOf(activeElement as HTMLElement);
136138
},
137-
getIndexOfTabById: (id) => tabs.indexOf(id),
139+
getIndexOfTabById: (id) => tabs.indexOf(id as TabKey),
138140
getTabListLength: () => tabs.length,
139141
notifyTabActivated: (index) =>
140142
dispatch(

0 commit comments

Comments
 (0)