Skip to content
Snippets Groups Projects
Commit 68cbe4e5 authored by Henrik Askjer's avatar Henrik Askjer
Browse files

Keep auto-select-first

parent a2eacf9e
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
return-object return-object
rounded rounded
hide-no-data hide-no-data
auto-select-first
no-filter no-filter
hide-details hide-details
label="Søk..." label="Søk..."
...@@ -37,7 +38,7 @@ ...@@ -37,7 +38,7 @@
export default { export default {
props: { props: {
endpoint: String endpoint: String,
}, },
data: function() { data: function() {
return { return {
...@@ -45,14 +46,15 @@ ...@@ -45,14 +46,15 @@
items: [], items: [],
search: null, search: null,
select: null, select: null,
suggesting: null,
debounced: debounce(function(q, self) { debounced: debounce(function(q, self) {
self.loading = true self.loading = true
return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q)) return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q))
.then( .then(
function(response) { function(response) {
if (self.loading) { if (self.suggesting) {
let hits = [] let hits = []
if (q == self.search) { if (q == self.$refs.autocomplete.searchInput) {
response.data.forEach((item, i) => { response.data.forEach((item, i) => {
let match = encodeURIComponent(item.match) let match = encodeURIComponent(item.match)
...@@ -69,16 +71,22 @@ ...@@ -69,16 +71,22 @@
}); });
hits.reverse() hits.reverse()
hits = hits.slice(0, 9) hits = hits.slice(0, 9)
}
hits.sort( (h1, h2) => { hits.sort( (h1, h2) => {
let val1 = h1.label.length * 10 + (h1.label[0].toLowerCase() === h1.label[0] ? 0 : 1) let val1 = h1.label.length * 10 + (h1.label[0].toLowerCase() === h1.label[0] ? 0 : 1)
let val2 = h2.label.length * 10 + (h2.label[0].toLowerCase() === h2.label[0] ? 0 : 1) let val2 = h2.label.length * 10 + (h2.label[0].toLowerCase() === h2.label[0] ? 0 : 1)
return val1 - val2 return val1 - val2
}) })
let currentSearch = self.$refs.autocomplete.searchInput
self.items = hits if (q == currentSearch) {
self.loading = false self.items = hits
}
self.items.push({currentSearch: encodeURIComponent(currentSearch), label: currentSearch + ' '})
}
} }
self.loading = false
}) })
}, 100) }, 100)
} }
...@@ -96,15 +104,18 @@ ...@@ -96,15 +104,18 @@
if (typeof item === 'string') { if (typeof item === 'string') {
item = {"q": encodeURIComponent(item)} item = {"q": encodeURIComponent(item)}
} }
this.loading = false this.items = []
this.suggesting = false
this.$emit('submit', item) this.$emit('submit', item)
let self = this setTimeout(() => this.$refs.autocomplete.blur(), 1)
setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1)
} }
} }
}, },
methods: { methods: {
run_query(q) { run_query(q) {
this.items = []
this.suggesting = true
this.debounced(q, this) this.debounced(q, this)
} }
}, },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment