mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
fix(ComboBox): programmatically set selected index (#995)
* fix(ComboBox): programmatically set selected index * refactor(ComboBox): renaming example to Reactive example
This commit is contained in:
parent
8936b9b5fa
commit
d31e529ca9
3 changed files with 45 additions and 13 deletions
|
@ -3,7 +3,7 @@
|
||||||
import { Button } from "carbon-components-svelte";
|
import { Button } from "carbon-components-svelte";
|
||||||
import Preview from "../../components/Preview.svelte";
|
import Preview from "../../components/Preview.svelte";
|
||||||
let comboboxComponent
|
let comboboxComponent
|
||||||
|
let selectedIndex = 1
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
### Default
|
### Default
|
||||||
|
@ -25,7 +25,12 @@ items={[
|
||||||
{id: "2", text: "Fax"}
|
{id: "2", text: "Fax"}
|
||||||
]} />
|
]} />
|
||||||
|
|
||||||
|
### Reactive example
|
||||||
|
|
||||||
|
<FileSource src="/framed/ComboBox/ReactiveComboBox" />
|
||||||
|
|
||||||
### Clear selection
|
### Clear selection
|
||||||
|
|
||||||
<ComboBox titleText="Contact" placeholder="Select contact method"
|
<ComboBox titleText="Contact" placeholder="Select contact method"
|
||||||
bind:this={comboboxComponent}
|
bind:this={comboboxComponent}
|
||||||
items={[
|
items={[
|
||||||
|
|
18
docs/src/pages/framed/ComboBox/ReactiveComboBox.svelte
Normal file
18
docs/src/pages/framed/ComboBox/ReactiveComboBox.svelte
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<script>
|
||||||
|
import { ComboBox, Button } from "carbon-components-svelte";
|
||||||
|
let selectedIndex = 1;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<ComboBox
|
||||||
|
titleText="Contact"
|
||||||
|
placeholder="Select contact method"
|
||||||
|
bind:selectedIndex
|
||||||
|
items="{[
|
||||||
|
{ id: '0', text: 'Slack' },
|
||||||
|
{ id: '1', text: 'Email' },
|
||||||
|
{ id: '2', text: 'Fax' },
|
||||||
|
]}"
|
||||||
|
/>
|
||||||
|
<br />
|
||||||
|
<Button on:click="{() => (selectedIndex = -1)}">Set to -1 (unselected)</Button>
|
||||||
|
<Button on:click="{() => (selectedIndex = 2)}">Set to 2 (Fax)</Button>
|
|
@ -110,7 +110,6 @@
|
||||||
let selectedId = undefined;
|
let selectedId = undefined;
|
||||||
let selectedItem = undefined;
|
let selectedItem = undefined;
|
||||||
let inputValue = value;
|
let inputValue = value;
|
||||||
let prevInputValue = undefined;
|
|
||||||
let prevSelectedIndex = undefined;
|
let prevSelectedIndex = undefined;
|
||||||
let highlightedIndex = -1;
|
let highlightedIndex = -1;
|
||||||
|
|
||||||
|
@ -130,6 +129,7 @@
|
||||||
* @type {() => void}
|
* @type {() => void}
|
||||||
*/
|
*/
|
||||||
export function clear() {
|
export function clear() {
|
||||||
|
prevSelectedIndex = undefined;
|
||||||
selectedIndex = -1;
|
selectedIndex = -1;
|
||||||
highlightedIndex = -1;
|
highlightedIndex = -1;
|
||||||
highlightedId = undefined;
|
highlightedId = undefined;
|
||||||
|
@ -150,6 +150,10 @@
|
||||||
if (!selectedItem) {
|
if (!selectedItem) {
|
||||||
selectedId = undefined;
|
selectedId = undefined;
|
||||||
selectedIndex = -1;
|
selectedIndex = -1;
|
||||||
|
inputValue = "";
|
||||||
|
highlightedIndex = -1;
|
||||||
|
highlightedId = undefined;
|
||||||
|
prevSelectedIndex = undefined;
|
||||||
} else {
|
} else {
|
||||||
// programmatically set selectedIndex
|
// programmatically set selectedIndex
|
||||||
inputValue = selectedItem.text;
|
inputValue = selectedItem.text;
|
||||||
|
@ -157,7 +161,8 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$: if (selectedIndex > -1 && prevSelectedIndex !== selectedIndex) {
|
$: if (selectedIndex > -1) {
|
||||||
|
if (prevSelectedIndex !== selectedIndex) {
|
||||||
prevSelectedIndex = selectedIndex;
|
prevSelectedIndex = selectedIndex;
|
||||||
if (filteredItems?.length === 1 && open) {
|
if (filteredItems?.length === 1 && open) {
|
||||||
selectedId = filteredItems[0].id;
|
selectedId = filteredItems[0].id;
|
||||||
|
@ -170,6 +175,10 @@
|
||||||
}
|
}
|
||||||
dispatch("select", { selectedId, selectedIndex, selectedItem });
|
dispatch("select", { selectedId, selectedIndex, selectedItem });
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
prevSelectedIndex = selectedIndex;
|
||||||
|
selectedItem = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
$: ariaLabel = $$props["aria-label"] || "Choose an item";
|
$: ariaLabel = $$props["aria-label"] || "Choose an item";
|
||||||
$: menuId = `menu-${id}`;
|
$: menuId = `menu-${id}`;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue