2024-12-25 20:46:14 +01:00
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2025-03-28 01:16:24 +01:00
|
|
|
import { useCurrentElement } from "@vueuse/core";
|
|
|
|
|
import type { ListboxItemEmits, ListboxItemProps } from "reka-ui";
|
|
|
|
|
import { ListboxItem, useForwardPropsEmits, useId } from "reka-ui";
|
|
|
|
|
import {
|
|
|
|
|
type HTMLAttributes,
|
|
|
|
|
computed,
|
|
|
|
|
onMounted,
|
|
|
|
|
onUnmounted,
|
|
|
|
|
ref,
|
|
|
|
|
} from "vue";
|
|
|
|
|
import { useCommand, useCommandGroup } from ".";
|
2024-12-25 20:46:14 +01:00
|
|
|
|
|
|
|
|
const props = defineProps<
|
2025-03-28 01:16:24 +01:00
|
|
|
ListboxItemProps & { class?: HTMLAttributes["class"] }
|
2024-12-25 20:46:14 +01:00
|
|
|
>();
|
2025-03-28 01:16:24 +01:00
|
|
|
const emits = defineEmits<ListboxItemEmits>();
|
2024-12-25 20:46:14 +01:00
|
|
|
|
|
|
|
|
const delegatedProps = computed(() => {
|
|
|
|
|
const { class: _, ...delegated } = props;
|
|
|
|
|
|
|
|
|
|
return delegated;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const forwarded = useForwardPropsEmits(delegatedProps, emits);
|
2025-03-28 01:16:24 +01:00
|
|
|
|
|
|
|
|
const id = useId();
|
|
|
|
|
const { filterState, allItems, allGroups } = useCommand();
|
|
|
|
|
const groupContext = useCommandGroup();
|
|
|
|
|
|
|
|
|
|
const isRender = computed(() => {
|
|
|
|
|
if (filterState.search) {
|
|
|
|
|
const filteredCurrentItem = filterState.filtered.items.get(id);
|
|
|
|
|
// If the filtered items is undefined means not in the all times map yet
|
|
|
|
|
// Do the first render to add into the map
|
|
|
|
|
if (filteredCurrentItem === undefined) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check with filter
|
|
|
|
|
return filteredCurrentItem > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const itemRef = ref();
|
|
|
|
|
const currentElement = useCurrentElement(itemRef);
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
if (!(currentElement.value instanceof HTMLElement)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// textValue to perform filter
|
|
|
|
|
allItems.value.set(
|
|
|
|
|
id,
|
|
|
|
|
currentElement.value.textContent ?? props.value?.toString() ?? "",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const groupId = groupContext?.id;
|
|
|
|
|
if (groupId) {
|
|
|
|
|
if (allGroups.value.has(groupId)) {
|
|
|
|
|
allGroups.value.get(groupId)?.add(id);
|
|
|
|
|
} else {
|
|
|
|
|
allGroups.value.set(groupId, new Set([id]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
allItems.value.delete(id);
|
|
|
|
|
});
|
2024-12-25 20:46:14 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-04-10 13:55:56 +02:00
|
|
|
<ListboxItem
|
|
|
|
|
v-if="isRender"
|
|
|
|
|
v-bind="forwarded"
|
|
|
|
|
:id="id"
|
|
|
|
|
ref="itemRef"
|
|
|
|
|
data-slot="command-item"
|
|
|
|
|
:class="cn(`data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground [&_svg:not([class*='text-'])]:text-muted-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-3 text-sm outline-hidden select-none data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4`, props.class)"
|
|
|
|
|
@select="() => {
|
|
|
|
|
filterState.search = ''
|
|
|
|
|
}"
|
|
|
|
|
>
|
|
|
|
|
<slot />
|
|
|
|
|
</ListboxItem>
|
2024-12-25 20:46:14 +01:00
|
|
|
</template>
|