|
3 | 3 |
|
4 | 4 | export let config = {};
|
5 | 5 |
|
6 |
| - const { Item, List, Selection, Multi, VirtualList, debounce } = config; |
| 6 | + const { Item, List, Selection, Multi, VirtualList, debounce, filter } = config; |
7 | 7 |
|
8 | 8 | const dispatch = createEventDispatcher();
|
9 | 9 |
|
|
83 | 83 | export let listClass = 'list';
|
84 | 84 | export let itemClass = 'item';
|
85 | 85 |
|
86 |
| - function filterMethod(args) { |
87 |
| - if (args.loadOptions && args.filterText.length > 0) return; |
88 |
| - if (!args.items) return []; |
89 |
| -
|
90 |
| - if (args.items && args.items.length > 0 && typeof args.items[0] !== 'object') { |
91 |
| - args.items = convertStringItemsToObjects(args.items); |
92 |
| - } |
93 |
| -
|
94 |
| - let filterResults = args.items.filter((item) => { |
95 |
| - let matchesFilter = itemFilter(getOptionLabel(item, args.filterText), args.filterText, item); |
96 |
| -
|
97 |
| - if (matchesFilter && args.isMulti && args.value && Array.isArray(args.value)) { |
98 |
| - matchesFilter = !args.value.some((x) => { |
99 |
| - return x[args.optionIdentifier] === item[args.optionIdentifier]; |
100 |
| - }); |
101 |
| - } |
102 |
| -
|
103 |
| - return matchesFilter; |
104 |
| - }); |
105 |
| -
|
106 |
| - if (args.groupBy) { |
107 |
| - filterResults = filterGroupedItems(filterResults); |
108 |
| - } |
109 |
| -
|
110 |
| - if (args.isCreatable) { |
111 |
| - filterResults = addCreatableItem(filterResults, args.filterText); |
112 |
| - } |
113 |
| -
|
114 |
| - return filterResults; |
115 |
| - } |
116 |
| -
|
117 | 86 | function addCreatableItem(_items, _filterText) {
|
118 | 87 | if (_filterText.length === 0) return _items;
|
119 | 88 | const itemToCreate = createItem(_filterText);
|
|
122 | 91 | return [..._items, itemToCreate];
|
123 | 92 | }
|
124 | 93 |
|
125 |
| - $: filteredItems = filterMethod({ |
| 94 | + $: filteredItems = filter({ |
126 | 95 | loadOptions,
|
127 | 96 | filterText,
|
128 | 97 | items,
|
129 |
| - value, |
130 | 98 | isMulti,
|
| 99 | + value, |
131 | 100 | optionIdentifier,
|
132 | 101 | groupBy,
|
133 | 102 | isCreatable,
|
| 103 | + itemFilter, |
| 104 | + convertStringItemsToObjects, |
| 105 | + filterGroupedItems, |
| 106 | + addCreatableItem, |
| 107 | + getOptionLabel |
134 | 108 | });
|
135 | 109 |
|
136 | 110 | let containerClasses = 'select-container';
|
|
392 | 366 |
|
393 | 367 | function updateValueDisplay(items) {
|
394 | 368 | if (!items || items.length === 0 || items.some((item) => typeof item !== 'object')) return;
|
395 |
| - if (!value || (isMulti ? value.some((selection) => !selection || !selection[optionIdentifier]) : !value[optionIdentifier])) return; |
| 369 | + if ( |
| 370 | + !value || |
| 371 | + (isMulti ? value.some((selection) => !selection || !selection[optionIdentifier]) : !value[optionIdentifier]) |
| 372 | + ) |
| 373 | + return; |
396 | 374 |
|
397 | 375 | if (Array.isArray(value)) {
|
398 | 376 | value = value.map((selection) => findItem(selection) || selection);
|
|
602 | 580 | $: ariaContext = handleAriaContent(filteredItems, hoverItemIndex, isFocused, listOpen);
|
603 | 581 | </script>
|
604 | 582 |
|
605 |
| -<div class={containerClasses} class:error={hasError} class:multi={isMulti} class:disabled={isDisabled} class:focused={isFocused} style={containerStyles} on:click={handleClick} bind:this={container}> |
| 583 | +<div |
| 584 | + class={containerClasses} |
| 585 | + class:error={hasError} |
| 586 | + class:multi={isMulti} |
| 587 | + class:disabled={isDisabled} |
| 588 | + class:focused={isFocused} |
| 589 | + style={containerStyles} |
| 590 | + on:click={handleClick} |
| 591 | + bind:this={container}> |
606 | 592 | <span aria-live="polite" aria-atomic="false" aria-relevant="additions text" class="a11yText">
|
607 | 593 | {#if isFocused}
|
608 | 594 | <span id="aria-selection">{ariaSelection}</span>
|
|
617 | 603 | {/if}
|
618 | 604 |
|
619 | 605 | {#if showMultiSelect}
|
620 |
| - <svelte:component this={Multi} {value} {getSelectionLabel} {activeValue} {isDisabled} {multiFullItemClearable} on:multiItemClear={handleMultiItemClear} on:focus={handleFocus} /> |
| 606 | + <svelte:component |
| 607 | + this={Multi} |
| 608 | + {value} |
| 609 | + {getSelectionLabel} |
| 610 | + {activeValue} |
| 611 | + {isDisabled} |
| 612 | + {multiFullItemClearable} |
| 613 | + on:multiItemClear={handleMultiItemClear} |
| 614 | + on:focus={handleFocus} /> |
621 | 615 | {/if}
|
622 | 616 |
|
623 | 617 | <input
|
|
657 | 651 | </div>
|
658 | 652 |
|
659 | 653 | {#if listOpen}
|
660 |
| - <svelte:component this={List} {...listProps} bind:hoverItemIndex on:itemSelected={itemSelected} on:itemCreated={itemCreated} on:closeList={closeList} /> |
| 654 | + <svelte:component |
| 655 | + this={List} |
| 656 | + {...listProps} |
| 657 | + bind:hoverItemIndex |
| 658 | + on:itemSelected={itemSelected} |
| 659 | + on:itemCreated={itemCreated} |
| 660 | + on:closeList={closeList} /> |
661 | 661 | {/if}
|
662 | 662 |
|
663 | 663 | {#if !isMulti || (isMulti && !showMultiSelect)}
|
|
0 commit comments