diff --git a/packages/builder/src/components/common/MultiSelect.svelte b/packages/builder/src/components/common/MultiSelect.svelte index 641ca6beaf..f000df7f41 100644 --- a/packages/builder/src/components/common/MultiSelect.svelte +++ b/packages/builder/src/components/common/MultiSelect.svelte @@ -2,22 +2,22 @@ import { onMount } from "svelte" import { fly } from "svelte/transition" import { Label } from "@budibase/bbui" + const xPath = + "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 placeholder = "" export let label - let input, - inputValue, - options = [], - activeOption, - showOptions = false, - selected = {}, - first = true, - slot - const iconClearPath = - "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" + 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 => { @@ -31,7 +31,6 @@ {} )) first = false - console.log(options) }) $: if (!first) value = Object.values(selected).map(o => o.value) @@ -55,61 +54,34 @@ } } - function optionsVisibility(show) { - if (readonly) return - if (typeof show === "boolean") { - showOptions = show - show && input.focus() - } else { - showOptions = !showOptions - } - if (!showOptions) { + function removeAll() { + selected = [] + inputValue = "" + } + + function showOptions(show) { + optionsVisible = show + if (!show) { activeOption = undefined - } - } - - function handleKeyup(e) { - if (e.keyCode === 13) { - Object.keys(selected).includes(activeOption.value) - ? remove(activeOption.value) - : add(activeOption) - inputValue = "" - } - if ([38, 40].includes(e.keyCode)) { - // up and down arrows - const increment = e.keyCode === 38 ? -1 : 1 - const calcIndex = filtered.indexOf(activeOption) + increment - activeOption = - calcIndex < 0 - ? filtered[filtered.length - 1] - : calcIndex === filtered.length - ? filtered[0] - : filtered[calcIndex] - } - } - - function handleBlur(e) { - optionsVisibility(false) - } - - function handleTokenClick(e) { - if (e.target.closest(".token-remove")) { - e.stopPropagation() - remove(e.target.closest(".token").dataset.id) - } else if (e.target.closest(".remove-all")) { - selected = [] - inputValue = "" } else { - optionsVisibility(true) + input.focus() } } + function handleBlur() { + showOptions(false) + } + + function handleFocus() { + showOptions(true) + } + function handleOptionMousedown(e) { const value = e.target.dataset.value if (selected[value]) { remove(value) } else { - add(options.filter(o => o.value === value)[0]) + add(options.filter(option => option.value === value)[0]) input.focus() } } @@ -120,63 +92,61 @@ {/if}