2023-02-10 02:47:41 +00:00
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<Combobox v-model="value">
|
|
|
|
<ComboboxLabel class="label">
|
|
|
|
<span class="label-text">{{ label }}</span>
|
|
|
|
</ComboboxLabel>
|
|
|
|
<div class="relative">
|
|
|
|
<ComboboxInput
|
|
|
|
:display-value="i => extractDisplay(i as SupportValues)"
|
|
|
|
class="w-full input input-bordered"
|
|
|
|
@change="search = $event.target.value"
|
|
|
|
/>
|
2023-10-06 21:32:49 +00:00
|
|
|
<button
|
|
|
|
v-if="!!value"
|
|
|
|
type="button"
|
|
|
|
class="absolute inset-y-0 right-6 flex items-center rounded-r-md px-2 focus:outline-none"
|
|
|
|
@click="clear"
|
|
|
|
>
|
2024-03-01 01:20:18 +00:00
|
|
|
<MdiClose class="w-5 h-5" />
|
2023-10-06 21:32:49 +00:00
|
|
|
</button>
|
2023-02-10 02:47:41 +00:00
|
|
|
<ComboboxButton class="absolute inset-y-0 right-0 flex items-center rounded-r-md px-2 focus:outline-none">
|
2024-03-01 01:20:18 +00:00
|
|
|
<MdiChevronDown class="w-5 h-5" />
|
2023-02-10 02:47:41 +00:00
|
|
|
</ComboboxButton>
|
|
|
|
<ComboboxOptions
|
|
|
|
v-if="computedItems.length > 0"
|
|
|
|
class="absolute dropdown-content z-10 mt-2 max-h-60 w-full overflow-auto rounded-md card bg-base-100 border border-gray-400"
|
|
|
|
>
|
|
|
|
<ComboboxOption
|
|
|
|
v-for="item in computedItems"
|
|
|
|
:key="item.id"
|
|
|
|
v-slot="{ active, selected }"
|
|
|
|
:value="item.value"
|
|
|
|
as="template"
|
|
|
|
>
|
|
|
|
<li
|
|
|
|
:class="[
|
|
|
|
'relative cursor-default select-none py-2 pl-3 pr-9 duration-75 ease-in-out transition-colors',
|
2023-02-15 17:52:13 +00:00
|
|
|
active ? 'bg-primary text-primary-content' : 'text-base-content',
|
2023-02-10 02:47:41 +00:00
|
|
|
]"
|
|
|
|
>
|
|
|
|
<slot name="display" v-bind="{ item: item, selected, active }">
|
|
|
|
<span :class="['block truncate', selected && 'font-semibold']">
|
|
|
|
{{ item.display }}
|
|
|
|
</span>
|
|
|
|
<span
|
|
|
|
v-if="selected"
|
|
|
|
:class="[
|
|
|
|
'absolute inset-y-0 right-0 flex text-primary items-center pr-4',
|
|
|
|
active ? 'text-primary-content' : 'bg-primary',
|
|
|
|
]"
|
|
|
|
>
|
2024-03-01 01:20:18 +00:00
|
|
|
<MdiCheck class="h-5 w-5" aria-hidden="true" />
|
2023-02-10 02:47:41 +00:00
|
|
|
</span>
|
|
|
|
</slot>
|
|
|
|
</li>
|
|
|
|
</ComboboxOption>
|
|
|
|
</ComboboxOptions>
|
|
|
|
</div>
|
|
|
|
</Combobox>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2024-02-29 19:45:05 +00:00
|
|
|
import lunr from "lunr";
|
2023-02-10 02:47:41 +00:00
|
|
|
import {
|
|
|
|
Combobox,
|
|
|
|
ComboboxInput,
|
|
|
|
ComboboxOptions,
|
|
|
|
ComboboxOption,
|
|
|
|
ComboboxButton,
|
|
|
|
ComboboxLabel,
|
|
|
|
} from "@headlessui/vue";
|
2024-03-01 01:20:18 +00:00
|
|
|
import MdiClose from "~icons/mdi/close";
|
|
|
|
import MdiChevronDown from "~icons/mdi/chevron-down";
|
|
|
|
import MdiCheck from "~icons/mdi/check";
|
2023-02-10 02:47:41 +00:00
|
|
|
|
|
|
|
type SupportValues = string | { [key: string]: any };
|
|
|
|
|
|
|
|
type ComboItem = {
|
|
|
|
display: string;
|
|
|
|
value: SupportValues;
|
|
|
|
id: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
label: string;
|
|
|
|
modelValue: SupportValues | null | undefined;
|
|
|
|
items: string[] | object[];
|
|
|
|
display?: string;
|
|
|
|
multiple?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const emit = defineEmits(["update:modelValue", "update:search"]);
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
|
label: "",
|
|
|
|
modelValue: "",
|
|
|
|
display: "text",
|
|
|
|
multiple: false,
|
|
|
|
});
|
|
|
|
|
2023-10-06 21:32:49 +00:00
|
|
|
function clear() {
|
|
|
|
emit("update:modelValue", null);
|
|
|
|
}
|
|
|
|
|
2023-02-10 02:47:41 +00:00
|
|
|
const search = ref("");
|
|
|
|
const value = useVModel(props, "modelValue", emit);
|
|
|
|
|
|
|
|
function extractDisplay(item?: SupportValues): string {
|
|
|
|
if (!item) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof item === "string") {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.display in item) {
|
|
|
|
return item[props.display] as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try these options as well
|
|
|
|
const fallback = ["name", "title", "display", "value"];
|
|
|
|
for (let i = 0; i < fallback.length; i++) {
|
|
|
|
const key = fallback[i];
|
|
|
|
if (key in item) {
|
|
|
|
return item[key] as string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
function lunrFactory() {
|
|
|
|
return lunr(function () {
|
|
|
|
this.ref("id");
|
|
|
|
this.field("display");
|
2023-02-10 02:47:41 +00:00
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
for (let i = 0; i < props.items.length; i++) {
|
|
|
|
const item = props.items[i];
|
|
|
|
const display = extractDisplay(item);
|
|
|
|
this.add({ id: i, display });
|
2023-02-10 02:47:41 +00:00
|
|
|
}
|
2024-02-29 19:45:05 +00:00
|
|
|
});
|
|
|
|
}
|
2023-02-10 02:47:41 +00:00
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
const index = ref<ReturnType<typeof lunrFactory>>(lunrFactory());
|
2023-02-10 02:47:41 +00:00
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
watchEffect(() => {
|
|
|
|
if (props.items) {
|
|
|
|
index.value = lunrFactory();
|
|
|
|
}
|
|
|
|
});
|
2023-02-10 02:47:41 +00:00
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
const computedItems = computed<ComboItem[]>(() => {
|
|
|
|
const list: ComboItem[] = [];
|
|
|
|
|
|
|
|
const matches = index.value.search("*" + search.value + "*");
|
2023-02-10 02:47:41 +00:00
|
|
|
|
2024-02-29 19:45:05 +00:00
|
|
|
for (let i = 0; i < matches.length; i++) {
|
|
|
|
const match = matches[i];
|
|
|
|
const item = props.items[parseInt(match.ref)];
|
|
|
|
const display = extractDisplay(item);
|
|
|
|
list.push({ id: i, display, value: item });
|
2023-02-10 02:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
});
|
|
|
|
</script>
|