import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Search, X } from 'lucide-react'; import type { User } from '../api/types'; export function userLabel(u: User): string { return [u.firstName, u.lastName].filter(Boolean).join(' ') || u.email; } /** Przeszukiwalny wybór użytkownika (listę pobiera strona) — wzór: GymPicker z członkostw. */ export function UserPicker({ users, value, onChange, ariaLabel, listLabel, emptyLabel, }: { users: User[]; value: string; onChange: (id: string) => void; ariaLabel?: string; listLabel?: string; emptyLabel?: string; }) { const { t } = useTranslation(); const [query, setQuery] = useState(''); const [open, setOpen] = useState(false); const aria = ariaLabel ?? t('picker.userAria'); const selected = users.find((u) => u.id === value); const q = query.trim().toLowerCase(); const filtered = q ? users.filter( (u) => userLabel(u).toLowerCase().includes(q) || u.email.toLowerCase().includes(q), ) : users; const close = () => { setOpen(false); setQuery(''); }; const handleBlur = (e: React.FocusEvent) => { if (e.currentTarget.contains(e.relatedTarget as Node)) return; close(); }; return (
{ setQuery(e.target.value); setOpen(true); }} onFocus={() => setOpen(true)} /> {selected && !open && ( )}
{open && (
{filtered.length === 0 ? (
{q ? t('picker.userNoResults', { query: query.trim() }) : (emptyLabel ?? t('picker.userEmpty'))}
) : ( filtered.map((u) => ( )) )}
)}
); }