98 lines
4.4 KiB
Vue
98 lines
4.4 KiB
Vue
<script setup>
|
||
import { ref, watch } from 'vue';
|
||
import { Search, X } from '@lucide/vue';
|
||
import { PersonAvatar } from '@inhaus/ui';
|
||
import { http } from '@inhaus/ui';
|
||
|
||
/**
|
||
* Personen suchen und auswählen – einzeln (Direktnachricht) oder mehrere (Gruppe).
|
||
* v-model: Liste der Personen [{ id, name, … }]
|
||
*/
|
||
const props = defineProps({
|
||
multiple: { type: Boolean, default: false },
|
||
exclude: { type: Array, default: () => [] },
|
||
placeholder: { type: String, default: 'Name oder E-Mail eingeben …' },
|
||
autofocus: { type: Boolean, default: false },
|
||
});
|
||
const emit = defineEmits(['pick']);
|
||
const selected = defineModel({ type: Array, default: () => [] });
|
||
|
||
const query = ref('');
|
||
const results = ref([]);
|
||
const active = ref(0);
|
||
const loading = ref(false);
|
||
let timer;
|
||
|
||
watch(query, (value) => {
|
||
clearTimeout(timer);
|
||
|
||
if (value.trim().length < 2) {
|
||
results.value = [];
|
||
|
||
return;
|
||
}
|
||
|
||
timer = setTimeout(async () => {
|
||
loading.value = true;
|
||
|
||
try {
|
||
const ids = [...props.exclude, ...selected.value.map((p) => p.id)];
|
||
results.value = (await http.get(`/personen/suche?q=${encodeURIComponent(value.trim())}`)).people.filter((p) => !ids.includes(p.id));
|
||
active.value = 0;
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}, 200);
|
||
});
|
||
|
||
function choose(person) {
|
||
if (!person) return;
|
||
|
||
emit('pick', person);
|
||
|
||
if (props.multiple) selected.value = [...selected.value, person];
|
||
query.value = '';
|
||
results.value = [];
|
||
}
|
||
|
||
const remove = (person) => (selected.value = selected.value.filter((p) => p.id !== person.id));
|
||
</script>
|
||
|
||
<template>
|
||
<div class="ih:grid ih:gap-2">
|
||
<div v-if="multiple && selected.length" class="ih:flex ih:flex-wrap ih:gap-1.5">
|
||
<span v-for="person in selected" :key="person.id" class="ih:inline-flex ih:items-center ih:gap-1.5 ih:rounded-full ih:border ih:bg-card ih:py-0.5 ih:pr-1 ih:pl-0.5 ih:text-[13px]">
|
||
<PersonAvatar :person="person" class="ih:size-6 ih:text-[10px]" />
|
||
{{ person.name }}
|
||
<button type="button" class="ih:rounded-full ih:p-0.5 ih:text-muted-foreground ih:hover:bg-muted ih:hover:text-foreground" :aria-label="`${person.name} entfernen`" @click="remove(person)"><X class="ih:size-3.5" /></button>
|
||
</span>
|
||
</div>
|
||
<div class="ih:relative">
|
||
<Search class="ih:pointer-events-none ih:absolute ih:top-1/2 ih:left-2.5 ih:size-4 ih:-translate-y-1/2 ih:text-muted-foreground" />
|
||
<input
|
||
v-model="query"
|
||
type="search"
|
||
autocomplete="off"
|
||
:autofocus="autofocus"
|
||
:placeholder="placeholder"
|
||
aria-label="Person suchen"
|
||
class="ih:h-10 ih:w-full ih:rounded-md ih:border ih:border-input ih:bg-card ih:pr-3 ih:pl-8 ih:text-sm ih:shadow-xs ih:focus-visible:ring-3 ih:focus-visible:ring-ring/30 ih:focus-visible:outline-none"
|
||
@keydown.down.prevent="active = Math.min(results.length - 1, active + 1)"
|
||
@keydown.up.prevent="active = Math.max(0, active - 1)"
|
||
@keydown.enter.prevent="choose(results[active])"
|
||
/>
|
||
</div>
|
||
<ul v-if="results.length" class="ih:max-h-64 ih:overflow-y-auto ih:rounded-md ih:border ih:bg-popover ih:p-1" role="listbox">
|
||
<li v-for="(person, i) in results" :key="person.id" role="option" :aria-selected="i === active">
|
||
<button type="button" class="ih:flex ih:w-full ih:items-center ih:gap-2.5 ih:rounded ih:px-2 ih:py-1.5 ih:text-left ih:text-sm" :class="i === active ? 'ih:bg-accent' : 'ih:hover:bg-accent/60'" @click="choose(person)" @mouseenter="active = i">
|
||
<PersonAvatar :person="person" class="ih:size-7 ih:text-[11px]" />
|
||
<span class="ih:min-w-0 ih:flex-1">
|
||
<span class="ih:block ih:truncate ih:font-medium">{{ person.name }}</span>
|
||
<span v-if="person.job_title || person.department" class="ih:block ih:truncate ih:text-xs ih:text-muted-foreground">{{ [person.job_title, person.department].filter(Boolean).join(' · ') }}</span>
|
||
</span>
|
||
</button>
|
||
</li>
|
||
</ul>
|
||
<p v-else-if="query.trim().length >= 2 && !loading" class="ih:px-1 ih:text-[13px] ih:text-muted-foreground">Niemand gefunden.</p>
|
||
</div>
|
||
</template>
|