diff --git a/COMPONENT_INDEX.md b/COMPONENT_INDEX.md index 37c737db..f412665b 100644 --- a/COMPONENT_INDEX.md +++ b/COMPONENT_INDEX.md @@ -2749,13 +2749,14 @@ No slots. ### Forwarded events +- `on:clear` - `on:keydown` - `on:focus` - `on:blur` ### Dispatched events -No dispatched events. +- `on:select` --- diff --git a/docs/src/PUBLIC_API.json b/docs/src/PUBLIC_API.json index 87b322e5..279d5abc 100644 --- a/docs/src/PUBLIC_API.json +++ b/docs/src/PUBLIC_API.json @@ -8051,11 +8051,22 @@ ], "slots": [], "forwarded_events": [ + [ + "clear", + { + "start": 8577, + "end": 8585, + "type": "EventHandler", + "name": "clear", + "modifiers": [], + "expression": null + } + ], [ "keydown", { - "start": 9267, - "end": 9277, + "start": 9473, + "end": 9483, "type": "EventHandler", "name": "keydown", "modifiers": [], @@ -8065,8 +8076,8 @@ [ "focus", { - "start": 9769, - "end": 9777, + "start": 9975, + "end": 9983, "type": "EventHandler", "name": "focus", "modifiers": [], @@ -8076,8 +8087,8 @@ [ "blur", { - "start": 9788, - "end": 9795, + "start": 9994, + "end": 10001, "type": "EventHandler", "name": "blur", "modifiers": [], @@ -8085,7 +8096,14 @@ } ] ], - "dispatched_events": [] + "dispatched_events": [ + [ + "select", + { + "detail": "{\n selectedIds,\n selected: checked,\n unselected: unchecked,\n }" + } + ] + ] }, "NotificationActionButton": { "moduleName": "NotificationActionButton", diff --git a/docs/src/pages/components/Dropdown.svx b/docs/src/pages/components/Dropdown.svx index a027819b..31c7d45f 100644 --- a/docs/src/pages/components/Dropdown.svx +++ b/docs/src/pages/components/Dropdown.svx @@ -13,6 +13,16 @@ components: ["Dropdown", "DropdownSkeleton"] {id: "1", text: "Email"}, {id: "2", text: "Fax"}]}" /> +### Format item display text + +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"}, + {id: "1", text: "Email"}, + {id: "2", text: "Fax"}]}" /> + ### Light variant ### No alphabetical ordering @@ -20,7 +20,32 @@ To prevent alphabetical item ordering, pass an empty function to the `sortItem` + +### Initial selected items + +To select (or bind) items, pass an array of item ids to `selectedIds`. + + + + + +### Format item display text + +Use the `itemToString` prop to format the display of individual items. + + { + return item.text + ' (' + item.id +')' +}} titleText="Contact" label="Select contact methods..." + items="{[{id: "0", text: "Slack"}, + {id: "1", text: "Email"}, + {id: "2", text: "Fax"}]}" sortItem="{() => {}}" /> diff --git a/src/MultiSelect/MultiSelect.svelte b/src/MultiSelect/MultiSelect.svelte index 447b0a72..95f02c9f 100644 --- a/src/MultiSelect/MultiSelect.svelte +++ b/src/MultiSelect/MultiSelect.svelte @@ -153,7 +153,7 @@ * @typedef {{ id: MultiSelectItemId; text: MultiSelectItemText; }} MultiSelectItem */ - import { afterUpdate, setContext } from "svelte"; + import { afterUpdate, createEventDispatcher, setContext } from "svelte"; import WarningFilled16 from "carbon-icons-svelte/lib/WarningFilled16"; import { Checkbox } from "../Checkbox"; import { @@ -165,15 +165,16 @@ ListBoxSelection, } from "../ListBox"; + const dispatch = createEventDispatcher(); + let multiSelectRef = null; let fieldRef = null; let selectionRef = null; let inputRef = null; - - $: inputValue = ""; - $: initialSorted = false; - $: highlightedIndex = -1; - $: prevChecked = []; + let inputValue = ""; + let initialSorted = false; + let highlightedIndex = -1; + let prevChecked = []; setContext("MultiSelect", { declareRef: ({ key, ref }) => { @@ -214,6 +215,11 @@ } prevChecked = checked; selectedIds = checked.map(({ id }) => id); + dispatch("select", { + selectedIds, + selected: checked, + unselected: unchecked, + }); } if (!open) { @@ -335,6 +341,7 @@ {#if checked.length > 0}