-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCombobox.vue
187 lines (171 loc) · 4.06 KB
/
Combobox.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<template>
<div ref="dropdownRef" class="relative">
<div class="flex items-center w-25 px-4 py-2 border rounded cursor-pointer" @click="toggleDropdown">
<span class="flex-grow">{{ selectedItem || placeholder }}</span>
<span class="ml-2">▾</span> <!-- Arrow symbol -->
</div>
<div
v-if="isOpen"
class="absolute w-25 mt-1 bg-white border rounded shadow-lg"
>
<input
type="text"
v-model="searchTerm"
@input="onSearch"
:placeholder="searchPlaceholder"
class="w-full px-4 py-2 border-b border-gray-300 rounded-t"
/>
<ul>
<li
v-for="(item, index) in filteredItems"
:key="index"
@mousedown.prevent="selectItem(item)"
class="px-4 py-2 cursor-pointer hover:bg-gray-100"
>
{{ item }}
</li>
<li v-if="filteredItems.length === 0" class="px-4 py-2 text-gray-500">
No results found
</li>
</ul>
</div>
<input
type="text"
v-model="inputValue"
@input="onInput"
@focus="isOpen = true"
@blur="onBlur"
:placeholder="placeholder"
class="hidden"
/>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
interface Props {
items: string,
placeholder?: string,
searchPlaceholder?: string,
modelValue?: string
}
const props = withDefaults(defineProps<Props>(), {
placeholder: 'Select an item',
searchPlaceholder: 'Search...',
modelValue: ''
})
const emits = defineEmits(['update:modelValue'])
const isOpen = ref(false)
const inputValue = ref(props.modelValue || '')
const selectedItem = ref<string | null>(props.modelValue || null)
const dropdownRef = ref<HTMLElement | null>(null)
const searchTerm = ref('')
const toggleDropdown = () => {
isOpen.value = !isOpen.value
if (isOpen.value) {
searchTerm.value = '' // Reset search term to show all items
}
}
const onInput = (event: Event) => {
const target = event.target as HTMLInputElement
inputValue.value = target.value
emits('update:modelValue', inputValue.value)
}
const selectItem = (item: string) => {
inputValue.value = item
selectedItem.value = item
emits('update:modelValue', item)
isOpen.value = false
}
const onBlur = () => {
setTimeout(() => {
isOpen.value = false
}, 100)
}
// Handle clicks outside the component to close the dropdown
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.value && !dropdownRef.value.contains(event.target as Node)) {
isOpen.value = false
}
}
onMounted(() => {
document.addEventListener('click', handleClickOutside)
})
onUnmounted(() => {
document.removeEventListener('click', handleClickOutside)
})
// Watch for changes in `modelValue` and update `selectedItem`
watch(() => props.modelValue, (newVal) => {
selectedItem.value = newVal
})
const filteredItems = computed(() => {
const itemsArray = props.items.split(',').map(item => item.trim()).filter(item => item)
if (searchTerm.value) {
return itemsArray.filter(item => item.toLowerCase().includes(searchTerm.value.toLowerCase()))
}
return itemsArray
})
const onSearch = () => {
// This function can be used for additional actions on search input if needed
}
</script>
<style lang="scss" scoped>
.relative {
position: relative;
}
.flex {
display: flex;
}
.items-center {
align-items: center;
}
.w-25 {
width: 25%;
}
.px-4 {
padding-left: 1rem;
padding-right: 1rem;
}
.py-2 {
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}
.border {
border-width: 1px;
}
.rounded {
border-radius: 0.25rem;
}
.mt-1 {
margin-top: 0.25rem;
}
.bg-white {
background-color: white;
}
.shadow-lg {
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
.absolute {
position: absolute;
}
.cursor-pointer {
cursor: pointer;
}
.hover\:bg-gray-100:hover {
background-color: #f7fafc;
}
.text-gray-500 {
color: #6b7280;
}
.ml-2 {
margin-left: 0.5rem;
}
.hidden {
display: none;
}
.w-full {
width: 100%;
}
.border-b {
border-bottom-width: 1px;
}
</style>