Show Autocomplete on keydown, hide on blur

This commit is contained in:
Henrik Giesel 2021-06-25 23:24:45 +02:00
parent c8ac822971
commit 65e9a0f2ed
4 changed files with 28 additions and 11 deletions

View file

@ -43,6 +43,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
bind:input bind:input
on:focus on:focus
on:blur={() => (active = false)} on:blur={() => (active = false)}
on:blur
on:keydown
on:tagupdate={updateTag} on:tagupdate={updateTag}
on:tagadd on:tagadd
on:tagjoinprevious on:tagjoinprevious

View file

@ -15,6 +15,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export { className as class }; export { className as class };
let autocomplete: Dropdown | undefined; let autocomplete: Dropdown | undefined;
let displayed: string[] = [];
function switchUpDown(event: KeyboardEvent): void { function switchUpDown(event: KeyboardEvent): void {
const target = event.currentTarget as HTMLButtonElement; const target = event.currentTarget as HTMLButtonElement;
@ -33,16 +34,30 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
} }
const createAutocomplete = const updateAutocomplete =
(createDropdown: (element: HTMLElement) => Dropdown) => (createDropdown: (element: HTMLElement) => Dropdown) =>
(element: HTMLElement) => { (event: KeyboardEvent): Dropdown => {
autocomplete = createDropdown(element); const target = event.target as HTMLElement;
autocomplete = createDropdown(target);
autocomplete.show();
return autocomplete; return autocomplete;
}; };
function destroyAutocomplete(): void {
if (!autocomplete) {
return;
}
autocomplete.hide();
}
</script> </script>
<WithDropdownMenu let:menuId let:createDropdown> <WithDropdownMenu let:menuId let:createDropdown>
<slot createAutocomplete={createAutocomplete(createDropdown)} /> <slot
updateAutocomplete={updateAutocomplete(createDropdown)}
{destroyAutocomplete}
/>
<DropdownMenu id={menuId} class={className}> <DropdownMenu id={menuId} class={className}>
{#each suggestions as tag} {#each suggestions as tag}

View file

@ -27,7 +27,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
newInput.blur(); newInput.blur();
} }
console.log("focus");
newInput.focus(); newInput.focus();
} }
@ -105,13 +104,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<TagAutocomplete <TagAutocomplete
class="d-flex flex-column-reverse" class="d-flex flex-column-reverse"
{suggestions} {suggestions}
let:createAutocomplete let:updateAutocomplete
let:destroyAutocomplete
> >
{#each tags as tag, index (tag.id)} {#each tags as tag, index (tag.id)}
<Tag <Tag
bind:name={tag.name} bind:name={tag.name}
on:keydown on:keydown={updateAutocomplete}
on:focus={(event) => createAutocomplete(event.target)} on:blur={destroyAutocomplete}
on:tagupdate={() => checkForDuplicateAt(index)} on:tagupdate={() => checkForDuplicateAt(index)}
on:tagadd={() => insertTagAt(index)} on:tagadd={() => insertTagAt(index)}
on:tagdelete={() => deleteTagAt(index)} on:tagdelete={() => deleteTagAt(index)}
@ -123,8 +123,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<TagInput <TagInput
bind:input={newInput} bind:input={newInput}
bind:name={newName} bind:name={newName}
on:focus={(event) => createAutocomplete(event.target)} on:keydown={updateAutocomplete}
on:keydown on:blur={destroyAutocomplete}
on:tagupdate={appendTag} on:tagupdate={appendTag}
on:tagadd={appendTag} on:tagadd={appendTag}
on:tagjoinprevious={joinWithLastTag} on:tagjoinprevious={joinWithLastTag}

View file

@ -51,7 +51,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
onAccept(); onAccept();
event.preventDefault(); event.preventDefault();
} }
console.log("keydown");
} }
function onPaste(event: ClipboardEvent): void { function onPaste(event: ClipboardEvent): void {
@ -100,6 +99,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:blur on:blur
on:blur={onAccept} on:blur={onAccept}
on:keydown={onKeydown} on:keydown={onKeydown}
on:keydown
on:paste={onPaste} on:paste={onPaste}
on:click on:click
/> />