|
36 | 36 | export let loading: boolean = false |
37 | 37 | export let matchingOptions: Option[] = [] |
38 | 38 | export let maxSelect: number | null = null // null means any number of options are selectable |
39 | | - export let maxSelectMsg: ((current: number, max: number) => string) | null = null |
| 39 | + export let maxSelectMsg: ((current: number, max: number) => string) | null = ( |
| 40 | + current: number, |
| 41 | + max: number |
| 42 | + ) => (max > 1 ? `${current}/${max}` : ``) |
40 | 43 | export let name: string | null = null |
41 | 44 | export let noMatchingOptionsMsg: string = `No matching options` |
42 | 45 | export let open: boolean = false |
|
49 | 52 | export let removeAllTitle: string = `Remove all` |
50 | 53 | export let removeBtnTitle: string = `Remove` |
51 | 54 | export let required: boolean = false |
| 55 | + export let resetFilterOnAdd: boolean = true |
52 | 56 | export let searchText: string = `` |
53 | 57 | export let selected: Option[] | Option | null = |
54 | 58 | options |
|
106 | 110 | let add_option_msg_is_active: boolean = false // controls active state of <li>{addOptionMsg}</li> |
107 | 111 | let window_width: number |
108 | 112 |
|
109 | | - // formValue binds to input.form-control to prevent form submission if required |
110 | | - // prop is true and no options are selected |
111 | | - $: form_value = _selectedValues.join(`,`) |
112 | | - $: if (form_value) invalid = false // reset error status whenever component state changes |
113 | | -
|
114 | 113 | // options matching the current search text |
115 | 114 | $: matchingOptions = options.filter( |
116 | 115 | (op) => filterFunc(op, searchText) && !_selectedLabels.includes(get_label(op)) // remove already selected options from dropdown list |
|
161 | 160 | if (option === undefined) { |
162 | 161 | throw `Run time error, option with label ${label} not found in options list` |
163 | 162 | } |
164 | | - searchText = `` // reset search string on selection |
| 163 | + if (resetFilterOnAdd) searchText = `` // reset search string on selection |
165 | 164 | if ([``, undefined, null].includes(option)) { |
166 | 165 | console.error( |
167 | 166 | `MultiSelect: encountered missing option with label ${label} (or option is poorly labeled)` |
|
192 | 191 | } |
193 | 192 | dispatch(`add`, { option }) |
194 | 193 | dispatch(`change`, { option, type: `add` }) |
| 194 | +
|
| 195 | + invalid = false // reset error status whenever new items are selected |
195 | 196 | } |
196 | 197 | } |
197 | 198 |
|
|
215 | 216 |
|
216 | 217 | dispatch(`remove`, { option }) |
217 | 218 | dispatch(`change`, { option, type: `remove` }) |
| 219 | + invalid = false // reset error status whenever items are removed |
218 | 220 | } |
219 | 221 |
|
220 | 222 | function open_dropdown(event: Event) { |
|
338 | 340 | title={disabled ? disabledInputTitle : null} |
339 | 341 | aria-disabled={disabled ? `true` : null} |
340 | 342 | > |
341 | | - <!-- formValue binds to input.form-control to prevent form submission if required prop is true and no options are selected --> |
| 343 | + <!-- bind:value={_selected} prevents form submission if required prop is true and no options are selected --> |
342 | 344 | <input |
343 | 345 | {required} |
344 | | - bind:value={form_value} |
| 346 | + bind:value={_selected} |
345 | 347 | tabindex="-1" |
346 | 348 | aria-hidden="true" |
347 | 349 | aria-label="ignore this, used only to prevent form submission if select is required but empty" |
|
419 | 421 | {#if maxSelect && (maxSelect > 1 || maxSelectMsg)} |
420 | 422 | <Wiggle bind:wiggle angle={20}> |
421 | 423 | <span style="padding: 0 3pt;"> |
422 | | - {maxSelectMsg?.(_selected.length, maxSelect) ?? |
423 | | - (maxSelect > 1 ? `${_selected.length}/${maxSelect}` : ``)} |
| 424 | + {maxSelectMsg?.(_selected.length, maxSelect)} |
424 | 425 | </span> |
425 | 426 | </Wiggle> |
426 | 427 | {/if} |
|
454 | 455 | <li |
455 | 456 | on:mousedown|stopPropagation |
456 | 457 | on:mouseup|stopPropagation={(event) => { |
457 | | - if (!disabled) is_selected(label) ? remove(label) : add(label, event) |
| 458 | + if (!disabled) add(label, event) |
458 | 459 | }} |
459 | 460 | title={disabled |
460 | 461 | ? disabledTitle |
|
0 commit comments