Skip to content

Commit db1e8bf

Browse files
committed
feat(Listbox,Switcher): preserve modelValue type in emit
1 parent be538ff commit db1e8bf

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/components/Listbox.vue

Lines changed: 6 additions & 5 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
78
">
89
import { ref, computed, useAttrs } from 'vue'
910
import { Listbox, ListboxButton } from '@headlessui/vue'
@@ -43,7 +44,7 @@ const props = withDefaults(
4344
/**
4445
* the prop modelValue is required to use [v-model](https://vuejs.org/guide/components/events.html#usage-with-v-model) with a component.
4546
*/
46-
modelValue: string
47+
modelValue: ModelValue
4748
/**
4849
* direction in which the dropdown is opening.
4950
* possible values: `up`, `down`. Default is `down`
@@ -75,7 +76,7 @@ const props = withDefaults(
7576
)
7677
7778
const emit = defineEmits<{
78-
'update:modelValue': [value: string]
79+
'update:modelValue': [value: ModelValue]
7980
}>()
8081
8182
const attrs = useAttrs()
@@ -98,8 +99,8 @@ const flatOptions = computed((): T[] =>
9899
)
99100
100101
const model = computed({
101-
get: () => props.modelValue,
102-
set: (value: string) => emit('update:modelValue', value)
102+
get: (): ModelValue => props.modelValue,
103+
set: (value: ModelValue) => emit('update:modelValue', value)
103104
})
104105
105106
const valueOption = computed(() => flatOptions.value.find(option => option.value === model.value))

src/components/Switcher.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
export type { SwitcherOption } from '../types/selection'
33
</script>
44

5-
<script setup lang="ts" generic="T extends SwitcherOption = SwitcherOption">
5+
<script
6+
setup
7+
lang="ts"
8+
generic="T extends SwitcherOption = SwitcherOption, ModelValue extends string = string">
69
import { computed } from 'vue'
710
import { RadioGroup, RadioGroupLabel, RadioGroupOption } from '@headlessui/vue'
811
import AIcon from './Icon.vue'
912
import { type SwitcherOption } from '../types/selection'
1013
1114
const props = withDefaults(
1215
defineProps<{
13-
modelValue: string
16+
modelValue: ModelValue
1417
options: T[]
1518
label?: string
1619
raised?: boolean
@@ -24,12 +27,12 @@ const props = withDefaults(
2427
)
2528
2629
const emit = defineEmits<{
27-
'update:modelValue': [value: string]
30+
'update:modelValue': [value: ModelValue]
2831
}>()
2932
3033
const model = computed({
31-
get: () => props.modelValue,
32-
set: value => emit('update:modelValue', value)
34+
get: (): ModelValue => props.modelValue,
35+
set: (value: ModelValue) => emit('update:modelValue', value)
3336
})
3437
</script>
3538
<template>

0 commit comments

Comments
 (0)