2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2025-01-21 12:00:09 +03:00
|
|
|
import { useLabelUser } from '@/backend/users/useLabelUser';
|
|
|
|
import { useUsers } from '@/backend/users/useUsers';
|
|
|
|
import { UserID } from '@/models/user';
|
2024-06-07 20:17:03 +03:00
|
|
|
import { matchUser } from '@/models/userAPI';
|
|
|
|
|
|
|
|
import { CProps } from '../props';
|
|
|
|
import SelectSingle from '../ui/SelectSingle';
|
|
|
|
|
|
|
|
interface SelectUserProps extends CProps.Styling {
|
|
|
|
value?: UserID;
|
|
|
|
onSelectValue: (newValue: UserID) => void;
|
2025-01-21 12:00:09 +03:00
|
|
|
filter?: (userID: UserID) => boolean;
|
2024-07-26 21:08:31 +03:00
|
|
|
|
|
|
|
placeholder?: string;
|
|
|
|
noBorder?: boolean;
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function SelectUser({
|
|
|
|
className,
|
2025-01-21 12:00:09 +03:00
|
|
|
filter,
|
2024-06-07 20:17:03 +03:00
|
|
|
value,
|
|
|
|
onSelectValue,
|
|
|
|
placeholder = 'Выберите пользователя',
|
|
|
|
...restProps
|
|
|
|
}: SelectUserProps) {
|
2025-01-21 12:00:09 +03:00
|
|
|
const { users } = useUsers();
|
|
|
|
const getUserLabel = useLabelUser();
|
|
|
|
|
|
|
|
const items = filter ? users.filter(user => filter(user.id)) : users;
|
2024-12-13 21:30:49 +03:00
|
|
|
const options =
|
|
|
|
items?.map(user => ({
|
|
|
|
value: user.id,
|
|
|
|
label: getUserLabel(user.id)
|
|
|
|
})) ?? [];
|
|
|
|
|
2025-01-21 12:00:09 +03:00
|
|
|
function filterLabel(option: { value: UserID | undefined; label: string }, inputValue: string) {
|
2024-12-13 21:30:49 +03:00
|
|
|
const user = items?.find(item => item.id === option.value);
|
|
|
|
return !user ? false : matchUser(user, inputValue);
|
|
|
|
}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SelectSingle
|
|
|
|
className={clsx('text-ellipsis', className)}
|
|
|
|
options={options}
|
2024-07-29 16:55:48 +03:00
|
|
|
value={value ? { value: value, label: getUserLabel(value) } : null}
|
2024-06-07 20:17:03 +03:00
|
|
|
onChange={data => {
|
2024-08-06 14:38:10 +03:00
|
|
|
if (data?.value !== undefined) onSelectValue(data.value);
|
2024-06-07 20:17:03 +03:00
|
|
|
}}
|
|
|
|
// @ts-expect-error: TODO: use type definitions from react-select in filter object
|
2025-01-21 12:00:09 +03:00
|
|
|
filterOption={filterLabel}
|
2024-06-07 20:17:03 +03:00
|
|
|
placeholder={placeholder}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SelectUser;
|