Skip to content

Commit cdef7ae

Browse files
committed
fix(CFormSelect): component does not respect initially selected value
1 parent 9207da5 commit cdef7ae

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/coreui-vue/src/components/form/CFormSelect.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineComponent, h, PropType } from 'vue'
33
type Option = {
44
disabled?: boolean
55
label?: string
6+
selected?: boolean
67
value?: string
78
}
89

@@ -28,10 +29,14 @@ const CFormSelect = defineComponent({
2829
* The default name for a value passed using v-model.
2930
*/
3031
modelValue: {
31-
type: String,
32+
type: [String, Array] as PropType<string | string[]>,
3233
default: undefined,
3334
require: false,
3435
},
36+
multiple: {
37+
type: Boolean,
38+
required: false,
39+
},
3540
/**
3641
* Options list of the select component. Available keys: `label`, `value`, `disabled`.
3742
* Examples:
@@ -80,9 +85,8 @@ const CFormSelect = defineComponent({
8085
const selected = Array.from(target.options)
8186
.filter((option) => option.selected)
8287
.map((option) => option.value)
83-
const value = target.multiple ? selected : selected[0]
8488
emit('change', event)
85-
emit('update:modelValue', value)
89+
emit('update:modelValue', target.multiple ? selected : selected[0])
8690
}
8791

8892
return () =>
@@ -97,17 +101,26 @@ const CFormSelect = defineComponent({
97101
'is-valid': props.valid,
98102
},
99103
],
104+
multiple: props.multiple,
100105
onChange: (event: InputEvent) => handleChange(event),
101106
size: props.htmlSize,
107+
...(props.modelValue && !props.multiple && { value: props.modelValue }),
102108
},
103109
props.options
104110
? props.options.map((option: Option | string) => {
105111
return h(
106112
'option',
107113
{
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+
}),
111124
},
112125
typeof option === 'string' ? option : option.label,
113126
)

0 commit comments

Comments
 (0)