adrema/resources/js/components/form/Multipleselect.vue

67 lines
2.2 KiB
Vue
Raw Normal View History

2023-06-14 16:20:18 +02:00
<template>
2024-06-28 14:26:12 +02:00
<label class="flex flex-col group" :for="id" :class="sizeClass(size)">
<f-label v-if="label" :required="false" :value="label"></f-label>
<div class="relative flex-none flex">
<div
:class="[fieldHeight, fieldAppearance, selectAppearance]"
class="form-select flex items-center w-full"
@click="visible = !visible"
v-text="`${modelValue.length} Einträge ausgewählt`"
></div>
2024-06-28 15:50:40 +02:00
<div v-show="visible" class="absolute w-[max-content] z-30 max-h-[25rem] overflow-auto shadow-lg bg-gray-600 border border-gray-500 rounded-lg p-2 top-7 space-y-1">
2023-07-27 14:00:27 +02:00
<div v-for="(option, index) in parsedOptions" :key="index" class="flex items-center space-x-2">
2024-06-28 14:26:12 +02:00
<f-switch :id="`${id}-${index}`" size="sm" :model-value="modelValue.includes(option.id)" :value="option.id" @update:modelValue="trigger(option, $event)"></f-switch>
2023-06-14 16:20:18 +02:00
<div class="text-sm text-gray-200" v-text="option.name"></div>
</div>
</div>
</div>
</label>
</template>
2024-06-28 14:26:12 +02:00
<script setup>
2023-06-14 16:20:18 +02:00
import map from 'lodash/map';
2024-06-28 14:26:12 +02:00
import {ref, computed} from 'vue';
import useFieldSize from '../../composables/useFieldSize';
const {fieldHeight, fieldAppearance, paddingX, sizeClass, selectAppearance} = useFieldSize();
2023-06-14 16:20:18 +02:00
2024-06-28 14:26:12 +02:00
const emit = defineEmits(['update:modelValue']);
const props = defineProps({
id: {
type: String,
required: true,
2023-06-14 16:20:18 +02:00
},
2024-06-28 14:26:12 +02:00
size: {
type: String,
default: () => 'base',
2023-07-27 14:00:27 +02:00
},
2024-06-28 14:26:12 +02:00
modelValue: {
validator: (v) => Array.isArray(v),
required: true,
2023-06-14 16:20:18 +02:00
},
2024-06-28 14:26:12 +02:00
label: {
type: String,
default: () => '',
2023-06-14 16:20:18 +02:00
},
2024-06-28 14:26:12 +02:00
options: {
validator: (v) => Array.isArray(v),
default: () => [],
},
});
const visible = ref(false);
const parsedOptions = computed(() =>
Array.isArray(props.options)
? props.options
: map(props.options, (value, key) => {
return {name: value, id: key};
})
);
function trigger(option, v) {
var value = JSON.parse(JSON.stringify(props.modelValue));
2023-06-14 16:20:18 +02:00
2024-06-28 14:26:12 +02:00
emit('update:modelValue', value.includes(option.id) ? value.filter((cv) => cv !== option.id) : [...value, option.id]);
2023-06-14 16:20:18 +02:00
}
2024-06-28 14:26:12 +02:00
</script>