-
Notifications
You must be signed in to change notification settings - Fork 5
/
DestinationField.vue
355 lines (330 loc) · 9.74 KB
/
DestinationField.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<docs>
DestinationField component, based on https://github.com/yuche/vue-strap/blob/master/src/Typeahead.vue
Asks a server for results based on the query string entered by the user and displays
a dropdown with the result labels for the user to choose from. Every result has a
value and a label. Apart from suggestions the user can enter custom data, then the
component sets both value and label to this string.
You can use this component with `v-model` to get/set its value.
</docs>
<template>
<div :class="{
'typeahead': true,
'open': showDropdown
}">
<input type="text" class="field-input typeahead-input"
ref="input"
:placeholder="placeholder"
:maxlength="maxlength"
autocomplete="off"
v-model="val"
@input="update"
@focus="focus"
@keydown.up.prevent="up"
@keydown.down.prevent="down"
@keyup.enter.stop.prevent= "hit"
@keydown.esc="esc"
@blur="showDropdown = false"
/>
<ul v-if="showDropdown" ref="dropdown" class="dropdown-menu">
<li v-for="(item, index) in items" :class="{'active': isActive(index)}">
<a class="dropdown-item" @mousedown.stop.prevent="hit" @mousemove="setActive(index)">
<component :is="templateComp" :item="item" :value="val"></component>
</a>
</li>
</ul>
</div>
</template>
<script>
import { fixedEncodeURIComponent, escapeRegExp } from '../utils'
const _DELAY_ = 200
export default {
props: {
value: { /** The component’s value. */
type: Object,
default () {
// When the user types a custom value into the field, value and label are
// identical. They only differ if a suggestion has been selected.
return {
value: '',
label: ''
}
}
},
count: { /** Max number of items that should be displayed in the dropdown. */
type: Number,
default: 8
},
template: String, /** Used to render a suggestion. */
dataKey: { /** The key of the suggestions array in the response JSON. If not set, the response itself is expected to by an array of suggestions. */
type: String,
default: null
},
labelKey: { /** The key indicating the label in a suggestion object. */
type: String,
default: 'label'
},
valueKey: { /** The key indicating the value in a suggestion object. */
type: String,
default: 'value'
},
placeholder: String, /** The input’s placeholder text. */
maxlength: {
type: Number,
default: -1
},
delay: { /** Request data from the server after the user stopped typing for this amount of time (milliseconds). */
type: Number,
default: _DELAY_
},
getData: { /** Function that GETs data from the server. Should return a promise. */
type: Function,
required: true
},
url: { /** The URL to query. */
type: String,
required: true
},
headers: { /** HTTP headers to send with the request. */
type: Object,
default () {
return {}
}
},
queryParam: { /** Query parameter for the search term. */
type: String,
default: 's'
},
showDropdownOnFocus: { /** Display the dropdown immediately after the user focused the input. */
type: Boolean,
default: false
}
},
data () {
return {
val: this.value.label, /** {string} Internal value variable bound to the input element. */
showDropdown: false, /** {boolean} The dropdown’s visibility. */
current: 0, /** {integer} The index of the currently highlighted item (suggestion). */
items: [] /** {Object[]} Collection of suggestions, each having a value and a label property. */
}
},
computed: {
/**
* A vue component that uses the `template` prop and offers a `highlight` method
* for the template to use.
* @return {Object} The templateComp component.
*/
templateComp () {
return {
template: typeof this.template === 'string' ? '<span v-html="this.template"></span>' : '<span v-html="highlight(item.' + this.labelKey + ', value)"></span>',
props: {
item: { default: null },
value: String
},
methods: {
highlight (string, phrase) {
return (string && phrase && string.replace(new RegExp('(' + escapeRegExp(phrase) + ')', 'gi'), '<strong>$1</strong>')) || string
}
}
}
},
/**
* Guess whether the user entered a url or a path.
* @return {boolean} `true` if the user probably entered a url or a path.
*/
urlMode () {
return !!this.val.match(/^(ww|ht|\/)/i)
}
},
watch: {
// Update internal data when changes are caused by the parent component:
value: {
handler (val) {
if (this.val !== val.label) {
this.val = val.label
}
},
deep: true
}
},
methods: {
/**
* Handle the input getting the focus.
*/
focus () {
if (!this.val && this.showDropdownOnFocus) {
// Show suggestions when field is blank.
this.update()
}
},
/**
* Update the list of suggestions.
*/
update () {
// If a suggestion has been selected, value and label are not identical.
// We clear the field if a suggestion had been selected, so the user gets
// feedback that they deselected the suggestion by typing something else.
if (this.value.label !== this.value.value) {
this.val = ''
}
// Inform the parent component about the changes.
this.$emit('input', {
value: this.val,
label: this.val
})
// Close the dropdown and clear the list of suggestions.
this.reset()
// No need to query anything if the user entered a url.
if (this.urlMode) {
return
}
const lastVal = this.val
setTimeout(() => {
// Only query if the value didn’t change during the delay period.
if (this.val === lastVal) {
this.query()
}
}, this.delay)
},
/**
* Request data from the server and process the response.
*/
query () {
this.getData({
url: this.url,
headers: this.headers,
queryParam: this.queryParam,
queryString: fixedEncodeURIComponent(this.val)
}).then(response => {
// get the search term from the url
const re = new RegExp('[&|?]' + this.queryParam + '=([^&]*)')
let searchVal
try {
searchVal = response.config.url.match(re)[1]
} catch (error) {
return
}
// Throw the response away if the typeahead value has changed in the meantime.
if (fixedEncodeURIComponent(this.val) !== searchVal) return
const data = response.data
this.items = (this.dataKey ? data[this.dataKey] : data).slice(0, this.count)
this.showDropdown = this.items.length > 0
})
},
/**
* Close the dropdown and clear the list of suggestions.
*/
reset () {
this.items = []
this.current = 0
this.showDropdown = false
},
/**
* Set `this.current` to the index of item that is being hovered or selected
* with the cursor keys.
* @param {integer} index - The active item’s index.
*/
setActive (index) {
this.current = index
},
/**
* Check whether the item with a given index is active.
* @param {integer} index - The index of the item to check.
* @return {boolean} Is this the item that is currently active?
*/
isActive (index) {
return this.current === index
},
/**
* Handle Enter keyups and mousedowns on a suggestion.
*/
hit () {
if (this.showDropdown) {
this.val = this.items[this.current][this.labelKey]
this.$emit('input', {
value: this.items[this.current][this.valueKey],
label: this.items[this.current][this.labelKey]
})
this.reset()
}
},
/**
* Handle keydowns of the 'up' arrow key.
* Move the active item up and scroll it into view, if necessary.
*/
up () {
if (!this.showDropdown) {
return
}
if (this.current > 0) {
this.current--
const d = this.$refs.dropdown
const i = d.children[this.current]
if (i.offsetTop < d.scrollTop) {
d.scrollTop -= i.clientHeight
}
}
},
/**
* Handle keydowns of the 'down' arrow key.
* Move the active item down and scroll it into view, if necessary.
*/
down () {
if (!this.showDropdown) {
return
}
if (this.current < this.items.length - 1) {
this.current++
const d = this.$refs.dropdown
const i = d.children[this.current]
if (i.offsetTop + i.clientHeight > d.scrollTop + d.clientHeight) {
d.scrollTop += i.clientHeight
}
}
},
/**
* Handle esc key keydown events.
* @param {Event} e - The original event.
*/
esc (e) {
if (this.showDropdown) {
e.stopPropagation()
this.showDropdown = false
}
}
}
}
</script>
<style lang="css">
.typeahead {
display: inline-block;
position: relative;
}
.typeahead .dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
min-width: 100%;
max-height: 12rem;
overflow-y: auto;
list-style: none;
margin: 0;
padding: 0;
z-index: 2000;
}
.typeahead.open .dropdown-menu {
display: block;
}
.typeahead .dropdown-menu > li {
width: 100%;
}
.typeahead .dropdown-menu > li.active {
color: #fff;
background-color: #aaa;
}
.typeahead .dropdown-menu > li > a {
display: inline-block;
width: 100%;
cursor: pointer;
}
</style>