Skip to content

Commit be538ff

Browse files
committed
feat(Combobox): preserve modelValue type in emit
1 parent 970831c commit be538ff

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/components/Combobox.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
lang="ts"
44
generic="
55
T extends BaseOption = ExtendedOption,
6-
Options extends T[] | OptionGroup<string, T>[] = T[] | OptionGroup<string, T>[]
6+
Options extends T[] | OptionGroup<string, T>[] = T[] | OptionGroup<string, T>[],
7+
ModelValue extends string | string[] = string | string[]
78
">
89
import { ref, computed, useAttrs } from 'vue'
910
import { Combobox, ComboboxButton, ComboboxInput } from '@headlessui/vue'
@@ -53,7 +54,7 @@ const props = withDefaults(
5354
* When a single option can be selected, modelValue must be a string.
5455
* When multiple options can be selected, modelValue must be a list of strings.
5556
*/
56-
modelValue: string | string[]
57+
modelValue: ModelValue
5758
/**
5859
* direction in which the dropdown is opening.
5960
*
@@ -111,7 +112,7 @@ const props = withDefaults(
111112
)
112113
113114
const emit = defineEmits<{
114-
'update:modelValue': [value: string | string[]]
115+
'update:modelValue': [value: ModelValue]
115116
'update:query': [query: string]
116117
}>()
117118
@@ -146,13 +147,13 @@ const handleGroupToggle = (options: T[]) => {
146147
? props.modelValue.filter(v => !values.includes(v))
147148
: [...new Set([...props.modelValue, ...values])]
148149
149-
emit('update:modelValue', newSelection)
150+
emit('update:modelValue', newSelection as ModelValue)
150151
clearQuery()
151152
}
152153
153154
const model = computed({
154-
get: () => props.modelValue,
155-
set: value => {
155+
get: (): ModelValue => props.modelValue,
156+
set: (value: ModelValue) => {
156157
// handle keyboard selection of group headers (which have __group__ prefix)
157158
if (isMultiSelect(value) && areOptionsGrouped(props.options)) {
158159
const groupValue = value.find(v => v.startsWith(GROUP_VALUE_PREFIX))
@@ -222,7 +223,7 @@ const inputSize = computed(() => {
222223
223224
const updateQuery = (value: string) => {
224225
if (model.value && !value && !isMultiSelect(model.value)) {
225-
model.value = ''
226+
model.value = '' as ModelValue
226227
}
227228
if (query.value !== value) {
228229
query.value = value || ''
@@ -238,7 +239,7 @@ const clearQuery = () => {
238239
const removeValue = (value: string, event: Event) => {
239240
event.stopPropagation()
240241
if (isMultiSelect(model.value) && model.value.includes(value)) {
241-
model.value = model.value.filter(item => item !== value)
242+
model.value = model.value.filter(item => item !== value) as ModelValue
242243
}
243244
}
244245
@@ -273,7 +274,7 @@ const showSelectableGroups = computed(
273274
274275
const handleDelete = () => {
275276
if (isMultiSelect(model.value) && !query.value) {
276-
model.value = model.value.slice(0, -1)
277+
model.value = model.value.slice(0, -1) as ModelValue
277278
}
278279
}
279280

0 commit comments

Comments
 (0)