From f148c8dacb6f01d6cc993250eb7dee931bd59813 Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Sun, 2 Aug 2020 09:20:21 +0200
Subject: [PATCH] Better autocomplete field

---
 static/js/autocomplete_model.js | 7 ++++++-
 static/js/base.js               | 4 ++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/static/js/autocomplete_model.js b/static/js/autocomplete_model.js
index aa1d220c..6e135ad1 100644
--- a/static/js/autocomplete_model.js
+++ b/static/js/autocomplete_model.js
@@ -18,7 +18,8 @@ $(document).ready(function () {
                 html += li(prefix + "_" + obj.id, obj[name_field]);
             });
 
-            $("#" + prefix + "_list").html(html);
+            let results_list = $("#" + prefix + "_list");
+            results_list.html(html);
 
             objects.results.forEach(function (obj) {
                 $("#" + prefix + "_" + obj.id).click(function() {
@@ -32,6 +33,10 @@ $(document).ready(function () {
                 if (input === obj[name_field])
                     $("#" + prefix + "_pk").val(obj.id);
             });
+
+            if (results_list.children().length === 1 && e.originalEvent.keyCode >= 32) {
+                results_list.children().first().trigger("click");
+            }
         });
     });
 });
\ No newline at end of file
diff --git a/static/js/base.js b/static/js/base.js
index fdad58d7..f485635b 100644
--- a/static/js/base.js
+++ b/static/js/base.js
@@ -86,8 +86,8 @@ function getMatchedNotes(pattern, fun) {
  * Generate a <li> entry with a given id and text
  */
 function li(id, text, extra_css) {
-    return "<li class=\"list-group-item py-1 px-2 d-flex justify-content-between align-items-center text-truncate " + extra_css + "\"" +
-        " id=\"" + id + "\">" + text + "</li>\n";
+    return "<li class=\"list-group-item py-1 px-2 d-flex justify-content-between align-items-center text-truncate "
+        + (extra_css ? extra_css : "") + "\"" + " id=\"" + id + "\">" + text + "</li>\n";
 }
 
 /**
-- 
GitLab