@@ -3,6 +3,7 @@ import { defineComponent, h, PropType } from 'vue'
3
3
type Option = {
4
4
disabled ?: boolean
5
5
label ?: string
6
+ selected ?: boolean
6
7
value ?: string
7
8
}
8
9
@@ -28,10 +29,14 @@ const CFormSelect = defineComponent({
28
29
* The default name for a value passed using v-model.
29
30
*/
30
31
modelValue : {
31
- type : String ,
32
+ type : [ String , Array ] as PropType < string | string [ ] > ,
32
33
default : undefined ,
33
34
require : false ,
34
35
} ,
36
+ multiple : {
37
+ type : Boolean ,
38
+ required : false ,
39
+ } ,
35
40
/**
36
41
* Options list of the select component. Available keys: `label`, `value`, `disabled`.
37
42
* Examples:
@@ -80,9 +85,8 @@ const CFormSelect = defineComponent({
80
85
const selected = Array . from ( target . options )
81
86
. filter ( ( option ) => option . selected )
82
87
. map ( ( option ) => option . value )
83
- const value = target . multiple ? selected : selected [ 0 ]
84
88
emit ( 'change' , event )
85
- emit ( 'update:modelValue' , value )
89
+ emit ( 'update:modelValue' , target . multiple ? selected : selected [ 0 ] )
86
90
}
87
91
88
92
return ( ) =>
@@ -97,17 +101,26 @@ const CFormSelect = defineComponent({
97
101
'is-valid' : props . valid ,
98
102
} ,
99
103
] ,
104
+ multiple : props . multiple ,
100
105
onChange : ( event : InputEvent ) => handleChange ( event ) ,
101
106
size : props . htmlSize ,
107
+ ...( props . modelValue && ! props . multiple && { value : props . modelValue } ) ,
102
108
} ,
103
109
props . options
104
110
? props . options . map ( ( option : Option | string ) => {
105
111
return h (
106
112
'option' ,
107
113
{
108
- ...( typeof option === 'object' &&
109
- option . disabled && { disabled : option . disabled } ) ,
110
- ...( typeof option === 'object' && option . value && { value : option . value } ) ,
114
+ ...( typeof option === 'object' && {
115
+ ...( option . disabled && { disabled : option . disabled } ) ,
116
+ ...( option . selected && { selected : option . selected } ) ,
117
+ ...( option . value && {
118
+ value : option . value ,
119
+ ...( props . modelValue &&
120
+ props . multiple &&
121
+ props . modelValue . includes ( option . value ) && { selected : true } ) ,
122
+ } ) ,
123
+ } ) ,
111
124
} ,
112
125
typeof option === 'string' ? option : option . label ,
113
126
)
0 commit comments