2022-10-24 04:54:39 +00:00
|
|
|
<template>
|
|
|
|
<div ref="menu" class="form-control w-full">
|
|
|
|
<label class="label">
|
|
|
|
<span class="label-text">{{ label }}</span>
|
|
|
|
</label>
|
|
|
|
<div class="dropdown dropdown-top sm:dropdown-end">
|
|
|
|
<div class="relative">
|
|
|
|
<input
|
2023-01-28 20:53:00 +00:00
|
|
|
v-model="internalSearch"
|
2022-10-24 04:54:39 +00:00
|
|
|
tabindex="0"
|
|
|
|
class="input w-full items-center flex flex-wrap border border-gray-400 rounded-lg"
|
|
|
|
@keyup.enter="selectFirst"
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
v-if="!!modelValue && Object.keys(modelValue).length !== 0"
|
|
|
|
style="transform: translateY(-50%)"
|
|
|
|
class="top-1/2 absolute right-2 btn btn-xs btn-circle no-animation"
|
|
|
|
@click="clear"
|
|
|
|
>
|
|
|
|
x
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<ul
|
|
|
|
tabindex="0"
|
|
|
|
style="display: inline"
|
|
|
|
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"
|
|
|
|
>
|
|
|
|
<li v-for="(obj, idx) in filtered" :key="idx">
|
2023-01-28 20:53:00 +00:00
|
|
|
<div type="button" @click="select(obj)">
|
|
|
|
<slot name="display" v-bind="{ item: obj }">
|
2023-02-18 06:41:01 +00:00
|
|
|
{{ extractor(obj, itemText) }}
|
2023-01-28 20:53:00 +00:00
|
|
|
</slot>
|
|
|
|
</div>
|
2022-10-24 04:54:39 +00:00
|
|
|
</li>
|
|
|
|
<li class="hidden first:flex">
|
|
|
|
<button disabled>
|
|
|
|
{{ noResultsText }}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
type ItemsObject = {
|
|
|
|
text?: string;
|
|
|
|
value?: string;
|
|
|
|
[key: string]: unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
label: string;
|
2023-01-28 20:53:00 +00:00
|
|
|
modelValue: string | ItemsObject | null | undefined;
|
2022-10-24 04:54:39 +00:00
|
|
|
items: ItemsObject[] | string[];
|
|
|
|
itemText?: keyof ItemsObject;
|
2023-01-28 20:53:00 +00:00
|
|
|
itemSearch?: keyof ItemsObject | null;
|
2022-10-24 04:54:39 +00:00
|
|
|
itemValue?: keyof ItemsObject;
|
|
|
|
search?: string;
|
|
|
|
noResultsText?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emit = defineEmits(["update:modelValue", "update:search"]);
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
label: "",
|
|
|
|
modelValue: "",
|
|
|
|
items: () => [],
|
|
|
|
itemText: "text",
|
|
|
|
search: "",
|
2023-01-28 20:53:00 +00:00
|
|
|
itemSearch: null,
|
|
|
|
itemValue: "value",
|
2022-10-24 04:54:39 +00:00
|
|
|
noResultsText: "No Results Found",
|
|
|
|
});
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
const searchKey = computed(() => props.itemSearch || props.itemText);
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
function clear() {
|
|
|
|
select(value.value);
|
|
|
|
}
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
const internalSearch = ref("");
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.search,
|
|
|
|
val => {
|
|
|
|
internalSearch.value = val;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => internalSearch.value,
|
|
|
|
val => {
|
|
|
|
emit("update:search", val);
|
|
|
|
}
|
|
|
|
);
|
2022-10-24 04:54:39 +00:00
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
function extractor(obj: string | ItemsObject, key: string | number): string {
|
|
|
|
if (typeof obj === "string") {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj[key] as string;
|
|
|
|
}
|
|
|
|
|
2022-10-24 04:54:39 +00:00
|
|
|
const value = useVModel(props, "modelValue", emit);
|
|
|
|
|
|
|
|
const usingObjects = computed(() => {
|
|
|
|
return props.items.length > 0 && typeof props.items[0] === "object";
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* isStrings is a type guard function to check if the items are an array of string
|
|
|
|
*/
|
|
|
|
function isStrings(_arr: string[] | ItemsObject[]): _arr is string[] {
|
|
|
|
return !usingObjects.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectFirst() {
|
|
|
|
if (filtered.value.length > 0) {
|
|
|
|
select(filtered.value[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(
|
|
|
|
value,
|
|
|
|
() => {
|
|
|
|
if (value.value) {
|
|
|
|
if (typeof value.value === "string") {
|
2023-01-28 20:53:00 +00:00
|
|
|
internalSearch.value = value.value;
|
2022-10-24 04:54:39 +00:00
|
|
|
} else {
|
2023-01-28 20:53:00 +00:00
|
|
|
internalSearch.value = value.value[searchKey.value] as string;
|
2022-10-24 04:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
immediate: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-01-28 20:53:00 +00:00
|
|
|
function select(obj: Props["modelValue"]) {
|
2022-10-24 04:54:39 +00:00
|
|
|
if (isStrings(props.items)) {
|
|
|
|
if (obj === value.value) {
|
|
|
|
value.value = "";
|
|
|
|
return;
|
|
|
|
}
|
2023-02-18 06:41:01 +00:00
|
|
|
// @ts-ignore
|
2022-10-24 04:54:39 +00:00
|
|
|
value.value = obj;
|
|
|
|
} else {
|
|
|
|
if (obj === value.value) {
|
|
|
|
value.value = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
// @ts-ignore
|
2022-10-24 04:54:39 +00:00
|
|
|
value.value = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const filtered = computed(() => {
|
2023-01-28 20:53:00 +00:00
|
|
|
if (!internalSearch.value || internalSearch.value === "") {
|
2022-10-24 04:54:39 +00:00
|
|
|
return props.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isStrings(props.items)) {
|
2023-01-28 20:53:00 +00:00
|
|
|
return props.items.filter(item => item.toLowerCase().includes(internalSearch.value.toLowerCase()));
|
2022-10-24 04:54:39 +00:00
|
|
|
} else {
|
|
|
|
return props.items.filter(item => {
|
2023-01-28 20:53:00 +00:00
|
|
|
if (searchKey.value && searchKey.value in item) {
|
|
|
|
return (item[searchKey.value] as string).toLowerCase().includes(internalSearch.value.toLowerCase());
|
2022-10-24 04:54:39 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|