diff --git a/COMPONENT_INDEX.md b/COMPONENT_INDEX.md
index 6d73a94c..2b716690 100644
--- a/COMPONENT_INDEX.md
+++ b/COMPONENT_INDEX.md
@@ -1166,7 +1166,7 @@ export interface DropdownItem {
| ref | let
| Yes | null | HTMLButtonElement
| null
| Obtain a reference to the button HTML element |
| inline | let
| Yes | boolean
| false
| Set to `true` to use the inline variant |
| open | let
| Yes | boolean
| false
| Set to `true` to open the dropdown |
-| selectedIndex | let
| Yes | number
| -1
| Specify the selected item index |
+| selectedId | let
| Yes | DropdownItemId
| -- | Specify the selected item id |
| items | let
| No | DropdownItem[]
| []
| Set the dropdown items |
| itemToString | let
| No | (item: DropdownItem) => string
| (item) => item.text || item.id
| Override the display of a dropdown item |
| type | let
| No | "default" | "inline"
| "default"
| Specify the type of dropdown |
diff --git a/docs/src/COMPONENT_API.json b/docs/src/COMPONENT_API.json
index 8e0845d3..4b3dd7b8 100644
--- a/docs/src/COMPONENT_API.json
+++ b/docs/src/COMPONENT_API.json
@@ -3008,11 +3008,10 @@
"reactive": false
},
{
- "name": "selectedIndex",
+ "name": "selectedId",
"kind": "let",
- "description": "Specify the selected item index",
- "type": "number",
- "value": "-1",
+ "description": "Specify the selected item id",
+ "type": "DropdownItemId",
"isFunction": false,
"isFunctionDeclaration": false,
"constant": false,
diff --git a/docs/src/pages/components/Dropdown.svx b/docs/src/pages/components/Dropdown.svx
index 58fa35ff..5928962d 100644
--- a/docs/src/pages/components/Dropdown.svx
+++ b/docs/src/pages/components/Dropdown.svx
@@ -9,13 +9,13 @@ components: ["Dropdown", "DropdownSkeleton"]
### Default
-
### Hidden label
-
@@ -25,7 +25,7 @@ Use the `itemToString` prop to format the display of individual items.
{
return item.text + ' (' + item.id +')'
-}} titleText="Contact" selectedIndex={0} items="{[{id: "0", text: "Slack"},
+}} titleText="Contact" selectedId="0" items="{[{id: "0", text: "Slack"},
{id: "1", text: "Email"},
{id: "2", text: "Fax"}]}" />
@@ -37,49 +37,49 @@ Use the `itemToString` prop to format the display of individual items.
Set `direction` to `"top"` for the dropdown menu to appear above the input.
-
### Light variant
-
### Inline variant
-
### Extra-large size
-
### Small size
-
### Invalid state
-
### Warning state
-
### Disabled state
-
diff --git a/docs/src/pages/framed/Dropdown/MultipleDropdown.svelte b/docs/src/pages/framed/Dropdown/MultipleDropdown.svelte
index 4ebb7fcb..0de755a2 100644
--- a/docs/src/pages/framed/Dropdown/MultipleDropdown.svelte
+++ b/docs/src/pages/framed/Dropdown/MultipleDropdown.svelte
@@ -7,28 +7,29 @@
{ id: "2", text: "Fax" },
];
- let dropdown1_selectedIndex = 0;
- let dropdown2_selectedIndex = 1;
+ let dropdown1_selectedId = "0";
+ let dropdown2_selectedId = "1";
- const formatSelected = (i) => (items[i] ? items[i].text : "N/A");
+ const formatSelected = (id) =>
+ items.find((item) => item.id === id)?.text ?? "N/A";
- $: primary = formatSelected(dropdown1_selectedIndex);
- $: secondary = formatSelected(dropdown2_selectedIndex);
+ $: primary = formatSelected(dropdown1_selectedId);
+ $: secondary = formatSelected(dropdown2_selectedId);
Primary: {primary}
diff --git a/src/Dropdown/Dropdown.svelte b/src/Dropdown/Dropdown.svelte
index d6732b74..18baba6e 100644
--- a/src/Dropdown/Dropdown.svelte
+++ b/src/Dropdown/Dropdown.svelte
@@ -18,8 +18,11 @@
*/
export let itemToString = (item) => item.text || item.id;
- /** Specify the selected item index */
- export let selectedIndex = -1;
+ /**
+ * Specify the selected item id
+ * @type {DropdownItemId}
+ */
+ export let selectedId = undefined;
/**
* Specify the type of dropdown
@@ -108,7 +111,6 @@
const dispatch = createEventDispatcher();
- let selectedId = undefined;
let highlightedIndex = -1;
function change(dir) {
@@ -123,12 +125,12 @@
highlightedIndex = index;
}
- $: if (selectedIndex > -1) {
+ $: if (selectedId !== undefined) {
dispatch("select", { selectedId, selectedIndex, selectedItem });
}
$: inline = type === "inline";
+ $: selectedIndex = items.findIndex((item) => item.id === selectedId);
$: selectedItem = items[selectedIndex];
- $: selectedId = items[selectedIndex] ? items[selectedIndex].id : undefined;
$: if (!open) {
highlightedIndex = -1;
}
@@ -206,8 +208,11 @@
}
if (key === 'Enter') {
open = !open;
- if (highlightedIndex > -1 && highlightedIndex !== selectedIndex) {
- selectedIndex = highlightedIndex;
+ if (
+ highlightedIndex > -1 &&
+ items[highlightedIndex].id !== selectedId
+ ) {
+ selectedId = items[highlightedIndex].id;
open = false;
}
} else if (key === 'Tab') {
@@ -233,11 +238,10 @@
{#each items as item, i (item.id)}