Merge pull request #380 from IBM/docs-multiselect

MultiSelect: forward "clear" event, dispatch "select" event
This commit is contained in:
Eric Liu 2020-10-30 06:06:00 -07:00 committed by GitHub
commit ba5462072c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 16 deletions

View file

@ -2749,13 +2749,14 @@ No slots.
### Forwarded events ### Forwarded events
- `on:clear`
- `on:keydown` - `on:keydown`
- `on:focus` - `on:focus`
- `on:blur` - `on:blur`
### Dispatched events ### Dispatched events
No dispatched events. - `on:select`
--- ---

View file

@ -8051,11 +8051,22 @@
], ],
"slots": [], "slots": [],
"forwarded_events": [ "forwarded_events": [
[
"clear",
{
"start": 8577,
"end": 8585,
"type": "EventHandler",
"name": "clear",
"modifiers": [],
"expression": null
}
],
[ [
"keydown", "keydown",
{ {
"start": 9267, "start": 9473,
"end": 9277, "end": 9483,
"type": "EventHandler", "type": "EventHandler",
"name": "keydown", "name": "keydown",
"modifiers": [], "modifiers": [],
@ -8065,8 +8076,8 @@
[ [
"focus", "focus",
{ {
"start": 9769, "start": 9975,
"end": 9777, "end": 9983,
"type": "EventHandler", "type": "EventHandler",
"name": "focus", "name": "focus",
"modifiers": [], "modifiers": [],
@ -8076,8 +8087,8 @@
[ [
"blur", "blur",
{ {
"start": 9788, "start": 9994,
"end": 9795, "end": 10001,
"type": "EventHandler", "type": "EventHandler",
"name": "blur", "name": "blur",
"modifiers": [], "modifiers": [],
@ -8085,7 +8096,14 @@
} }
] ]
], ],
"dispatched_events": [] "dispatched_events": [
[
"select",
{
"detail": "{\n selectedIds,\n selected: checked,\n unselected: unchecked,\n }"
}
]
]
}, },
"NotificationActionButton": { "NotificationActionButton": {
"moduleName": "NotificationActionButton", "moduleName": "NotificationActionButton",

View file

@ -13,6 +13,16 @@ components: ["Dropdown", "DropdownSkeleton"]
{id: "1", text: "Email"}, {id: "1", text: "Email"},
{id: "2", text: "Fax"}]}" /> {id: "2", text: "Fax"}]}" />
### Format item display text
Use the `itemToString` prop to format the display of individual items.
<Dropdown itemToString={item => {
return item.text + ' (' + item.id +')'
}} titleText="Contact" selectedIndex={0} items="{[{id: "0", text: "Slack"},
{id: "1", text: "Email"},
{id: "2", text: "Fax"}]}" />
### Light variant ### Light variant
<Dropdown light titleText="Contact" selectedIndex={0} items="{[{id: "0", text: "Slack"}, <Dropdown light titleText="Contact" selectedIndex={0} items="{[{id: "0", text: "Slack"},

View file

@ -10,7 +10,7 @@ By default, items will be ordered alphabetically based on the `item.text` value.
<MultiSelect titleText="Contact" label="Select contact methods..." <MultiSelect titleText="Contact" label="Select contact methods..."
items="{[{id: "0", text: "Slack"}, items="{[{id: "0", text: "Slack"},
{id: "1", text: "Email"}, {id: "1", text: "Email"},
{id: "2", text: "Fax"}]}" selectedIds="{["0", "1"]}" {id: "2", text: "Fax"}]}"
/> />
### No alphabetical ordering ### No alphabetical ordering
@ -20,7 +20,32 @@ To prevent alphabetical item ordering, pass an empty function to the `sortItem`
<MultiSelect titleText="Contact" label="Select contact methods..." <MultiSelect titleText="Contact" label="Select contact methods..."
items="{[{id: "0", text: "Slack"}, items="{[{id: "0", text: "Slack"},
{id: "1", text: "Email"}, {id: "1", text: "Email"},
{id: "2", text: "Fax"}]}" selectedIds="{["0", "1"]}" {id: "2", text: "Fax"}]}"
sortItem="{() => {}}"
/>
### Initial selected items
To select (or bind) items, pass an array of item ids to `selectedIds`.
<MultiSelect selectedIds="{["0", "1"]}" titleText="Contact" label="Select contact methods..."
items="{[{id: "0", text: "Slack"},
{id: "1", text: "Email"},
{id: "2", text: "Fax"}]}"
/>
### Format item display text
Use the `itemToString` prop to format the display of individual items.
<MultiSelect itemToString={item => {
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="{() => {}}" sortItem="{() => {}}"
/> />

View file

@ -153,7 +153,7 @@
* @typedef {{ id: MultiSelectItemId; text: MultiSelectItemText; }} MultiSelectItem * @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 WarningFilled16 from "carbon-icons-svelte/lib/WarningFilled16";
import { Checkbox } from "../Checkbox"; import { Checkbox } from "../Checkbox";
import { import {
@ -165,15 +165,16 @@
ListBoxSelection, ListBoxSelection,
} from "../ListBox"; } from "../ListBox";
const dispatch = createEventDispatcher();
let multiSelectRef = null; let multiSelectRef = null;
let fieldRef = null; let fieldRef = null;
let selectionRef = null; let selectionRef = null;
let inputRef = null; let inputRef = null;
let inputValue = "";
$: inputValue = ""; let initialSorted = false;
$: initialSorted = false; let highlightedIndex = -1;
$: highlightedIndex = -1; let prevChecked = [];
$: prevChecked = [];
setContext("MultiSelect", { setContext("MultiSelect", {
declareRef: ({ key, ref }) => { declareRef: ({ key, ref }) => {
@ -214,6 +215,11 @@
} }
prevChecked = checked; prevChecked = checked;
selectedIds = checked.map(({ id }) => id); selectedIds = checked.map(({ id }) => id);
dispatch("select", {
selectedIds,
selected: checked,
unselected: unchecked,
});
} }
if (!open) { if (!open) {
@ -335,6 +341,7 @@
{#if checked.length > 0} {#if checked.length > 0}
<ListBoxSelection <ListBoxSelection
selectionCount="{checked.length}" selectionCount="{checked.length}"
on:clear
on:clear="{() => { on:clear="{() => {
sortedItems = sortedItems.map((item) => ({ sortedItems = sortedItems.map((item) => ({
...item, ...item,