-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTypeaheadSelect.vue
More file actions
165 lines (164 loc) · 4.14 KB
/
TypeaheadSelect.vue
File metadata and controls
165 lines (164 loc) · 4.14 KB
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
<template>
<div :class="wrapperClass">
<div class="dropdown">
<input
v-model="selected"
:class="{ 'form-control': isStandalone }"
:disabled="disabled"
type="text"
:placeholder="placeholder"
@keydown.enter="enter"
@keydown.down="down"
@keydown.up="up"
@input="debounceSearch"
/>
<div
class="dropdown-menu overflow-auto"
:class="{ show: isOpen }"
style="width: 100%"
>
<a
v-for="(suggestion, idx) in matches"
:key="idx"
class="dropdown-item"
:class="{ active: isActive(idx) }"
@click="suggestionClick(idx)"
>
{{ suggestion[labelVariable] }}
<span v-if="suggestion.total_count">
({{ suggestion.total_count.toLocaleString() }}
<span v-if="totalLabel">{{ totalLabel }}</span>
)
</span>
</a>
</div>
</div>
</div>
</template>
<script>
// cribbed from https://medium.com/@fareez_ahamed/make-your-own-autocomplete-component-quickly-using-vue-js-21a642e8b140
import debounce from 'lodash/debounce';
import { worldLocation } from '@/js/get-location';
export default {
name: 'TypeaheadSelect',
props: {
queryFunction: Function,
apiUrl: String,
placeholder: String,
totalLabel: String,
selectedValue: [String, Object],
labelVariable: {
type: String,
default: 'name',
},
wrapperClass: {
type: String,
default: '',
},
removeOnSelect: {
type: Boolean,
default: true,
},
isStandalone: {
type: Boolean,
default: true,
},
disabled: {
type: Boolean,
default: false,
},
},
data() {
return {
isOpen: false,
current: 0,
selected: null,
matches: [],
};
},
watch: {
selectedValue() {
this.selected = this.selectedValue
? typeof this.selectedValue == 'string'
? this.selectedValue
: this.selectedValue[this.labelVariable]
: null;
},
},
created() {
this.debounceSearch = debounce(this.change, 250);
},
methods: {
enter() {
this.selected = this.matches[this.current];
this.isOpen = false;
this.$emit('selected', this.selected);
if (this.removeOnSelect || !this.selected) {
this.selected = null; // reset
} else {
this.selected = this.selected[this.labelVariable];
}
},
// When up pressed while suggestions are open
up() {
if (this.current > 0) this.current--;
},
// When up pressed while suggestions are open
down() {
if (this.current < this.matches.length - 1) this.current++;
},
// For highlighting element
isActive(index) {
return index === this.current;
},
//When the user changes input
change() {
if (this.selected.length > 0) {
if (this.selected.toLowerCase() === 'world') {
this.matches = [
{
...worldLocation,
label: 'World',
},
];
if (this.isOpen === false) {
this.isOpen = true;
this.current = 0;
}
} else {
this.querySubscription = this.queryFunction(
this.apiUrl,
this.selected,
).subscribe((results) => {
if (results.length > 10) {
this.matches = results.slice(0, 10);
} else {
this.matches = results;
}
if (this.isOpen === false) {
this.isOpen = true;
this.current = 0;
}
});
}
} else {
this.matches = [];
this.isOpen = false;
this.current = 0;
this.$emit('selected', null);
}
},
//When one of the suggestion is clicked
suggestionClick(index) {
this.selected = this.matches[index];
this.$emit('selected', this.selected);
this.isOpen = false;
if (this.removeOnSelect || !this.selected) {
this.selected = null; // reset
} else {
this.selected = this.selected[this.labelVariable];
}
},
},
};
</script>