@@ -8,10 +8,17 @@ import {
8
8
useId ,
9
9
useSignal ,
10
10
useTask$ ,
11
+ useComputed$ ,
11
12
} from '@builder.io/qwik' ;
13
+ import { JSX } from '@builder.io/qwik/jsx-runtime' ;
14
+ import ComboboxContextId from './combobox-context-id' ;
15
+ import { ComboboxContext } from './combobox-context.type' ;
16
+ import { getOptionLabel } from './utils' ;
12
17
13
18
import { type Option } from './combobox-context.type' ;
14
19
20
+ type QrlType < T > = T extends QRL < infer U > ? U : never ;
21
+
15
22
export type ComboboxProps <
16
23
O extends Option = Option ,
17
24
Complex extends { option : O ; key : number } = { option : O ; key : number } ,
@@ -43,11 +50,6 @@ export type OptionInfo = {
43
50
index : number ;
44
51
} ;
45
52
46
- import ComboboxContextId from './combobox-context-id' ;
47
- import { ComboboxContext } from './combobox-context.type' ;
48
- import { JSX } from '@builder.io/qwik/jsx-runtime' ;
49
- import { getOptionLabel } from './utils' ;
50
-
51
53
export const Combobox = component$ (
52
54
<
53
55
O extends Option = Option ,
@@ -69,22 +71,23 @@ export const Combobox = component$(
69
71
...rest
70
72
} = props ;
71
73
74
+ const complexSig = useComputed$ ( ( ) =>
75
+ options . map ( ( option , key ) => ( { option, key } as Complex ) ) ,
76
+ ) ;
72
77
const optionsSig = useSignal < Complex [ ] > ( [ ] ) ;
73
78
const inputValueSig = useSignal < string > ( defaultLabel ) ;
74
79
useTask$ ( async ( { track } ) => {
75
- // TODO track in separate signal to prevent copying on every key
76
- const opts = track ( ( ) =>
77
- options . map ( ( option , key ) => ( { option, key } as Complex ) ) ,
78
- ) ;
80
+ const opts = track ( ( ) => complexSig . value ) ;
79
81
const inputValue = track ( ( ) => inputValueSig . value ) ;
80
-
81
- let filterFunction = await track ( ( ) => filter$ ) ?. resolve ( ) ;
82
+ let filterFunction : QrlType < ComboboxProps < O , Complex > [ 'filter$' ] > | undefined =
83
+ await track ( ( ) => filter$ ) ?. resolve ( ) ;
82
84
83
85
if ( ! filterFunction ) {
84
86
const labelKey = track ( ( ) => optionLabelKey ) ;
85
87
86
88
filterFunction = ( value : string , options : Complex [ ] ) => {
87
89
if ( ! options ) return [ ] ;
90
+ if ( ! value ) return options ;
88
91
return options . filter ( ( { option } ) => {
89
92
if ( typeof option === 'string' )
90
93
return option . toLowerCase ( ) . includes ( value . toLowerCase ( ) ) ;
0 commit comments