Skip to content

Commit

Permalink
Fix input cursor position bug on mobile
Browse files Browse the repository at this point in the history
Refactors how search suggestions are added to the view

Fixes #1133
  • Loading branch information
benbusby committed Apr 19, 2024
1 parent 37ff61d commit 9bfdd88
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions app/static/js/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ const handleUserInput = () => {
xhrRequest.send('q=' + searchInput.value);
};

const closeAllLists = el => {
// Close all autocomplete suggestions
let suggestions = document.getElementsByClassName("autocomplete-items");
for (let i = 0; i < suggestions.length; i++) {
if (el !== suggestions[i] && el !== searchInput) {
suggestions[i].parentNode.removeChild(suggestions[i]);
}
}
};

const removeActive = suggestion => {
// Remove "autocomplete-active" class from previously active suggestion
for (let i = 0; i < suggestion.length; i++) {
Expand Down Expand Up @@ -71,7 +61,7 @@ const addActive = (suggestion) => {

const autocompleteInput = (e) => {
// Handle navigation between autocomplete suggestions
let suggestion = document.getElementById(this.id + "-autocomplete-list");
let suggestion = document.getElementById("autocomplete-list");
if (suggestion) suggestion = suggestion.getElementsByTagName("div");
if (e.keyCode === 40) { // down
e.preventDefault();
Expand All @@ -92,29 +82,28 @@ const autocompleteInput = (e) => {
};

const updateAutocompleteList = () => {
let autocompleteList, autocompleteItem, i;
let autocompleteItem, i;
let val = originalSearch;
closeAllLists();

let autocompleteList = document.getElementById("autocomplete-list");
autocompleteList.innerHTML = "";

if (!val || !autocompleteResults) {
return false;
}

currentFocus = -1;
autocompleteList = document.createElement("div");
autocompleteList.setAttribute("id", this.id + "-autocomplete-list");
autocompleteList.setAttribute("class", "autocomplete-items");
searchInput.parentNode.appendChild(autocompleteList);

for (i = 0; i < autocompleteResults.length; i++) {
if (autocompleteResults[i].substr(0, val.length).toUpperCase() === val.toUpperCase()) {
autocompleteItem = document.createElement("div");
autocompleteItem.setAttribute("class", "autocomplete-item");
autocompleteItem.innerHTML = "<strong>" + autocompleteResults[i].substr(0, val.length) + "</strong>";
autocompleteItem.innerHTML += autocompleteResults[i].substr(val.length);
autocompleteItem.innerHTML += "<input type=\"hidden\" value=\"" + autocompleteResults[i] + "\">";
autocompleteItem.addEventListener("click", function () {
searchInput.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
autocompleteList.innerHTML = "";
document.getElementById("search-form").submit();
});
autocompleteList.appendChild(autocompleteItem);
Expand All @@ -123,10 +112,16 @@ const updateAutocompleteList = () => {
};

document.addEventListener("DOMContentLoaded", function() {
let autocompleteList = document.createElement("div");
autocompleteList.setAttribute("id", "autocomplete-list");
autocompleteList.setAttribute("class", "autocomplete-items");

searchInput = document.getElementById("search-bar");
searchInput.parentNode.appendChild(autocompleteList);

searchInput.addEventListener("keydown", (event) => autocompleteInput(event));

document.addEventListener("click", function (e) {
closeAllLists(e.target);
autocompleteList.innerHTML = "";
});
});
});

0 comments on commit 9bfdd88

Please sign in to comment.