Line data Source code
1 16 : <template>
2 0 : <div class="relative flex min-h-6 items-center gap-2.5">
3 : <span class="text-left font-sans text-xs text-muted-foreground">{{
4 : label
5 : }}</span>
6 : <select
7 : class="absolute right-0 h-6 rounded-md border bg-secondary px-1.5 transition-colors duration-100 focus:outline focus:outline-ring"
8 : :value="modelValue"
9 : @change="onChange"
10 : >
11 0 : <option
12 : v-for="option in normalizedOptions"
13 : :key="option.value"
14 : :value="option.value"
15 : >
16 : {{ option.label }}
17 : </option>
18 : </select>
19 : </div>
20 : </template>
21 :
22 : <script setup lang="ts">
23 : import { computed } from 'vue'
24 :
25 : interface DropdownOption {
26 : label: string
27 : value: string | number
28 : }
29 :
30 : interface Props {
31 : label: string
32 : options: string[] | DropdownOption[]
33 : modelValue: string | number
34 : }
35 :
36 0 : const props = defineProps<Props>()
37 :
38 0 : const emit = defineEmits<{
39 : 'update:modelValue': [value: string | number]
40 : }>()
41 :
42 0 : const normalizedOptions = computed((): DropdownOption[] => {
43 0 : return props.options.map((option) => {
44 0 : if (typeof option === 'string') {
45 0 : return { label: option, value: option }
46 : }
47 0 : return option
48 : })
49 : })
50 :
51 0 : const onChange = (event: Event) => {
52 0 : const value = (event.target as HTMLSelectElement).value
53 0 : emit('update:modelValue', value)
54 : }
55 : </script>
|