1
0
Fork 1
mirror of https://github.com/hay-kot/homebox.git synced 2024-12-22 23:06:31 +00:00
homebox/frontend/components/Form/Multiselect.vue

79 lines
2.1 KiB
Vue
Raw Normal View History

2022-09-02 17:46:20 +00:00
<template>
<div ref="menu" class="form-control w-full">
2022-09-02 17:46:20 +00:00
<label class="label">
<span class="label-text">{{ label }}</span>
</label>
<div class="dropdown dropdown-top sm:dropdown-end">
<div tabindex="0" class="w-full min-h-[48px] flex gap-2 p-4 flex-wrap border border-gray-400 rounded-lg">
<span v-for="itm in value" :key="name != '' ? itm[name] : itm" class="badge">
{{ name != "" ? itm[name] : itm }}
</span>
2022-09-02 17:46:20 +00:00
</div>
<ul
tabindex="0"
2022-09-03 09:17:57 +00:00
class="dropdown-content mb-1 menu shadow border border-gray-400 rounded bg-base-100 w-full z-[9999] max-h-60 overflow-y-scroll scroll-bar"
2022-09-02 17:46:20 +00:00
>
<li
v-for="(obj, idx) in items"
:key="idx"
2022-09-02 17:46:20 +00:00
:class="{
bordered: selected[idx],
2022-09-02 17:46:20 +00:00
}"
>
<button type="button" @click="toggle(idx)">
{{ name != "" ? obj[name] : obj }}
2022-09-02 17:46:20 +00:00
</button>
</li>
</ul>
</div>
</div>
</template>
<script lang="ts" setup>
const emit = defineEmits(["update:modelValue"]);
2022-09-02 17:46:20 +00:00
const props = defineProps({
label: {
type: String,
default: "",
2022-09-02 17:46:20 +00:00
},
modelValue: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-09-02 17:46:20 +00:00
type: Array as () => any[],
default: null,
},
items: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-09-02 17:46:20 +00:00
type: Array as () => any[],
required: true,
},
name: {
type: String,
default: "name",
2022-09-02 17:46:20 +00:00
},
selectFirst: {
type: Boolean,
default: false,
},
});
const value = useVModel(props, "modelValue", emit);
2022-09-02 17:46:20 +00:00
const selected = computed<Record<number, boolean>>(() => {
const obj: Record<number, boolean> = {};
value.value.forEach(itm => {
const idx = props.items.findIndex(item => item[props.name] === itm.name);
obj[idx] = true;
});
return obj;
});
2022-09-02 17:46:20 +00:00
function toggle(index: number) {
2022-09-02 17:46:20 +00:00
const item = props.items[index];
if (selected.value[index]) {
value.value = value.value.filter(itm => itm.name !== item.name);
2022-09-02 17:46:20 +00:00
} else {
value.value = [...value.value, item];
2022-09-02 17:46:20 +00:00
}
}
</script>