@@ -7,28 +7,20 @@ import {
77 useComputed$ ,
88 useTask$ ,
99 $ ,
10- useVisibleTask$ ,
11- useSignal ,
1210} from '@builder.io/qwik' ;
1311import { tabsContextId } from './tabs-context-id' ;
1412
1513export interface TabProps {
16- for : string ;
1714 onClick ?: PropFunction < ( ) => void > ;
1815 class ?: string ;
1916 selectedClassName ?: string ;
2017}
2118
22- // Tab button inside of a tab list
2319export const Tab = component$ ( ( props : TabProps ) => {
2420 const contextService = useContext ( tabsContextId ) ;
2521
2622 const uniqueId = useId ( ) ;
2723
28- const currentTabIndex = useSignal ( 0 ) ;
29-
30- const isSelectedSignal = useSignal ( false ) ;
31-
3224 useTask$ ( ( { cleanup } ) => {
3325 contextService . tabsChanged$ ( ) ;
3426
@@ -37,24 +29,25 @@ export const Tab = component$((props: TabProps) => {
3729 } ) ;
3830 } ) ;
3931
40- useTask$ ( ( { track } ) => {
41- track ( contextService . indexByTabId ) ;
42- console . log ( 'contextService.indexByTabId' , contextService . indexByTabId ) ;
43- currentTabIndex . value = contextService . indexByTabId [ uniqueId ] ;
32+ const isSelectedSignal = useComputed$ ( ( ) => {
33+ return (
34+ contextService . selectedIndex . value ===
35+ contextService . tabsMap [ uniqueId ] ?. index
36+ ) ;
4437 } ) ;
4538
46- useVisibleTask$ ( ( ) => {
47- console . log (
48- 'contextService.selectedIndex.value' ,
49- contextService . selectedIndex . value
50- ) ;
51- console . log ( 'currentTabIndex.value' , currentTabIndex . value ) ;
52- isSelectedSignal . value =
53- contextService . selectedIndex . value === currentTabIndex . value ;
39+ // TODO: Figure out a way to fix this shitty hack :)
40+ useTask$ ( ( { track } ) => {
41+ track ( ( ) => isSelectedSignal . value ) ;
42+
43+ if ( isSelectedSignal . value ) {
44+ contextService . showTabs$ ( ) ;
45+ }
5446 } ) ;
5547
5648 const selectTab$ = $ ( ( ) => {
57- contextService . selectedIndex . value = currentTabIndex . value ;
49+ contextService . selectedIndex . value =
50+ contextService . tabsMap [ uniqueId ] ?. index || 0 ;
5851 } ) ;
5952
6053 const selectIfAutomatic$ = $ ( ( ) => {
@@ -65,19 +58,19 @@ export const Tab = component$((props: TabProps) => {
6558
6659 return (
6760 < button
68- data-for = { props . for }
61+ id = { 'tab-' + uniqueId }
6962 data-tab-id = { uniqueId }
7063 type = "button"
7164 role = "tab"
7265 onFocus$ = { selectIfAutomatic$ }
7366 onMouseEnter$ = { selectIfAutomatic$ }
7467 aria-selected = { isSelectedSignal . value }
75- aria-controls = { 'tabpanel-' + props . for }
76- class = { `${ isSelectedSignal ? `selected ${ props . selectedClassName } ` : '' } ${
77- props . class ? ` ${ props . class } ` : ''
78- } `}
79- onClick$ = { async ( ) => {
80- await selectTab$ ( ) ;
68+ aria-controls = { 'tabpanel-' + contextService . tabsMap [ uniqueId ] ?. tabPanelId }
69+ class = { `${
70+ isSelectedSignal . value ? `selected ${ props . selectedClassName } ` : ''
71+ } ${ props . class ? ` ${ props . class } ` : '' } `}
72+ onClick$ = { ( ) => {
73+ selectTab$ ( ) ;
8174 if ( props . onClick ) {
8275 props . onClick ( ) ;
8376 }
0 commit comments