11import { component$ , type PropsOf , useContext , sync$ , $ , Slot } from '@builder.io/qwik' ;
22import SelectContextId from './select-context' ;
33import { getNextEnabledOptionIndex , getPrevEnabledOptionIndex } from './utils' ;
4-
5- export type OptionsType = {
6- element : HTMLLIElement ;
7- isDisabled : boolean ;
8- } [ ] ;
94export type OpenKeys = 'ArrowUp' | 'Enter' | 'Space' | 'ArrowDown' ;
105
116type SelectTriggerProps = PropsOf < 'button' > ;
12- export type DisabledArr = Array < { disabled : boolean } > ;
137export const SelectTrigger = component$ < SelectTriggerProps > ( ( props ) => {
148 const context = useContext ( SelectContextId ) ;
159 const openKeys = [ 'ArrowUp' , 'ArrowDown' ] ;
@@ -30,17 +24,7 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
3024 const handleKeyDown$ = $ ( ( e : KeyboardEvent ) => {
3125 const shouldOpen = ! context . isListboxOpenSig . value && openKeys . includes ( e . key ) ;
3226 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 ;
4428
4529 if ( shouldOpen ) {
4630 context . isListboxOpenSig . value = true ;
@@ -51,32 +35,35 @@ export const SelectTrigger = component$<SelectTriggerProps>((props) => {
5135 }
5236
5337 if ( e . key === 'Home' ) {
54- context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , options ) ;
38+ context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , context . options ) ;
5539 }
5640
5741 if ( e . key === 'End' ) {
58- const lastEnabledOptionIndex = getPrevEnabledOptionIndex ( options . length , options ) ;
42+ const lastEnabledOptionIndex = getPrevEnabledOptionIndex (
43+ context . options . length ,
44+ context . options ,
45+ ) ;
5946 context . highlightedIndexSig . value = lastEnabledOptionIndex ;
6047 }
6148
6249 /** When initially opening the listbox, we want to grab the first enabled option index */
6350 if ( context . highlightedIndexSig . value === null ) {
64- context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , options ) ;
51+ context . highlightedIndexSig . value = getNextEnabledOptionIndex ( - 1 , context . options ) ;
6552 return ;
6653 }
6754
6855 if ( context . isListboxOpenSig . value && ! shouldOpen ) {
6956 if ( e . key === 'ArrowDown' ) {
7057 context . highlightedIndexSig . value = getNextEnabledOptionIndex (
7158 context . highlightedIndexSig . value ,
72- options ,
59+ context . options ,
7360 ) ;
7461 }
7562
7663 if ( e . key === 'ArrowUp' ) {
7764 context . highlightedIndexSig . value = getPrevEnabledOptionIndex (
7865 context . highlightedIndexSig . value ,
79- options ,
66+ context . options ,
8067 ) ;
8168 }
8269
0 commit comments