Skip to content

Commit f641f18

Browse files
committed
fix(components): fix tabs selected problem with daisy implementation
1 parent c4d6f84 commit f641f18

File tree

2 files changed

+44
-42
lines changed
  • packages

2 files changed

+44
-42
lines changed

packages/daisy/src/components/tabs/tab.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ import { Tab as HeadlessTab } from '@qwik-ui/headless';
33

44
interface TabProps {
55
class?: string;
6-
className?: string;
7-
isActive?: boolean;
86
isLifted?: boolean;
97
isBordered?: boolean;
108
onClick$?: PropFunction<(clicked: number) => void>;
119
}
1210

1311
export const Tab = component$(
14-
({ isActive, isBordered, isLifted, onClick$, ...props }: TabProps) => {
12+
({ isBordered, isLifted, onClick$, ...props }: TabProps) => {
1513
return (
1614
<HeadlessTab
1715
onClick={onClick$}
18-
class={`tab ${isActive ? 'tab-active' : ''} ${
19-
isBordered ? 'tab-bordered' : ''
20-
} ${isLifted ? 'tab-lifted' : ''}`}
16+
class={`tab ${isBordered ? 'tab-bordered' : ''} ${
17+
isLifted ? 'tab-lifted' : ''
18+
}`}
19+
selectedClassName="tab-active"
2120
{...props}
2221
>
2322
<Slot />

packages/headless/src/components/tabs/tabs.tsx

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -82,48 +82,51 @@ export const TabList = component$((props: TabListProps) => {
8282
interface TabProps {
8383
onClick?: PropFunction<(clicked: number) => void>;
8484
class?: string;
85+
selectedClassName?: string;
8586
}
8687

8788
// Tab button inside of a tab list
88-
export const Tab = component$(({ onClick, ...props }: TabProps) => {
89-
const contextService = useContext(tabsContext);
90-
const thisTabIndex = useSignal(0);
91-
92-
useMount$(async () => {
93-
thisTabIndex.value = await await contextService.getNextTabIndex();
94-
});
95-
const isSelected = () =>
96-
thisTabIndex.value === contextService.selectedIndex.value;
89+
export const Tab = component$(
90+
({ selectedClassName, onClick, ...props }: TabProps) => {
91+
const contextService = useContext(tabsContext);
92+
const thisTabIndex = useSignal(0);
9793

98-
const selectIfAutomatic = $(() => {
99-
if (contextService.behavior === 'automatic') {
100-
contextService.selectedIndex.value = thisTabIndex.value;
101-
}
102-
});
94+
useMount$(async () => {
95+
thisTabIndex.value = await await contextService.getNextTabIndex();
96+
});
97+
const isSelected = () =>
98+
thisTabIndex.value === contextService.selectedIndex.value;
10399

104-
return (
105-
<button
106-
id={`${contextService.tabsHash}-tab-${thisTabIndex.value}`}
107-
type="button"
108-
role="tab"
109-
onFocus$={selectIfAutomatic}
110-
onMouseEnter$={selectIfAutomatic}
111-
aria-selected={isSelected()}
112-
aria-controls={`tabpanel-${thisTabIndex.value}`}
113-
class={`${isSelected() ? 'selected' : ''}${
114-
props.class ? ` ${props.class}` : ''
115-
}`}
116-
onClick$={$(() => {
100+
const selectIfAutomatic = $(() => {
101+
if (contextService.behavior === 'automatic') {
117102
contextService.selectedIndex.value = thisTabIndex.value;
118-
if (onClick) {
119-
onClick(thisTabIndex.value);
120-
}
121-
})}
122-
>
123-
<Slot />
124-
</button>
125-
);
126-
});
103+
}
104+
});
105+
106+
return (
107+
<button
108+
id={`${contextService.tabsHash}-tab-${thisTabIndex.value}`}
109+
type="button"
110+
role="tab"
111+
onFocus$={selectIfAutomatic}
112+
onMouseEnter$={selectIfAutomatic}
113+
aria-selected={isSelected()}
114+
aria-controls={`tabpanel-${thisTabIndex.value}`}
115+
class={`${isSelected() ? `selected ${selectedClassName}` : ''}${
116+
props.class ? ` ${props.class}` : ''
117+
}`}
118+
onClick$={$(() => {
119+
contextService.selectedIndex.value = thisTabIndex.value;
120+
if (onClick) {
121+
onClick(thisTabIndex.value);
122+
}
123+
})}
124+
>
125+
<Slot />
126+
</button>
127+
);
128+
}
129+
);
127130

128131
interface TabPanelProps {
129132
class?: string;

0 commit comments

Comments
 (0)