Skip to content

Commit

Permalink
Bugfix: key up on index 0 never leaves suggestion list
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarssanchez committed Jan 24, 2025
1 parent 7b9d032 commit f0fec70
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions assets/js/autosuggest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ function updateAutosuggestBox(options, input) {
function hideAutosuggestBox() {
const lists = document.querySelectorAll('.autosuggest-list');
const containers = document.querySelectorAll('.ep-autosuggest');
const inputs = document.querySelectorAll('.ep-autosuggest-container [aria-activedescendant]');

// empty all EP results lists
lists.forEach((list) => {
Expand All @@ -406,12 +405,19 @@ function hideAutosuggestBox() {
container.style = 'display: none;';
});

// Remove active descendant attribute from all inputs
inputs.forEach((input) => setInputActiveDescendant('', input));
hideInputActiveDescendant();

return true;
}

/**
* Remove active descendant attribute from all inputs.
*/
const hideInputActiveDescendant = () => {
const inputs = document.querySelectorAll('.ep-autosuggest-container [aria-activedescendant]');
inputs.forEach((input) => setInputActiveDescendant('', input));
};

/**
* Checks for any manually ordered posts and puts them in the correct place
*
Expand Down Expand Up @@ -491,7 +497,7 @@ function init() {

// to be used by the handleUpDown function
// to keep track of the currently selected result
let currentIndex;
let currentIndex = -1;

// these are the keycodes we listen for in handleUpDown,
// and in handleKeyup
Expand Down Expand Up @@ -552,14 +558,14 @@ function init() {
// if enter, navigate to that element
switch (event.keyCode) {
case 38: // Up
// don't go less than the 0th index
currentIndex = currentIndex - 1 >= 0 ? currentIndex - 1 : 0;
currentIndex = currentIndex - 1 >= 0 ? currentIndex - 1 : -1;
hideInputActiveDescendant();
deSelectResults();
break;
case 40: // Down
if (typeof currentIndex === 'undefined') {
// index is not yet defined, so let's
// start with the first one
if (currentIndex === -1) {
// -1 means we are at the search input
// manually move the index to the first one when pressing down.
currentIndex = 0;
} else {
const current = getSelectedResultIndex();
Expand Down

0 comments on commit f0fec70

Please sign in to comment.