From c0e2aa5c629d819c5c65ef2994c365283ca5a63a Mon Sep 17 00:00:00 2001 From: Tonya Date: Thu, 5 Sep 2024 23:52:26 +0000 Subject: [PATCH] Merge pull request #197 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add search to multi select and improve behaviour when multiple … --- frontend/components/Form/Multiselect.vue | 64 +++++++++++++++--------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/frontend/components/Form/Multiselect.vue b/frontend/components/Form/Multiselect.vue index e961f7f36..b9b58ad38 100644 --- a/frontend/components/Form/Multiselect.vue +++ b/frontend/components/Form/Multiselect.vue @@ -9,23 +9,28 @@ {{ name != "" ? itm[name] : itm }} - +
+ +
+ + @@ -49,6 +54,10 @@ type: String, default: "name", }, + uniqueField: { + type: String, + default: "id", + }, selectFirst: { type: Boolean, default: false, @@ -57,19 +66,26 @@ const value = useVModel(props, "modelValue", emit); - const selected = computed>(() => { - const obj: Record = {}; - value.value.forEach(itm => { - const idx = props.items.findIndex(item => item[props.name] === itm.name); - obj[idx] = true; + const search = ref(""); + + const filteredItems = computed(() => { + if (!search.value) { + return props.items; + } + + return props.items.filter(item => { + return item[props.name].toLowerCase().includes(search.value.toLowerCase()); }); - return obj; }); - function toggle(index: number) { - const item = props.items[index]; - if (selected.value[index]) { - value.value = value.value.filter(itm => itm.name !== item.name); + const selected = computed(() => { + return value.value.map(itm => itm[props.uniqueField]); + }); + + function toggle(uniqueField: string) { + const item = props.items.find(itm => itm[props.uniqueField] === uniqueField); + if (selected.value.includes(item[props.uniqueField])) { + value.value = value.value.filter(itm => itm[props.uniqueField] !== item[props.uniqueField]); } else { value.value = [...value.value, item]; }