mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 18:31: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
|
@ -1,6 +1,9 @@
|
||||||
<script>
|
<script>
|
||||||
import { ComboBox } from "carbon-components-svelte";
|
import { ComboBox } from "carbon-components-svelte";
|
||||||
|
import { Button } from "carbon-components-svelte";
|
||||||
import Preview from "../../components/Preview.svelte";
|
import Preview from "../../components/Preview.svelte";
|
||||||
|
let comboboxComponent
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
### Default
|
### Default
|
||||||
|
@ -22,6 +25,17 @@ items={[
|
||||||
{id: "2", text: "Fax"}
|
{id: "2", text: "Fax"}
|
||||||
]} />
|
]} />
|
||||||
|
|
||||||
|
### Clear selection
|
||||||
|
<ComboBox titleText="Contact" placeholder="Select contact method"
|
||||||
|
bind:this={comboboxComponent}
|
||||||
|
items={[
|
||||||
|
{id: "0", text: "Slack"},
|
||||||
|
{id: "1", text: "Email"},
|
||||||
|
{id: "2", text: "Fax"}
|
||||||
|
]} />
|
||||||
|
<br>
|
||||||
|
<Button on:click={comboboxComponent.clear}>Clear</Button>
|
||||||
|
|
||||||
### Multiple combo boxes
|
### Multiple combo boxes
|
||||||
|
|
||||||
<FileSource src="/framed/ComboBox/MultipleComboBox" />
|
<FileSource src="/framed/ComboBox/MultipleComboBox" />
|
||||||
|
|
|
@ -123,6 +123,14 @@
|
||||||
highlightedIndex = index;
|
highlightedIndex = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function clear() {
|
||||||
|
selectedIndex = -1;
|
||||||
|
highlightedIndex = -1;
|
||||||
|
open = false;
|
||||||
|
inputValue = "";
|
||||||
|
ref?.focus();
|
||||||
|
}
|
||||||
|
|
||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
if (open) {
|
if (open) {
|
||||||
ref.focus();
|
ref.focus();
|
||||||
|
@ -140,6 +148,9 @@
|
||||||
$: 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();
|
||||||
|
dispatch("clear");
|
||||||
}
|
}
|
||||||
$: ariaLabel = $$props["aria-label"] || "Choose an item";
|
$: ariaLabel = $$props["aria-label"] || "Choose an item";
|
||||||
$: menuId = `menu-${id}`;
|
$: menuId = `menu-${id}`;
|
||||||
|
@ -286,13 +297,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}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue