-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
116 lines (103 loc) · 2.73 KB
/
App.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
<template>
<div class="min-h-screen container p-10 mx-auto">
<ComboBox
ref="combobox"
v-model="query"
:suggestions="suggestions"
:open="showSuggestions"
@selected="selectSuggestion"
>
<form action="#">
<ComboBoxLabel>
<span class="sr-only">Type to search products...</span>
<ComboBoxTextbox
class="w-full border p-4"
placeholder="Type to search products..."
@focus.native="showSuggestions = true"
/>
</ComboBoxLabel>
</form>
<ComboBoxSuggestions
ref="suggestions"
class="w-full py-2 mt-2 bg-white rounded-xl border shadow-lg"
>
<ComboBoxSuggestion
slot-scope="{ suggestion, index, isFocused }"
class="w-full px-5 py-3 cursor-pointer"
:class="{
'bg-gray-200': isFocused,
'hover:bg-gray-100': !isFocused,
}"
v-text="suggestion"
:suggestion="suggestion"
:index="index"
/>
</ComboBoxSuggestions>
</ComboBox>
</div>
</template>
<script>
import {
ComboBox,
ComboBoxLabel,
ComboBoxTextbox,
ComboBoxSuggestions,
ComboBoxSuggestion,
} from "../src";
export default {
components: {
ComboBox,
ComboBoxLabel,
ComboBoxTextbox,
ComboBoxSuggestions,
ComboBoxSuggestion,
},
data() {
return {
query: "",
showSuggestions: false,
suggestions: [],
skipSearch: false,
};
},
watch: {
query(newVal) {
if (this.skipSearch) {
return;
}
if (!newVal || !newVal.trim()) {
this.suggestions = [];
} else {
// This part can also be async if you have an API for searching.
const suggestions = ["Apple", "Banana", "Grapes", "Orange"];
this.suggestions = suggestions.filter((suggestion) => {
return suggestion.toLowerCase().startsWith(newVal.toLowerCase())
});
this.showSuggestions = true;
}
},
},
mounted() {
// Close ComboBox if user clicked outside the component.
// You have complete control whether you want combobox open or not.
document.body.addEventListener("click", (evt) => {
if (!this.$refs.combobox.$el.contains(evt.target)) {
this.showSuggestions = false
}
})
},
methods: {
selectSuggestion(suggestion) {
// Prevent searching again after the query is updated.
// In this demo, if you type "a" and selected "Apple"
this.skipSearch = true;
this.query = suggestion;
this.showSuggestions = false;
// Wait for the new input to be rendered before allowing searches.
this.$nextTick(() => {
this.skipSearch = false
})
},
},
};
</script>