Skip to content

Commit 73851f7

Browse files
committed
fix: set DBTabItem selected state correctly
Set (aria-)selected state via listener on parent tablist to also react on de-selection of item.
1 parent 5542b6c commit 73851f7

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ useDefaultProps<DBTabItemProps>({});
2727

2828
export default function DBTabItem(props: DBTabItemProps) {
2929
const _ref = useRef<HTMLInputElement | any>(null);
30+
3031
// jscpd:ignore-start
3132
const state = useStore<DBTabItemState>({
3233
_selected: false,
@@ -49,18 +50,6 @@ export default function DBTabItem(props: DBTabItemProps) {
4950
props.onChange(event);
5051
}
5152

52-
// We have different ts types in different frameworks, so we need to use any here
53-
54-
useTarget({
55-
stencil: () => {
56-
const selected = (event.target as any)?.['checked'];
57-
state._selected = getBooleanAsString(selected);
58-
},
59-
default: () => {
60-
state._selected = (event.target as any)?.['checked'];
61-
}
62-
});
63-
6453
useTarget({
6554
angular: () =>
6655
handleFrameworkEventAngular(state, event, 'checked'),
@@ -82,6 +71,14 @@ export default function DBTabItem(props: DBTabItemProps) {
8271

8372
useTarget({ react: () => state.handleNameAttribute() });
8473
state.initialized = false;
74+
75+
// deselect this tab when another tab in tablist is selected
76+
_ref.closest('[role=tablist]')?.addEventListener(
77+
'change',
78+
(event: any) => {
79+
state._selected = event.target === _ref;
80+
}
81+
);
8582
}
8683
}, [_ref, state.initialized]);
8784

0 commit comments

Comments
 (0)