Added clear function

This commit is contained in:
Rafael Dessotti 2021-08-31 08:26:09 -03:00
commit ae1d34bd3e

View file

@ -123,6 +123,14 @@
highlightedIndex = index; highlightedIndex = index;
} }
function clear() {
selectedIndex = -1;
highlightedIndex = -1;
open = false;
inputValue = "";
ref?.focus();
}
afterUpdate(() => { afterUpdate(() => {
if (open) { if (open) {
ref.focus(); ref.focus();
@ -140,6 +148,8 @@
$: if (selectedIndex > -1) { $: if (selectedIndex > -1) {
selectedId = items[selectedIndex].id; selectedId = items[selectedIndex].id;
dispatch("select", { selectedId, selectedIndex, selectedItem }); dispatch("select", { selectedId, selectedIndex, selectedItem });
} else {
clear();
} }
$: ariaLabel = $$props["aria-label"] || "Choose an item"; $: ariaLabel = $$props["aria-label"] || "Choose an item";
$: menuId = `menu-${id}`; $: menuId = `menu-${id}`;
@ -286,13 +296,7 @@
{#if inputValue} {#if inputValue}
<ListBoxSelection <ListBoxSelection
on:clear on:clear
on:clear="{() => { on:clear="{clear}"
selectedIndex = -1;
highlightedIndex = -1;
open = false;
inputValue = '';
ref.focus();
}}"
translateWithId="{translateWithId}" translateWithId="{translateWithId}"
disabled="{disabled}" disabled="{disabled}"
open="{open}" open="{open}"