Improvements for the autocomplete component.

This commit is contained in:
Fabio Manganiello 2023-05-14 15:07:54 +02:00
parent 8447f9a854
commit 2cba504e3b
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 26 additions and 10 deletions

View file

@ -2,19 +2,26 @@ function autocomplete(inp, arr, listener) {
/*the autocomplete function takes two arguments, /*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/ the text field element and an array of possible autocompleted values:*/
let currentFocus; let currentFocus;
/*execute a function when someone writes in the text field:*/ /*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function() { inp.addEventListener("input", function() {
let a, b, i, val = this.value; let a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/ /*close any already open lists of autocompleted values*/
closeAllLists(); closeAllLists();
if (!val) { return false;} if (!val) {
return false;
}
currentFocus = -1; currentFocus = -1;
/*create a DIV element that will contain the items (values):*/ /*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV"); a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list"); a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items"); a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/ /*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a); this.parentNode.appendChild(a);
/*for each item in the array...*/ /*for each item in the array...*/
for (i = 0; i < arr.length; i++) { for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/ /*check if the item starts with the same letters as the text field value:*/
@ -43,10 +50,13 @@ function autocomplete(inp, arr, listener) {
} }
}); });
inp.addEventListener("keydown", function(e) { inp.addEventListener("keyup", function(e) {
if (e.keyCode === 9) { if (["ArrowUp", "ArrowDown", "Tab", "Enter"].indexOf(e.key) >= 0) {
/*Reset the list if tab has been pressed*/ e.stopPropagation();
closeAllLists(); }
if (e.key === "Enter") {
this.blur();
} }
}); });
@ -54,19 +64,21 @@ function autocomplete(inp, arr, listener) {
inp.addEventListener("keydown", function(e) { inp.addEventListener("keydown", function(e) {
let x = document.getElementById(this.id + "autocomplete-list"); let x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div"); if (x) x = x.getElementsByTagName("div");
if (e.keyCode === 40) { if (e.key === 'ArrowDown' || (e.key === 'Tab' && !e.shiftKey)) {
/*If the arrow DOWN key is pressed, /*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/ increase the currentFocus variable:*/
currentFocus++; currentFocus++;
/*and and make the current item more visible:*/ /*and and make the current item more visible:*/
addActive(x); addActive(x);
} else if (e.keyCode === 38) { //up e.preventDefault();
} else if (e.key === 'ArrowUp' || (e.key === 'Tab' && e.shiftKey)) { //up
/*If the arrow UP key is pressed, /*If the arrow UP key is pressed,
decrease the currentFocus variable:*/ decrease the currentFocus variable:*/
currentFocus--; currentFocus--;
/*and and make the current item more visible:*/ /*and and make the current item more visible:*/
addActive(x); addActive(x);
} else if (e.keyCode === 13) { e.preventDefault();
} else if (e.key === 'Enter') {
/*If the ENTER key is pressed, prevent the form from being submitted,*/ /*If the ENTER key is pressed, prevent the form from being submitted,*/
if (currentFocus > -1 && x && x.length) { if (currentFocus > -1 && x && x.length) {
e.preventDefault(); e.preventDefault();
@ -77,6 +89,7 @@ function autocomplete(inp, arr, listener) {
} }
} }
}); });
function addActive(x) { function addActive(x) {
/*a function to classify an item as "active":*/ /*a function to classify an item as "active":*/
if (!x) return false; if (!x) return false;
@ -87,12 +100,14 @@ function autocomplete(inp, arr, listener) {
/*add class "autocomplete-active":*/ /*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active"); x[currentFocus].classList.add("autocomplete-active");
} }
function removeActive(x) { function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/ /*a function to remove the "active" class from all autocomplete items:*/
for (let i = 0; i < x.length; i++) { for (let i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active"); x[i].classList.remove("autocomplete-active");
} }
} }
function closeAllLists(elmnt) { function closeAllLists(elmnt) {
/*close all autocomplete lists in the document, /*close all autocomplete lists in the document,
except the one passed as an argument:*/ except the one passed as an argument:*/
@ -103,6 +118,7 @@ function autocomplete(inp, arr, listener) {
} }
} }
} }
/*execute a function when someone clicks in the document:*/ /*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) { document.addEventListener("click", function (e) {
closeAllLists(e.target); closeAllLists(e.target);

View file

@ -22,11 +22,11 @@
background-color: $background-color; background-color: $background-color;
&:hover { &:hover {
background-color: $hover-bg; background-color: $hover-bg-2;
} }
} }
} }
.autocomplete-active { .autocomplete-active {
background-color: $selected-bg !important; background-color: $hover-bg-2 !important;
} }