fix autocomplete click-away
This commit is contained in:
parent
93ac03acd4
commit
8489b7e2be
1 changed files with 13 additions and 1 deletions
|
|
@ -90,7 +90,9 @@ class AutoComplete {
|
||||||
this.elem.addEventListener('keyup', (e) => this.keyUp(e));
|
this.elem.addEventListener('keyup', (e) => this.keyUp(e));
|
||||||
this.elem.closest('form')?.addEventListener('submit', (e) => this.directFormSubmit(e));
|
this.elem.closest('form')?.addEventListener('submit', (e) => this.directFormSubmit(e));
|
||||||
this.elem.addEventListener('click', () => this.deselectAll());
|
this.elem.addEventListener('click', () => this.deselectAll());
|
||||||
this.elem.addEventListener('focusout', () => this.hideSuggestions());
|
|
||||||
|
// clicking outside the suggestion box requires a bit of work to determine if suggestions should be hidden
|
||||||
|
document.addEventListener('click', (e) => this.checkOutsideClick(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
mouseOver(e) {
|
mouseOver(e) {
|
||||||
|
|
@ -138,6 +140,9 @@ class AutoComplete {
|
||||||
|
|
||||||
// up/down direction
|
// up/down direction
|
||||||
switch (e.which) {
|
switch (e.which) {
|
||||||
|
case KEYS.ESC:
|
||||||
|
this.hideSuggestions();
|
||||||
|
return;
|
||||||
case KEYS.UP:
|
case KEYS.UP:
|
||||||
case KEYS.DOWN:
|
case KEYS.DOWN:
|
||||||
// move up or down the selection list
|
// move up or down the selection list
|
||||||
|
|
@ -302,6 +307,13 @@ class AutoComplete {
|
||||||
[...this.results.querySelectorAll('.suggestion.selected')].forEach((elem) => elem.classList.remove('selected'));
|
[...this.results.querySelectorAll('.suggestion.selected')].forEach((elem) => elem.classList.remove('selected'));
|
||||||
this.selectedItem = 0;
|
this.selectedItem = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if a click is detected on the page, generally we hide the suggestions, unless the click was within the autocomplete elements
|
||||||
|
checkOutsideClick(e) {
|
||||||
|
if (e.target.id === 'txtAddress') return;
|
||||||
|
if (e.target?.parentNode?.classList.contains(this.options.containerClass)) return;
|
||||||
|
this.hideSuggestions();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AutoComplete;
|
export default AutoComplete;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue