Skip to content

Commit 91b5a58

Browse files
committed
fix: pass correct tab item index from DBTab parent to child
set child active after event registered, remove event listerner on unmount
1 parent 90f72f2 commit 91b5a58

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

packages/components/src/components/tab-item/tab-item.lite.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
onMount,
3+
onUnMount,
34
onUpdate,
45
Show,
56
useDefaultProps,
@@ -28,6 +29,10 @@ useDefaultProps<DBTabItemProps>({});
2829
export default function DBTabItem(props: DBTabItemProps) {
2930
const _ref = useRef<HTMLInputElement | any>(null);
3031

32+
function setSelectedOnChange(event: any) {
33+
state._selected = event.target === _ref;
34+
}
35+
3136
// jscpd:ignore-start
3237
const state = useStore<DBTabItemState>({
3338
_selected: false,
@@ -65,20 +70,18 @@ export default function DBTabItem(props: DBTabItemProps) {
6570

6671
onUpdate(() => {
6772
if (state.initialized && _ref) {
68-
if (props.active) {
69-
_ref.click();
70-
}
71-
7273
useTarget({ react: () => state.handleNameAttribute() });
7374
state.initialized = false;
7475

7576
// deselect this tab when another tab in tablist is selected
7677
_ref.closest('[role=tablist]')?.addEventListener(
7778
'change',
78-
(event: any) => {
79-
state._selected = event.target === _ref;
80-
}
79+
setSelectedOnChange
8180
);
81+
82+
if (props.active) {
83+
_ref.click();
84+
}
8285
}
8386
}, [_ref, state.initialized]);
8487

@@ -88,6 +91,13 @@ export default function DBTabItem(props: DBTabItemProps) {
8891
}
8992
}, [props.name]);
9093

94+
onUnMount(() => {
95+
_ref.closest('[role=tablist]')?.removeEventListener(
96+
'change',
97+
setSelectedOnChange
98+
);
99+
});
100+
91101
return (
92102
<li class={cls('db-tab-item', props.className)} role="none">
93103
<label

packages/components/src/components/tabs/tabs.lite.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ export default function DBTabs(props: DBTabsProps) {
170170
if (tabItem) {
171171
const list = tabItem.parentElement;
172172
if (list) {
173-
const indices = Array.from(list.childNodes).indexOf(
173+
const tabIndex = Array.from(list.children).indexOf(
174174
tabItem
175175
);
176176
if (props.onIndexChange) {
177-
props.onIndexChange(indices);
177+
props.onIndexChange(tabIndex);
178178
}
179179

180180
if (props.onTabSelect) {

packages/components/src/components/tabs/tabs.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const testActions = () => {
4242
test('should be clickable', async ({ mount }) => {
4343
expect(tabIndex).toBe(undefined);
4444

45+
// Beware: the comments below actually change the selector for vue
46+
// this is necessary because vue will not trigger a check on an list element but requires the actual
47+
// radio button element, which has the role=tab
4548
const component = await mount(comp);
4649
await component
4750
.getByTestId('test2')

0 commit comments

Comments
 (0)