Loheide-eu-mitarbeiterplatt.../resources/js/components/TreeSelect.vue
Friederich Loheide 05b4b79cb3
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m8s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m6s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m23s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m40s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m8s
Titelbild im Profil, durchsuchbare Baum-Auswahl im IT-Ticket (0.20.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 05:20:19 +00:00

220 lines
9.4 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
import { computed, nextTick, ref, watch } from 'vue';
import { Check, ChevronsUpDown, Search, X } from '@lucide/vue';
import { PopoverAnchor, PopoverContent, PopoverPortal, PopoverRoot } from 'reka-ui';
/**
* Durchsuchbare Auswahl für Bäume (z. B. Kategorien, Standorte).
* options in Baum-Reihenfolge: [{ value, name, depth, path }] – depth 0 = oberste Ebene.
* Beim Suchen bleiben die übergeordneten Einträge der Treffer als Orientierung sichtbar.
*/
const props = defineProps({
options: { type: Array, required: true },
placeholder: { type: String, default: '– bitte wählen –' },
searchPlaceholder: { type: String, default: 'Suchen …' },
disabled: { type: Boolean, default: false },
clearable: { type: Boolean, default: false },
id: { type: String, default: null },
});
const model = defineModel({ type: [String, Number, null], default: null });
const open = ref(false);
const query = ref('');
const active = ref(0);
const list = ref(null);
const input = ref(null);
const trigger = ref(null);
// Klick auf das Feld selbst schließt über @click – nicht zusätzlich als „Klick außerhalb“
function ignoreTrigger(event) {
const target = event.detail?.originalEvent?.target ?? event.target;
if (trigger.value?.contains(target)) event.preventDefault();
}
const normalize = (text) => String(text ?? '').toLocaleLowerCase('de-DE').normalize('NFD').replace(/[̀-ͯ]/g, '');
// Eltern je Eintrag (Baum-Reihenfolge: nächster Vorgänger eine Ebene höher)
const items = computed(() => {
const stack = [];
return props.options.map((option, index) => {
const depth = option.depth ?? 0;
stack.length = depth;
const parent = depth > 0 ? stack[depth - 1] ?? null : null;
stack[depth] = index;
const next = props.options[index + 1];
return { ...option, name: option.name ?? String(option.label ?? '').replace(/^[\s └]+/, ''), depth, index, parent, hasChildren: (next?.depth ?? 0) > depth };
});
});
const selected = computed(() => items.value.find((item) => String(item.value) === String(model.value)) ?? null);
const visible = computed(() => {
const term = normalize(query.value.trim());
if (!term) return items.value.map((item) => ({ ...item, match: true }));
const keep = new Set();
const matches = new Set();
for (const item of items.value) {
if (!normalize(item.name).includes(term) && !normalize(item.path).includes(term)) continue;
matches.add(item.index);
for (let parent = item.parent; parent !== null && !keep.has(parent); parent = items.value[parent].parent) keep.add(parent);
keep.add(item.index);
}
return items.value.filter((item) => keep.has(item.index)).map((item) => ({ ...item, match: matches.has(item.index) }));
});
// Treffer hervorheben
function parts(name) {
const term = normalize(query.value.trim());
const at = term ? normalize(name).indexOf(term) : -1;
if (at < 0) return [{ text: name }];
return [{ text: name.slice(0, at) }, { text: name.slice(at, at + term.length), mark: true }, { text: name.slice(at + term.length) }];
}
watch(open, (value) => {
if (!value) return;
query.value = '';
nextTick(() => {
const current = visible.value.findIndex((item) => String(item.value) === String(model.value));
active.value = Math.max(0, current);
scrollToActive('center');
input.value?.focus();
});
});
watch(query, () => {
const first = visible.value.findIndex((item) => item.match);
active.value = Math.max(0, first);
nextTick(() => scrollToActive());
});
function scrollToActive(block = 'nearest') {
list.value?.querySelector(`[data-index="${active.value}"]`)?.scrollIntoView({ block });
}
function move(step) {
const count = visible.value.length;
if (!count) return;
active.value = (active.value + step + count) % count;
nextTick(() => scrollToActive());
}
function choose(item) {
if (!item) return;
model.value = item.value;
open.value = false;
}
function clear() {
model.value = null;
}
</script>
<template>
<PopoverRoot v-model:open="open">
<PopoverAnchor as-child>
<div class="relative">
<!-- Eigener Knopf statt PopoverTrigger: der würde die id überschreiben (Label-Bezug) -->
<button
:id="id"
ref="trigger"
type="button"
:disabled="disabled"
role="combobox"
aria-haspopup="listbox"
:aria-expanded="open"
@click="open = !open"
@keydown.down.prevent="open = true"
class="flex h-9 w-full items-center gap-2 rounded-md border border-input bg-card pr-8 pl-3 text-left text-sm shadow-xs focus-visible:border-brand-400 focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:outline-none disabled:opacity-60 aria-invalid:border-destructive"
v-bind="$attrs"
>
<span v-if="selected" class="min-w-0 flex-1 truncate">
<span v-if="selected.path && selected.path !== selected.name" class="text-muted-foreground">{{ selected.path.slice(0, selected.path.length - selected.name.length) }}</span>{{ selected.name }}
</span>
<span v-else class="min-w-0 flex-1 truncate text-muted-foreground">{{ placeholder }}</span>
</button>
<button
v-if="clearable && selected && !disabled"
type="button"
class="absolute top-1/2 right-7 -translate-y-1/2 rounded-sm p-0.5 text-muted-foreground hover:text-foreground"
aria-label="Auswahl entfernen"
@click="clear"
>
<X class="size-3.5" />
</button>
<ChevronsUpDown class="pointer-events-none absolute top-1/2 right-2.5 size-4 -translate-y-1/2 text-muted-foreground" />
</div>
</PopoverAnchor>
<PopoverPortal>
<PopoverContent
align="start"
:side-offset="4"
class="z-50 w-(--reka-popover-trigger-width) min-w-64 max-w-[calc(100vw-2rem)] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md outline-hidden"
@open-auto-focus.prevent
@interact-outside="ignoreTrigger"
>
<div class="flex items-center gap-2 border-b px-3">
<Search class="size-4 shrink-0 text-muted-foreground" />
<input
ref="input"
v-model="query"
type="search"
:placeholder="searchPlaceholder"
class="h-10 w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:hidden"
aria-label="Einträge durchsuchen"
autocomplete="off"
@keydown.down.prevent="move(1)"
@keydown.up.prevent="move(-1)"
@keydown.enter.prevent="choose(visible[active])"
/>
</div>
<ul ref="list" role="listbox" class="max-h-72 overflow-y-auto overscroll-contain p-1">
<li
v-for="(item, position) in visible"
:key="item.value"
role="option"
:aria-selected="String(item.value) === String(model)"
:data-index="position"
class="relative flex cursor-pointer items-center gap-2 rounded-sm py-1.5 pr-2 text-sm select-none"
:class="[position === active ? 'bg-accent text-accent-foreground' : '', item.match ? '' : 'text-muted-foreground']"
:style="{ paddingLeft: `${0.5 + item.depth * 1.25}rem` }"
@mousemove="active = position"
@click="choose(item)"
>
<!-- Baumlinien -->
<span
v-for="level in item.depth"
:key="level"
aria-hidden="true"
class="absolute inset-y-0 border-l border-border"
:style="{ left: `${0.5 + (level - 1) * 1.25 + 0.3}rem` }"
/>
<span v-if="item.depth" aria-hidden="true" class="absolute top-1/2 w-2 border-t border-border" :style="{ left: `${0.5 + (item.depth - 1) * 1.25 + 0.3}rem` }" />
<span class="min-w-0 flex-1 break-words" :class="item.hasChildren ? 'font-medium' : ''">
<template v-for="(part, i) in parts(item.name)" :key="i"><mark v-if="part.mark" class="rounded-[2px] bg-brand/15 text-inherit">{{ part.text }}</mark><template v-else>{{ part.text }}</template></template>
</span>
<Check v-if="String(item.value) === String(model)" class="size-4 shrink-0 text-brand-text" />
</li>
<li v-if="!visible.length" class="px-3 py-6 text-center text-sm text-muted-foreground">Keine Treffer für „{{ query }}“</li>
</ul>
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>