Admin: panel silowni jak w apce + wyznaczanie wlasciciela (grupowane menu kontekstowe z modalami, sekcja Wlasciciele)
This commit is contained in:
109
admin/src/components/UserPicker.tsx
Normal file
109
admin/src/components/UserPicker.tsx
Normal file
@@ -0,0 +1,109 @@
|
||||
import { useState } from 'react';
|
||||
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 = 'Szukaj użytkownika',
|
||||
listLabel = 'Lista użytkowników',
|
||||
emptyLabel = 'Brak użytkowników',
|
||||
}: {
|
||||
users: User[];
|
||||
value: string;
|
||||
onChange: (id: string) => void;
|
||||
ariaLabel?: string;
|
||||
listLabel?: string;
|
||||
emptyLabel?: string;
|
||||
}) {
|
||||
const [query, setQuery] = useState('');
|
||||
const [open, setOpen] = useState(false);
|
||||
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<HTMLDivElement>) => {
|
||||
if (e.currentTarget.contains(e.relatedTarget as Node)) return;
|
||||
close();
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative" onBlur={handleBlur}>
|
||||
<div className="flex items-center gap-2 rounded-xl border border-line bg-input px-3 focus-within:border-accent">
|
||||
<Search className="h-4 w-4 shrink-0 text-dim" />
|
||||
<input
|
||||
role="combobox"
|
||||
aria-expanded={open}
|
||||
aria-label={ariaLabel}
|
||||
className="w-full bg-transparent py-2.5 text-[0.95rem] focus:outline-none"
|
||||
placeholder={selected && !open ? '' : `${ariaLabel}…`}
|
||||
value={open ? query : selected ? userLabel(selected) : ''}
|
||||
onChange={(e) => {
|
||||
setQuery(e.target.value);
|
||||
setOpen(true);
|
||||
}}
|
||||
onFocus={() => setOpen(true)}
|
||||
/>
|
||||
{selected && !open && (
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Wyczyść wybór użytkownika"
|
||||
onClick={() => onChange('')}
|
||||
className="shrink-0 cursor-pointer text-dim hover:text-ink"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{open && (
|
||||
<div
|
||||
role="listbox"
|
||||
aria-label={listLabel}
|
||||
className="absolute z-20 mt-1.5 max-h-56 w-full overflow-y-auto rounded-xl border border-line bg-card shadow-xl"
|
||||
>
|
||||
{filtered.length === 0 ? (
|
||||
<div className="px-3.5 py-3 text-[0.875rem] text-dim">
|
||||
{q ? `Brak użytkowników dla „${query.trim()}”` : emptyLabel}
|
||||
</div>
|
||||
) : (
|
||||
filtered.map((u) => (
|
||||
<button
|
||||
key={u.id}
|
||||
type="button"
|
||||
role="option"
|
||||
aria-selected={u.id === value}
|
||||
onClick={() => {
|
||||
onChange(u.id);
|
||||
close();
|
||||
}}
|
||||
className={`flex w-full cursor-pointer items-center justify-between gap-3 px-3.5 py-2.5 text-left text-[0.95rem] hover:bg-line/40 ${
|
||||
u.id === value ? 'font-semibold text-accent-text' : ''
|
||||
}`}
|
||||
>
|
||||
<span className="min-w-0 truncate">{userLabel(u)}</span>
|
||||
{u.firstName && (
|
||||
<span className="shrink-0 text-[0.875rem] text-dim">{u.email}</span>
|
||||
)}
|
||||
</button>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user