mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
Clear ComboBox Programaticaly (#804)
* Added clear function * Dispatching clear event * Exporting function clear * Added example
This commit is contained in:
parent
18e8430646
commit
dfc00f6668
2 changed files with 26 additions and 7 deletions
|
@ -123,6 +123,14 @@
|
|||
highlightedIndex = index;
|
||||
}
|
||||
|
||||
export function clear() {
|
||||
selectedIndex = -1;
|
||||
highlightedIndex = -1;
|
||||
open = false;
|
||||
inputValue = "";
|
||||
ref?.focus();
|
||||
}
|
||||
|
||||
afterUpdate(() => {
|
||||
if (open) {
|
||||
ref.focus();
|
||||
|
@ -140,6 +148,9 @@
|
|||
$: if (selectedIndex > -1) {
|
||||
selectedId = items[selectedIndex].id;
|
||||
dispatch("select", { selectedId, selectedIndex, selectedItem });
|
||||
} else {
|
||||
clear();
|
||||
dispatch("clear");
|
||||
}
|
||||
$: ariaLabel = $$props["aria-label"] || "Choose an item";
|
||||
$: menuId = `menu-${id}`;
|
||||
|
@ -286,13 +297,7 @@
|
|||
{#if inputValue}
|
||||
<ListBoxSelection
|
||||
on:clear
|
||||
on:clear="{() => {
|
||||
selectedIndex = -1;
|
||||
highlightedIndex = -1;
|
||||
open = false;
|
||||
inputValue = '';
|
||||
ref.focus();
|
||||
}}"
|
||||
on:clear="{clear}"
|
||||
translateWithId="{translateWithId}"
|
||||
disabled="{disabled}"
|
||||
open="{open}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue