Skip to content

Commit ef6598e

Browse files
committed
remove props selectedLabels and selectedValues
1 parent 9287494 commit ef6598e

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

readme.md

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636

3737
## Recent breaking changes
3838

39-
- **v5.0.0** Supports both simple and object options. Previously strings and numbers were converted to `{ value, label }` objects internally and returned by `bind:selected`. Now, if you pass in `string[]`, that's exactly what you'll get from `bind:selected`. See [PR 76](https://github.com/janosh/svelte-multiselect/pull/76).
40-
- **v6.0.0** The prop `showOptions` which controls whether the list of dropdown options is currently open or closed was renamed to `open`. See [PR 103](https://github.com/janosh/svelte-multiselect/pull/103).
41-
- **v6.0.1** The prop `disabledTitle` which sets the title of the `<MultiSelect>` `<input>` node if in `disabled` mode was renamed to `disabledInputTitle`. See [PR 105](https://github.com/janosh/svelte-multiselect/pull/105).
42-
- **v6.0.1** The default margin of `1em 0` on the wrapper `div.multiselect` was removed. Instead, there is now a new CSS variable `--sms-margin`. Set it to `--sms-margin: 1em 0;` to restore the old appearance. See [PR 105](https://github.com/janosh/svelte-multiselect/pull/105).
43-
- **6.1.0** The `dispatch` events `focus` and `blur` were renamed to `open` and `close`, respectively. These actions refer to the dropdown list, i.e. `<MultiSelect on:open={(event) => console.log(event)}>` will trigger when the dropdown list opens. The focus and blur events are now regular DOM (not Svelte `dispatch`) events emitted by the `<input>` node. See [PR 120](https://github.com/janosh/svelte-multiselect/pull/120).
44-
- **v7.0.0** `selected` (as well `selectedLabels` and `selectedValues`) used to be arrays always. Now, if `maxSelect=1`, they will no longer be a length-1 array but simply a single a option (label/value respectively) or `null` if no option is selected. See [PR 123](https://github.com/janosh/svelte-multiselect/pull/123).
45-
- **v7.1.1** Prop `noOptionsMsg` was renamed to `noMatchingOptionsMsg`. See [PR 133](https://github.com/janosh/svelte-multiselect/pull/123).
39+
- **v5.0.0**&nbsp; Supports both simple and object options. Previously strings and numbers were converted to `{ value, label }` objects internally and returned by `bind:selected`. Now, if you pass in `string[]`, that's exactly what you'll get from `bind:selected`. See [PR 76](https://github.com/janosh/svelte-multiselect/pull/76).
40+
- **v6.0.0**&nbsp; The prop `showOptions` which controls whether the list of dropdown options is currently open or closed was renamed to `open`. See [PR 103](https://github.com/janosh/svelte-multiselect/pull/103).
41+
- **v6.0.1**&nbsp; The prop `disabledTitle` which sets the title of the `<MultiSelect>` `<input>` node if in `disabled` mode was renamed to `disabledInputTitle`. See [PR 105](https://github.com/janosh/svelte-multiselect/pull/105).
42+
- **v6.0.1**&nbsp; The default margin of `1em 0` on the wrapper `div.multiselect` was removed. Instead, there is now a new CSS variable `--sms-margin`. Set it to `--sms-margin: 1em 0;` to restore the old appearance. See [PR 105](https://github.com/janosh/svelte-multiselect/pull/105).
43+
- **6.1.0**&nbsp; The `dispatch` events `focus` and `blur` were renamed to `open` and `close`, respectively. These actions refer to the dropdown list, i.e. `<MultiSelect on:open={(event) => console.log(event)}>` will trigger when the dropdown list opens. The focus and blur events are now regular DOM (not Svelte `dispatch`) events emitted by the `<input>` node. See [PR 120](https://github.com/janosh/svelte-multiselect/pull/120).
44+
- **v7.0.0**&nbsp; `selected` (as well `selectedLabels` and `selectedValues`) used to be arrays always. Now, if `maxSelect=1`, they will no longer be a length-1 array but simply a single a option (label/value respectively) or `null` if no option is selected. See [PR 123](https://github.com/janosh/svelte-multiselect/pull/123).
45+
- **v7.1.1**&nbsp; Prop `noOptionsMsg` was renamed to `noMatchingOptionsMsg`. See [PR 133](https://github.com/janosh/svelte-multiselect/pull/123).
46+
- **To be released**&nbsp; Props `selectedLabels` and `selectedValues` were removed. If you were using them, they were equivalent to assigning `bind:selected` to a local variable and then running `selectedLabels = selected.map(option => option.label)` and `selectedValues = selected.map(option => option.value)`.
4647

4748
## Installation
4849

@@ -316,18 +317,6 @@ import type { Option } from 'svelte-multiselect'
316317

317318
Array of currently selected options. Supports 2-way binding `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. If `maxSelect={1}`, selected will not be an array but a single `Option` or `null` if no options are selected.
318319

319-
1. ```ts
320-
selectedLabels: (string | number)[] | string | number | null = []
321-
```
322-
323-
Labels of currently selected options. Exposed just for convenience, equivalent to `selected.map(op => op.label)` when options are objects. If options are simple strings (or numbers), `selected === selectedLabels`. Supports binding but is read-only, i.e. since this value is reactive to `selected`, you cannot control `selected` by changing `bind:selectedLabels`. If `maxSelect={1}`, selectedLabels will not be an array but a single `string | number` or `null` if no options are selected.
324-
325-
1. ```ts
326-
selectedValues: unknown[] | unknown | null = []
327-
```
328-
329-
Values of currently selected options. Exposed just for convenience, equivalent to `selected.map(op => op.value)` when options are objects. If options are simple strings (or numbers), `selected === selectedValues`. Supports binding but is read-only, i.e. since this value is reactive to `selected`, you cannot control `selected` by changing `bind:selectedValues`. If `maxSelect={1}`, selectedLabels will not be an array but a single value or `null` if no options are selected.
330-
331320
1. ```ts
332321
sortSelected: boolean | ((op1: Option, op2: Option) => number) = false
333322
```

src/lib/MultiSelect.svelte

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,20 @@
5858
options
5959
?.filter((op) => (op as ObjectOption)?.preselected)
6060
.slice(0, maxSelect ?? undefined) ?? []
61-
export let selectedLabels: (string | number)[] | string | number | null = []
62-
export let selectedValues: unknown[] | unknown | null = []
6361
export let sortSelected: boolean | ((op1: Option, op2: Option) => number) = false
6462
export let ulOptionsClass: string = ``
6563
export let ulSelectedClass: string = ``
6664
6765
// get the label key from an option object or the option itself if it's a string or number
6866
const get_label = (op: Option) => (op instanceof Object ? op.label : op)
6967
70-
// fallback on label if option is object and value is undefined
71-
const get_value = (op: Option) => (op instanceof Object ? op.value ?? op.label : op)
72-
7368
// selected and _selected are identical except if maxSelect=1, selected will be the single item (or null)
7469
// in _selected which will always be an array for easier component internals. selected then solves
7570
// https://github.com/janosh/svelte-multiselect/issues/86
7671
let _selected = (selected ?? []) as Option[]
7772
$: selected = maxSelect === 1 ? _selected[0] ?? null : _selected
7873
7974
let wiggle = false // controls wiggle animation when user tries to exceed maxSelect
80-
$: _selectedLabels = _selected?.map(get_label) ?? []
81-
$: selectedLabels = maxSelect === 1 ? _selectedLabels[0] ?? null : _selectedLabels
82-
$: _selectedValues = _selected?.map(get_value) ?? []
83-
$: selectedValues = maxSelect === 1 ? _selectedValues[0] ?? null : _selectedValues
8475
8576
type $$Events = MultiSelectEvents // for type-safe event listening on this component
8677
@@ -112,7 +103,8 @@
112103
113104
// options matching the current search text
114105
$: matchingOptions = options.filter(
115-
(op) => filterFunc(op, searchText) && !_selectedLabels.includes(get_label(op)) // remove already selected options from dropdown list
106+
(op) =>
107+
filterFunc(op, searchText) && !_selected.map(get_label).includes(get_label(op)) // remove already selected options from dropdown list
116108
)
117109
// raise if matchingOptions[activeIndex] does not yield a value
118110
if (activeIndex !== null && !matchingOptions[activeIndex]) {
@@ -124,7 +116,7 @@
124116
// add an option to selected list
125117
function add(label: string | number, event: Event) {
126118
if (maxSelect && maxSelect > 1 && _selected.length >= maxSelect) wiggle = true
127-
if (!isNaN(Number(label)) && typeof _selectedLabels[0] === `number`)
119+
if (!isNaN(Number(label)) && typeof _selected.map(get_label)[0] === `number`)
128120
label = Number(label) // convert to number if possible
129121
130122
const is_duplicate = _selected.some((option) => duplicateFunc(option, label))
@@ -200,7 +192,7 @@
200192
function remove(label: string | number) {
201193
if (_selected.length === 0) return
202194
203-
_selected.splice(_selectedLabels.lastIndexOf(label), 1)
195+
_selected.splice(_selected.map(get_label).lastIndexOf(label), 1)
204196
_selected = _selected // Svelte rerender after in-place splice
205197
206198
const option =
@@ -249,7 +241,7 @@
249241
250242
if (activeOption) {
251243
const label = get_label(activeOption)
252-
selectedLabels?.includes(label) ? remove(label) : add(label, event)
244+
_selected.map(get_label).includes(label) ? remove(label) : add(label, event)
253245
searchText = ``
254246
} else if (allowUserOptions && searchText.length > 0) {
255247
// user entered text but no options match, so if allowUserOptions is truthy, we create new option
@@ -293,8 +285,8 @@
293285
}
294286
}
295287
// on backspace key: remove last selected option
296-
else if (event.key === `Backspace` && _selectedLabels.length > 0 && !searchText) {
297-
remove(_selectedLabels.at(-1) as string | number)
288+
else if (event.key === `Backspace` && _selected.length > 0 && !searchText) {
289+
remove(_selected.map(get_label).at(-1) as string | number)
298290
}
299291
}
300292
@@ -305,7 +297,7 @@
305297
searchText = ``
306298
}
307299
308-
$: is_selected = (label: string | number) => _selectedLabels.includes(label)
300+
$: is_selected = (label: string | number) => _selected.map(get_label).includes(label)
309301
310302
const if_enter_or_space = (handler: () => void) => (event: KeyboardEvent) => {
311303
if ([`Enter`, `Space`].includes(event.code)) {

0 commit comments

Comments
 (0)