svelte-multiselect 6.0.1 → 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MultiSelect.svelte +2 -4
- package/package.json +1 -1
- package/readme.md +47 -23
package/MultiSelect.svelte
CHANGED
|
@@ -42,7 +42,7 @@ export let removeAllTitle = `Remove all`;
|
|
|
42
42
|
export let removeBtnTitle = `Remove`;
|
|
43
43
|
export let required = false;
|
|
44
44
|
export let searchText = ``;
|
|
45
|
-
export let selected = [];
|
|
45
|
+
export let selected = options?.filter((op) => op?.preselected) ?? [];
|
|
46
46
|
export let selectedLabels = [];
|
|
47
47
|
export let selectedValues = [];
|
|
48
48
|
export let sortSelected = false;
|
|
@@ -78,9 +78,7 @@ $: formValue = selectedValues.join(`,`);
|
|
|
78
78
|
$: if (formValue)
|
|
79
79
|
invalid = false; // reset error status whenever component state changes
|
|
80
80
|
// options matching the current search text
|
|
81
|
-
$: matchingOptions = options.filter((op) => filterFunc(op, searchText) &&
|
|
82
|
-
!(op instanceof Object && op.disabled) &&
|
|
83
|
-
!selectedLabels.includes(get_label(op)) // remove already selected options from dropdown list
|
|
81
|
+
$: matchingOptions = options.filter((op) => filterFunc(op, searchText) && !selectedLabels.includes(get_label(op)) // remove already selected options from dropdown list
|
|
84
82
|
);
|
|
85
83
|
// raise if matchingOptions[activeIndex] does not yield a value
|
|
86
84
|
if (activeIndex !== null && !matchingOptions[activeIndex]) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -263,7 +263,7 @@ import type { Option } from 'svelte-multiselect'
|
|
|
263
263
|
Text the user-entered to filter down on the list of options. Binds both ways, i.e. can also be used to set the input text.
|
|
264
264
|
|
|
265
265
|
1. ```ts
|
|
266
|
-
selected: Option[] = []
|
|
266
|
+
selected: Option[] = options?.filter((op) => op?.preselected) ?? []
|
|
267
267
|
```
|
|
268
268
|
|
|
269
269
|
Array of currently selected options. Can be bound to `bind:selected={[1, 2, 3]}` to control component state externally or passed as prop to set pre-selected options that will already be populated when component mounts before any user interaction.
|
|
@@ -286,6 +286,13 @@ import type { Option } from 'svelte-multiselect'
|
|
|
286
286
|
|
|
287
287
|
Default behavior is to render selected items in the order they were chosen. `sortSelected={true}` uses default JS array sorting. A compare function enables custom logic for sorting selected options. See the [`/sort-selected`](https://svelte-multiselect.netlify.app/sort-selected) example.
|
|
288
288
|
|
|
289
|
+
1. ```ts
|
|
290
|
+
userInputAs: 'string' | 'number' | 'object' =
|
|
291
|
+
options.length > 0 ? (typeof options[0] as 'object' | 'string' | 'number') : 'string'
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
What type new options created from user text input should be. Only relevant if `allowUserOptions=true | 'append'`. If not explicitly set, we default `userInputAs` to the type of the 1st option (if available, else `string`) to keep type homogeneity. E.g. if MultiSelect already contains at least one option and it's an object, new options from user-entered text will take the shape `{label: userText, value: userText}`. Likewise if the 1st existing option is a number of string. If MultiSelect starts out empty but you still want user-created custom options to be objects, pass `userInputAs='object'`.
|
|
295
|
+
|
|
289
296
|
## Slots
|
|
290
297
|
|
|
291
298
|
`MultiSelect.svelte` has 3 named slots:
|
|
@@ -321,34 +328,50 @@ Example:
|
|
|
321
328
|
|
|
322
329
|
`MultiSelect.svelte` dispatches the following events:
|
|
323
330
|
|
|
324
|
-
|
|
331
|
+
1. ```ts
|
|
332
|
+
on:add={(event) => console.log(event.detail.option)}
|
|
333
|
+
```
|
|
325
334
|
|
|
326
|
-
|
|
327
|
-
| ----------- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
328
|
-
| `add` | `{ option: Option }` | Triggers when a new option is selected. |
|
|
329
|
-
| `remove` | `{ option: Option }` | Triggers when one selected option provided as `event.detail.option` is removed. |
|
|
330
|
-
| `removeAll` | `options: Option[]` | Triggers when all selected options are removed. The payload `event.detail.options` gives the options that were previously selected. |
|
|
331
|
-
| `change` | `type: 'add' \| 'remove' \| 'removeAll'` | Triggers when a option is either added or removed, or all options are removed at once. Payload will be a single or an array of `Option`s, respectively. |
|
|
332
|
-
| `blur` | none | Triggers when the input field looses focus. |
|
|
335
|
+
Triggers when a new option is selected.
|
|
333
336
|
|
|
334
|
-
|
|
337
|
+
1. ```ts
|
|
338
|
+
on:remove={(event) => console.log(event.detail.option)}`
|
|
339
|
+
```
|
|
335
340
|
|
|
336
|
-
|
|
341
|
+
Triggers when one selected option provided as `event.detail.option` is removed.
|
|
337
342
|
|
|
338
|
-
|
|
343
|
+
1. ```ts
|
|
344
|
+
on:removeAll={(event) => console.log(event.detail.options)}`
|
|
345
|
+
```
|
|
339
346
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
347
|
+
Triggers when all selected options are removed. The payload `event.detail.options` gives the options that were previously selected.
|
|
348
|
+
|
|
349
|
+
1. ```ts
|
|
350
|
+
on:change={(event) => console.log(`${event.detail.type}: '${event.detail.option}'`)}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
Triggers when an option is either added or removed, or all options are removed at once. `type` is one of `'add' | 'remove' | 'removeAll'` and payload will be `option: Option` or `options: Option[]`, respectively.
|
|
354
|
+
|
|
355
|
+
1. ```ts
|
|
356
|
+
on:blur={() => console.log('Multiselect input lost focus')}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Triggers when the input field looses focus.
|
|
360
|
+
|
|
361
|
+
For example, here's how you might annoy your users with an alert every time one or more options are added or removed:
|
|
345
362
|
|
|
346
363
|
```svelte
|
|
347
364
|
<MultiSelect
|
|
348
|
-
on:change={(e) =>
|
|
365
|
+
on:change={(e) => {
|
|
366
|
+
if (e.detail.type === 'add') alert(`You added ${e.detail.option}`)
|
|
367
|
+
if (e.detail.type === 'remove') alert(`You removed ${e.detail.option}`)
|
|
368
|
+
if (e.detail.type === 'removeAll') alert(`You removed ${e.detail.options}`)
|
|
369
|
+
}}
|
|
349
370
|
/>
|
|
350
371
|
```
|
|
351
372
|
|
|
373
|
+
> Note: Depending on the data passed to the component the `options(s)` payload will either be objects or simple strings/numbers.
|
|
374
|
+
|
|
352
375
|
## TypeScript
|
|
353
376
|
|
|
354
377
|
TypeScript users can import the types used for internal type safety:
|
|
@@ -385,7 +408,7 @@ There are 3 ways to style this component. To understand which options do what, i
|
|
|
385
408
|
|
|
386
409
|
### With CSS variables
|
|
387
410
|
|
|
388
|
-
If you only want to make small adjustments, you can pass the following CSS variables directly to the component as props or define them in a `:global()` CSS context. See [`app.css`](https://github.com/janosh/svelte-multiselect/blob/main/src/app.css) for how these variables are set
|
|
411
|
+
If you only want to make small adjustments, you can pass the following CSS variables directly to the component as props or define them in a `:global()` CSS context. See [`app.css`](https://github.com/janosh/svelte-multiselect/blob/main/src/app.css) for how these variables are set on the demo site of this component.
|
|
389
412
|
|
|
390
413
|
- `div.multiselect`
|
|
391
414
|
- `border: var(--sms-border, 1pt solid lightgray)`: Change this to e.g. to `1px solid red` to indicate this form field is in an invalid state.
|
|
@@ -393,9 +416,10 @@ If you only want to make small adjustments, you can pass the following CSS varia
|
|
|
393
416
|
- `padding: var(--sms-padding, 0 3pt)`
|
|
394
417
|
- `background: var(--sms-bg)`
|
|
395
418
|
- `color: var(--sms-text-color)`
|
|
396
|
-
- `min-height: var(--sms-min-height)`
|
|
419
|
+
- `min-height: var(--sms-min-height, 19pt)`
|
|
397
420
|
- `max-width: var(--sms-max-width)`
|
|
398
421
|
- `margin: var(--sms-margin)`
|
|
422
|
+
- `font-size: var(--sms-font-size, inherit)`
|
|
399
423
|
- `div.multiselect.open`
|
|
400
424
|
- `z-index: var(--sms-open-z-index, 4)`: Increase this if needed to ensure the dropdown list is displayed atop all other page elements.
|
|
401
425
|
- `div.multiselect:focus-within`
|
|
@@ -404,10 +428,10 @@ If you only want to make small adjustments, you can pass the following CSS varia
|
|
|
404
428
|
- `background: var(--sms-disabled-bg, lightgray)`: Background when in disabled state.
|
|
405
429
|
- `div.multiselect input::placeholder`
|
|
406
430
|
- `color: var(--sms-placeholder-color)`
|
|
407
|
-
- `
|
|
431
|
+
- `opacity: var(--sms-placeholder-opacity)`
|
|
408
432
|
- `div.multiselect > ul.selected > li`
|
|
409
433
|
- `background: var(--sms-selected-bg, rgba(0, 0, 0, 0.15))`: Background of selected options.
|
|
410
|
-
- `padding: var(--sms-selected-li-padding, 5pt
|
|
434
|
+
- `padding: var(--sms-selected-li-padding, 1pt 5pt)`: Height of selected options.
|
|
411
435
|
- `color: var(--sms-selected-text-color, var(--sms-text-color))`: Text color for selected options.
|
|
412
436
|
- `ul.selected > li button:hover, button.remove-all:hover, button:focus`
|
|
413
437
|
- `color: var(--sms-button-hover-color, lightskyblue)`: Color of the remove-icon buttons for removing all or individual selected options when in `:focus` or `:hover` state.
|
|
@@ -415,7 +439,7 @@ If you only want to make small adjustments, you can pass the following CSS varia
|
|
|
415
439
|
- `background: var(--sms-options-bg, white)`: Background of dropdown list.
|
|
416
440
|
- `max-height: var(--sms-options-max-height, 50vh)`: Maximum height of options dropdown.
|
|
417
441
|
- `overscroll-behavior: var(--sms-options-overscroll, none)`: Whether scroll events bubble to parent elements when reaching the top/bottom of the options dropdown. See [MDN](https://developer.mozilla.org/docs/Web/CSS/overscroll-behavior).
|
|
418
|
-
- `box-shadow: var(--sms-options-shadow, 0 0 14pt -8pt black)
|
|
442
|
+
- `box-shadow: var(--sms-options-shadow, 0 0 14pt -8pt black)`: Box shadow of dropdown list.
|
|
419
443
|
- `div.multiselect > ul.options > li`
|
|
420
444
|
- `scroll-margin: var(--sms-options-scroll-margin, 100px)`: Top/bottom margin to keep between dropdown list items and top/bottom screen edge when auto-scrolling list to keep items in view.
|
|
421
445
|
- `div.multiselect > ul.options > li.selected`
|