2022-09-02 17:46:20 +00:00
|
|
|
<template>
|
|
|
|
<div class="form-control w-full">
|
|
|
|
<label class="label">
|
|
|
|
<span class="label-text">{{ label }}</span>
|
|
|
|
</label>
|
2022-09-12 22:47:27 +00:00
|
|
|
<select v-model="selectedIdx" class="select select-bordered">
|
2022-09-02 17:46:20 +00:00
|
|
|
<option disabled selected>Pick one</option>
|
2022-09-12 22:47:27 +00:00
|
|
|
<option v-for="(obj, idx) in items" :key="name != '' ? obj[name] : obj" :value="idx">
|
2022-09-09 22:46:53 +00:00
|
|
|
{{ name != "" ? obj[name] : obj }}
|
2022-09-02 17:46:20 +00:00
|
|
|
</option>
|
|
|
|
</select>
|
|
|
|
<!-- <label class="label">
|
|
|
|
<span class="label-text-alt">Alt label</span>
|
|
|
|
<span class="label-text-alt">Alt label</span>
|
|
|
|
</label> -->
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2022-09-09 22:46:53 +00:00
|
|
|
const emit = defineEmits(["update:modelValue"]);
|
2022-09-02 17:46:20 +00:00
|
|
|
const props = defineProps({
|
|
|
|
label: {
|
|
|
|
type: String,
|
2022-09-09 22:46:53 +00:00
|
|
|
default: "",
|
2022-09-02 17:46:20 +00:00
|
|
|
},
|
|
|
|
modelValue: {
|
2022-09-12 22:47:27 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-10-15 20:15:55 +00:00
|
|
|
type: [Object, String] as any,
|
2022-09-02 17:46:20 +00:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
items: {
|
2022-09-12 22:47:27 +00:00
|
|
|
// 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,
|
2022-09-09 22:46:53 +00:00
|
|
|
default: "name",
|
2022-09-02 17:46:20 +00:00
|
|
|
},
|
2022-09-24 19:33:38 +00:00
|
|
|
value: {
|
|
|
|
type: String,
|
|
|
|
default: null,
|
|
|
|
required: false,
|
|
|
|
},
|
2022-09-02 17:46:20 +00:00
|
|
|
});
|
|
|
|
|
2022-10-14 00:45:18 +00:00
|
|
|
const selectedIdx = ref(-1);
|
|
|
|
const internalSelected = useVModel(props, "modelValue", emit);
|
2022-09-24 19:33:38 +00:00
|
|
|
|
2022-10-14 00:45:18 +00:00
|
|
|
watch(selectedIdx, newVal => {
|
|
|
|
internalSelected.value = props.items[newVal];
|
|
|
|
});
|
2022-09-24 19:33:38 +00:00
|
|
|
|
2022-10-14 00:45:18 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
function compare(a: any, b: any): boolean {
|
2022-10-15 20:15:55 +00:00
|
|
|
if (props.value) {
|
2022-10-14 00:45:18 +00:00
|
|
|
return a[props.value] === b[props.value];
|
|
|
|
}
|
2022-10-15 20:15:55 +00:00
|
|
|
|
|
|
|
if (a === b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!a || !b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(a) === JSON.stringify(b);
|
2022-09-24 19:33:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
watch(
|
2022-10-14 00:45:18 +00:00
|
|
|
internalSelected,
|
2022-09-12 22:47:27 +00:00
|
|
|
() => {
|
2022-10-14 00:45:18 +00:00
|
|
|
const idx = props.items.findIndex(item => compare(item, internalSelected.value));
|
|
|
|
selectedIdx.value = idx;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
immediate: true,
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
);
|
2022-09-02 17:46:20 +00:00
|
|
|
</script>
|