ConceptPortal-public/rsconcept/frontend/src/features/users/components/SelectUser.tsx

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-05-27 20:42:34 +03:00
'use client';
import clsx from 'clsx';
import { SelectSingle } from '@/components/Input';
2025-01-28 23:23:42 +03:00
import { CProps } from '@/components/props';
import { useLabelUser } from '../backend/useLabelUser';
import { useUsers } from '../backend/useUsers';
import { matchUser } from '../models/userAPI';
2024-05-27 20:42:34 +03:00
interface SelectUserProps extends CProps.Styling {
value: number | null;
2025-02-17 15:12:15 +03:00
onChange: (newValue: number) => void;
filter?: (userID: number) => boolean;
2024-07-26 21:09:16 +03:00
placeholder?: string;
noBorder?: boolean;
2024-05-27 20:42:34 +03:00
}
2025-02-11 20:56:24 +03:00
export function SelectUser({
2024-05-27 20:42:34 +03:00
className,
filter,
2024-05-27 20:42:34 +03:00
value,
onChange,
2024-05-27 20:42:34 +03:00
placeholder = 'Выберите пользователя',
...restProps
}: SelectUserProps) {
const { users } = useUsers();
const getUserLabel = useLabelUser();
const items = filter ? users.filter(user => filter(user.id)) : users;
const options = items.map(user => ({
value: user.id,
label: getUserLabel(user.id)
}));
2025-02-17 15:12:15 +03:00
function filterLabel(option: { value: string | undefined; label: string }, query: string) {
const user = items.find(item => item.id === Number(option.value));
return !user ? false : matchUser(user, query);
}
2024-05-27 20:42:34 +03:00
return (
<SelectSingle
className={clsx('text-ellipsis', className)}
options={options}
2024-07-29 16:56:24 +03:00
value={value ? { value: value, label: getUserLabel(value) } : null}
2024-05-27 20:42:34 +03:00
onChange={data => {
if (data?.value !== undefined) onChange(data.value);
2024-05-27 20:42:34 +03:00
}}
filterOption={filterLabel}
2024-05-27 20:42:34 +03:00
placeholder={placeholder}
{...restProps}
/>
);
}