@@ -32,44 +32,24 @@ export type InternalSelectProps = {
32
32
_itemsMap : TItemsMap ;
33
33
} ;
34
34
35
- export type TMultiple < M > = M extends true ? string [ ] : string ;
36
-
37
- /**
38
- * Value sets an initial value for the select. If multiple is true, value is disabled
39
- *
40
- */
41
- type TMultiValue =
42
- | { multiple : true ; value ?: never }
43
- | { multiple ?: false ; value ?: string } ;
44
-
45
- type TStringOrArray =
46
- | {
47
- multiple ?: true ;
48
- onChange$ ?: QRL < ( value : string [ ] ) => void > ;
49
- }
50
- | {
51
- multiple ?: false ;
52
- onChange$ ?: QRL < ( value : string ) => void > ;
53
- } ;
54
-
55
- export type SelectProps < M extends boolean = boolean > = Omit <
56
- PropsOf < 'div' > ,
57
- 'onChange$'
58
- > & {
35
+ export type SelectProps <
36
+ M extends true ,
37
+ T = boolean extends M ? string : M extends true ? string [ ] : string ,
38
+ > = Omit < PropsOf < 'div' > , 'onChange$' > & {
59
39
/** A signal that controls the current selected value (controlled). */
60
- 'bind:value' ?: Signal < TMultiple < M > > ;
40
+ 'bind:value' ?: T extends string ? Signal < T > : never ;
61
41
62
42
/** A signal that controls the current open state (controlled). */
63
43
'bind:open' ?: Signal < boolean > ;
64
44
65
45
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66
- 'bind:displayValue' ?: Signal < TMultiple < M > > ;
46
+ 'bind:displayValue' ?: T extends string ? Signal < T > : never ;
67
47
68
48
/**
69
49
* QRL handler that runs when a select value changes.
70
50
* @param value The new value as a string.
71
51
*/
72
- onChange$ ?: QRL < ( value : TMultiple < M > ) => void > ;
52
+ onChange$ ?: QRL < ( value : T ) => void > ;
73
53
/**
74
54
* QRL handler that runs when the listbox opens or closes.
75
55
* @param open The new state of the listbox.
@@ -107,13 +87,18 @@ export type SelectProps<M extends boolean = boolean> = Omit<
107
87
*/
108
88
multiple ?: M ;
109
89
90
+ /**
91
+ * Value sets an initial value for the select. If multiple is true, value is disabled
92
+ *
93
+ */
94
+ value ?: M extends false ? string : never ;
95
+
110
96
invalid ?: boolean ;
111
- } & TMultiValue &
112
- TStringOrArray ;
97
+ } ;
113
98
114
99
/* root component in select-inline.tsx */
115
- export const HSelectImpl = component$ < SelectProps < boolean > & InternalSelectProps > (
116
- ( props : SelectProps < boolean > & InternalSelectProps ) => {
100
+ export const HSelectImpl = component$ (
101
+ < M extends true , T > ( props : SelectProps < M , T > & InternalSelectProps ) => {
117
102
const {
118
103
_itemsMap,
119
104
_valuePropIndex : givenValuePropIndex ,
@@ -259,7 +244,7 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
259
244
}
260
245
261
246
if ( onChange$ && selectedIndexSetSig . value . size > 0 ) {
262
- await onChange$ ( context . multiple ? values : values [ 0 ] ) ;
247
+ await onChange$ ( context . multiple ? ( values as T ) : ( values [ 0 ] as T ) ) ;
263
248
}
264
249
265
250
// sync the user's given signal when an option is selected
@@ -269,9 +254,9 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
269
254
270
255
if ( currUserSigValues !== newUserSigValues ) {
271
256
if ( context . multiple ) {
272
- bindValueSig . value = values ;
257
+ bindValueSig . value = values as T ;
273
258
} else {
274
- bindValueSig . value = values [ 0 ] ;
259
+ bindValueSig . value = values [ 0 ] as T ;
275
260
}
276
261
}
277
262
}
@@ -281,8 +266,8 @@ export const HSelectImpl = component$<SelectProps<boolean> & InternalSelectProps
281
266
// sync the user's given signal for the display value
282
267
if ( bindDisplayTextSig && context . currDisplayValueSig . value ) {
283
268
bindDisplayTextSig . value = context . multiple
284
- ? context . currDisplayValueSig . value
285
- : context . currDisplayValueSig . value [ 0 ] ;
269
+ ? ( context . currDisplayValueSig . value as T )
270
+ : ( context . currDisplayValueSig . value [ 0 ] as T ) ;
286
271
}
287
272
} ) ;
288
273
0 commit comments