Use noSuggestions

This commit is contained in:
Henrik Giesel 2021-09-08 17:20:20 +02:00
parent ae225f9569
commit 8d8a559f73
2 changed files with 17 additions and 6 deletions

View file

@ -48,7 +48,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let activeInput: HTMLInputElement; let activeInput: HTMLInputElement;
let autocomplete: any; let autocomplete: any;
let suggestionsPromise: Promise<string[]> = Promise.resolve([]);
const noSuggestions = Promise.resolve([]);
let suggestionsPromise: Promise<string[]> = noSuggestions;
async function fetchSuggestions(input: string): Promise<string[]> { async function fetchSuggestions(input: string): Promise<string[]> {
const data = await postRequest( const data = await postRequest(
@ -63,10 +65,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function updateSuggestions(): void { function updateSuggestions(): void {
const activeTag = tags[active!]; const activeTag = tags[active!];
const activeName = activeTag.name;
suggestionsPromise = fetchSuggestions(activeTag.name).then( suggestionsPromise =
(names: string[]): string[] => names.map(replaceWithUnicodeSeparator) activeName.length === 0
); ? noSuggestions
: fetchSuggestions(activeTag.name).then((names: string[]): string[] =>
names.map(replaceWithUnicodeSeparator)
);
} }
function onAutocomplete(selected: string): void { function onAutocomplete(selected: string): void {

View file

@ -83,10 +83,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
async function chooseSelected() { async function chooseSelected() {
active = true; active = true;
dispatch("choose", { chosen: suggestionsItems[selected ?? -1] }); dispatch("choose", { chosen: suggestionsItems[selected ?? -1] });
await tick();
} }
async function update() { async function update() {
dropdown = dropdown as Dropdown;
dropdown.update(); dropdown.update();
await tick(); await tick();
@ -97,6 +97,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
return selected !== null; return selected !== null;
} }
function disable(): void {
disabled = true;
}
const createAutocomplete = const createAutocomplete =
(createDropdown: (element: HTMLElement) => Dropdown) => (createDropdown: (element: HTMLElement) => Dropdown) =>
(element: HTMLElement): any => { (element: HTMLElement): any => {
@ -110,8 +114,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
selectPrevious, selectPrevious,
selectNext, selectNext,
chooseSelected, chooseSelected,
hasSelected,
update, update,
hasSelected,
disable,
}; };
return api; return api;