1
1
import { component$ , type PropsOf , useContext , sync$ , $ , Slot } from '@builder.io/qwik' ;
2
2
import SelectContextId from './select-context' ;
3
3
import { getNextEnabledOptionIndex , getPrevEnabledOptionIndex } from './utils' ;
4
-
5
- export type OptionsType = {
6
- element : HTMLLIElement ;
7
- isDisabled : boolean ;
8
- } [ ] ;
9
4
export type OpenKeys = 'ArrowUp' | 'Enter' | 'Space' | 'ArrowDown' ;
10
5
11
6
type SelectTriggerProps = PropsOf < 'button' > ;
12
- export type DisabledArr = Array < { disabled : boolean } > ;
13
7
export const SelectTrigger = component$ < SelectTriggerProps > ( ( props ) => {
14
8
const context = useContext ( SelectContextId ) ;
15
9
const openKeys = [ 'ArrowUp' , 'ArrowDown' ] ;
@@ -30,17 +24,7 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
30
24
const handleKeyDown$ = $ ( ( e : KeyboardEvent ) => {
31
25
const shouldOpen = ! context . isListboxOpenSig . value && openKeys . includes ( e . key ) ;
32
26
const shouldClose = context . isListboxOpenSig . value && closedKeys . includes ( e . key ) ;
33
-
34
- // TODO: refactor with data grabbed from inline component
35
- const options : OptionsType = context . optionRefsArray . value . map ( ( option ) => {
36
- if ( option . value === undefined ) {
37
- throw new Error ( 'Qwik UI: internal select option is undefined' ) ;
38
- }
39
-
40
- const isDisabled = option . value . ariaDisabled === 'true' ;
41
-
42
- return { element : option . value , isDisabled } ;
43
- } ) ;
27
+ if ( ! context . options ) return ;
44
28
45
29
if ( shouldOpen ) {
46
30
context . isListboxOpenSig . value = true ;
@@ -51,32 +35,35 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
51
35
}
52
36
53
37
if ( e . key === 'Home' ) {
54
- context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , options ) ;
38
+ context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , context . options ) ;
55
39
}
56
40
57
41
if ( e . key === 'End' ) {
58
- const lastEnabledOptionIndex = getPrevEnabledOptionIndex ( options . length , options ) ;
42
+ const lastEnabledOptionIndex = getPrevEnabledOptionIndex (
43
+ context . options . length ,
44
+ context . options ,
45
+ ) ;
59
46
context . highlightedIndexSig . value = lastEnabledOptionIndex ;
60
47
}
61
48
62
49
/** When initially opening the listbox, we want to grab the first enabled option index */
63
50
if ( context . highlightedIndexSig . value === null ) {
64
- context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , options ) ;
51
+ context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , context . options ) ;
65
52
return ;
66
53
}
67
54
68
55
if ( context . isListboxOpenSig . value && ! shouldOpen ) {
69
56
if ( e . key === 'ArrowDown' ) {
70
57
context . highlightedIndexSig . value = getNextEnabledOptionIndex (
71
58
context . highlightedIndexSig . value ,
72
- options ,
59
+ context . options ,
73
60
) ;
74
61
}
75
62
76
63
if ( e . key === 'ArrowUp' ) {
77
64
context . highlightedIndexSig . value = getPrevEnabledOptionIndex (
78
65
context . highlightedIndexSig . value ,
79
- options ,
66
+ context . options ,
80
67
) ;
81
68
}
82
69
0 commit comments