From 68cbe4e5d4da372a3f8ae495cdf7db89d1c0ca3a Mon Sep 17 00:00:00 2001
From: "Henrik.Askjer" <henrik.askjer@uib.no>
Date: Mon, 23 Aug 2021 15:16:14 +0200
Subject: [PATCH] Keep auto-select-first

---
 src/components/Autocomplete.vue | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/components/Autocomplete.vue b/src/components/Autocomplete.vue
index f3cc318..e38e22f 100644
--- a/src/components/Autocomplete.vue
+++ b/src/components/Autocomplete.vue
@@ -12,6 +12,7 @@
         return-object
         rounded
         hide-no-data
+        auto-select-first
         no-filter
         hide-details
         label="Søk..."
@@ -37,7 +38,7 @@
 
   export default {
     props: {
-      endpoint: String
+      endpoint: String,
     },
     data: function() {
       return {
@@ -45,14 +46,15 @@
         items: [],
         search: null,
         select: null,
+        suggesting: null,
         debounced: debounce(function(q, self) {
           self.loading = true
           return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q))
                       .then(
                             function(response) {
-                                if (self.loading) {
+                                if (self.suggesting) {
                                   let hits = []
-                                  if (q == self.search)  {
+                                  if (q == self.$refs.autocomplete.searchInput)  {
                                     response.data.forEach((item, i) => {
                                       let match = encodeURIComponent(item.match)
 
@@ -69,16 +71,22 @@
                                     });
                                     hits.reverse()
                                     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 val2 = h2.label.length * 10 + (h2.label[0].toLowerCase() === h2.label[0] ? 0 : 1)
                                     return val1 - val2
+                                    
                                   })
-
-                                  self.items = hits
-                                  self.loading = false
+                                  let currentSearch = self.$refs.autocomplete.searchInput
+                                  if (q == currentSearch) {
+                                    self.items = hits
+                                  }
+                                  self.items.push({currentSearch: encodeURIComponent(currentSearch), label: currentSearch + ' '})
+                                  
+                                  }
                                 }
+                                self.loading = false
                               })
         }, 100)
       }
@@ -96,15 +104,18 @@
           if (typeof item === 'string') {
             item = {"q": encodeURIComponent(item)}
           }
-          this.loading = false
+          this.items = []
+          this.suggesting = false
+
           this.$emit('submit', item)
-          let self = this
-          setTimeout(() => self.$refs.autocomplete.$refs.input.select(), 1)
+          setTimeout(() => this.$refs.autocomplete.blur(), 1)
         }
       }
     },
     methods: {
       run_query(q) {
+        this.items = []
+        this.suggesting = true
         this.debounced(q, this)
       }
     },
-- 
GitLab