-import {escapeHtml} from "../services/util";
-import {onChildEvent} from "../services/dom";
-import {Component} from "./component";
-import {KeyboardNavigationHandler} from "../services/keyboard-navigation";
+import {escapeHtml} from '../services/util.ts';
+import {onChildEvent} from '../services/dom.ts';
+import {Component} from './component';
+import {KeyboardNavigationHandler} from '../services/keyboard-navigation.ts';
const ajaxCache = {};
* AutoSuggest
*/
export class AutoSuggest extends Component {
+
setup() {
this.parent = this.$el.parentElement;
this.container = this.$el;
setupListeners() {
const navHandler = new KeyboardNavigationHandler(
this.list,
- event => {
+ () => {
this.input.focus();
setTimeout(() => this.hideSuggestions(), 1);
},
event => {
event.preventDefault();
- this.selectSuggestion(event.target.textContent);
+ const selectionValue = event.target.textContent;
+ if (selectionValue) {
+ this.selectSuggestion(selectionValue);
+ }
},
);
navHandler.shareHandlingToEl(this.input);
const search = this.input.value.toLowerCase();
const suggestions = await this.loadSuggestions(search, nameFilter);
- const toShow = suggestions.filter(val => {
- return search === '' || val.toLowerCase().startsWith(search);
- }).slice(0, 10);
+ const toShow = suggestions.filter(val => search === '' || val.toLowerCase().startsWith(search)).slice(0, 10);
this.displaySuggestions(toShow);
}
*/
displaySuggestions(suggestions) {
if (suggestions.length === 0) {
- return this.hideSuggestions();
+ this.hideSuggestions();
+ return;
}
// This used to use <button>s but was changed to div elements since Safari would not focus on buttons
this.hideSuggestions();
}
}
-}
\ No newline at end of file
+
+}