@@ -6,11 +6,11 @@ import {
6
6
useComputed$ ,
7
7
useContext ,
8
8
useSignal ,
9
- useVisibleTask $,
9
+ sync $,
10
10
} from '@builder.io/qwik' ;
11
- import { KeyCode } from '../../utils/key-code.type' ;
12
11
import { TAB_PANEL_ID_PREFIX } from './tab-panel' ;
13
12
import { tabsContextId } from './tabs-context-id' ;
13
+ import { KeyCode } from '../../utils/key-code.type' ;
14
14
15
15
export const TAB_ID_PREFIX = '_tab_' ;
16
16
@@ -21,17 +21,6 @@ export type TabProps = QwikIntrinsicElements['button'] & {
21
21
tabId ?: string ;
22
22
} ;
23
23
24
- const preventedKeys = [
25
- KeyCode . Home ,
26
- KeyCode . End ,
27
- KeyCode . PageDown ,
28
- KeyCode . PageUp ,
29
- KeyCode . ArrowDown ,
30
- KeyCode . ArrowUp ,
31
- KeyCode . ArrowLeft ,
32
- KeyCode . ArrowRight ,
33
- ] ;
34
-
35
24
export const Tab = component$ < TabProps > ( ( { selectedClassName, tabId, ...props } ) => {
36
25
const contextService = useContext ( tabsContextId ) ;
37
26
@@ -48,20 +37,6 @@ export const Tab = component$<TabProps>(({ selectedClassName, tabId, ...props })
48
37
return contextService . selectedTabIdSig . value === tabId ;
49
38
} ) ;
50
39
51
- useVisibleTask$ ( function preventDefaultOnKeysVisibleTask ( { cleanup } ) {
52
- function handler ( event : KeyboardEvent ) {
53
- if ( preventedKeys . includes ( event . key as KeyCode ) ) {
54
- event . preventDefault ( ) ;
55
- }
56
- contextService . onTabKeyDown$ ( event . key as KeyCode , tabId ! ) ;
57
- }
58
- // TODO put the listener on TabList
59
- elementRefSig . value ?. addEventListener ( 'keydown' , handler ) ;
60
- cleanup ( ( ) => {
61
- elementRefSig . value ?. removeEventListener ( 'keydown' , handler ) ;
62
- } ) ;
63
- } ) ;
64
-
65
40
const selectIfAutomatic$ = $ ( ( ) => {
66
41
contextService . selectIfAutomatic$ ( tabId ! ) ;
67
42
} ) ;
@@ -86,6 +61,27 @@ export const Tab = component$<TabProps>(({ selectedClassName, tabId, ...props })
86
61
tabIndex = { isSelectedSig . value ? 0 : - 1 }
87
62
class = { [ props . class , classNamesSig . value ] }
88
63
onClick$ = { [ $ ( ( ) => contextService . selectTab$ ( tabId ! ) ) , props . onClick$ ] }
64
+ onKeyDown$ = { [
65
+ /* KeyCode cannot be used here. */
66
+ sync$ ( ( e : KeyboardEvent ) : void => {
67
+ const keys = [
68
+ 'Home' ,
69
+ 'End' ,
70
+ 'PageDown' ,
71
+ 'PageUp' ,
72
+ 'ArrowDown' ,
73
+ 'ArrowUp' ,
74
+ 'ArrowLeft' ,
75
+ 'ArrowRight' ,
76
+ ] ;
77
+
78
+ if ( keys . includes ( e . key ) ) {
79
+ e . preventDefault ( ) ;
80
+ }
81
+ } ) ,
82
+ $ ( ( e ) => contextService . onTabKeyDown$ ( e . key as KeyCode , tabId ! ) ) ,
83
+ props . onKeyDown$ ,
84
+ ] }
89
85
>
90
86
< Slot />
91
87
</ button >
0 commit comments