diff --git a/packages/builder/src/components/common/MultiSelect.svelte b/packages/builder/src/components/common/MultiSelect.svelte index f000df7f41..f2fa530530 100644 --- a/packages/builder/src/components/common/MultiSelect.svelte +++ b/packages/builder/src/components/common/MultiSelect.svelte @@ -6,74 +6,57 @@ "M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" export let value = [] - export let readonly = false export let label let placeholder = "Type to search" - let input - let inputValue let options = [] - let activeOption let optionsVisible = false let selected = {} let first = true let slot onMount(() => { - slot.querySelectorAll("option").forEach(o => { - o.selected && !value.includes(o.value) && (value = [...value, o.value]) - options = [...options, { value: o.value, name: o.textContent }] - }) - value && - (selected = options.reduce( - (obj, op) => - value.includes(op.value) ? { ...obj, [op.value]: op } : obj, - {} - )) + const domOptions = Array.from(slot.querySelectorAll("option")) + options = domOptions.map(option => ({ + value: option.value, + name: option.textContent, + })) + if (value) { + options.forEach(option => { + if (value.includes(option.value)) { + selected[option.value] = option + } + }) + } first = false }) - $: if (!first) value = Object.values(selected).map(o => o.value) - $: filtered = options.filter(o => - inputValue ? o.name.toLowerCase().includes(inputValue.toLowerCase()) : o - ) - $: if ( - (activeOption && !filtered.includes(activeOption)) || - (!activeOption && inputValue) - ) - activeOption = filtered[0] + // Keep value up to date with selected options + $: { + if (!first) { + value = Object.values(selected).map(option => option.value) + } + } function add(token) { - if (!readonly) selected[token.value] = token + selected[token.value] = token } function remove(value) { - if (!readonly) { - const { [value]: val, ...rest } = selected - selected = rest - } + const { [value]: val, ...rest } = selected + selected = rest } function removeAll() { selected = [] - inputValue = "" } function showOptions(show) { optionsVisible = show - if (!show) { - activeOption = undefined - } else { - input.focus() - } } - function handleBlur() { - showOptions(false) - } - - function handleFocus() { - showOptions(true) + function handleClick() { + showOptions(!optionsVisible) } function handleOptionMousedown(e) { @@ -82,7 +65,6 @@ remove(value) } else { add(options.filter(option => option.value === value)[0]) - input.focus() } } @@ -91,75 +73,51 @@ {#if label} {/if} -