2024-07-30 15:59:37 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2025-03-12 12:04:23 +03:00
|
|
|
import { SelectSingle } from '@/components/input';
|
2025-02-20 20:22:05 +03:00
|
|
|
import { type Styling } from '@/components/props';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
2025-02-20 20:22:05 +03:00
|
|
|
import { type ILibraryItem } from '../backend/types';
|
2025-03-12 11:54:32 +03:00
|
|
|
import { matchLibraryItem } from '../models/library-api';
|
2024-07-30 15:59:37 +03:00
|
|
|
|
2025-02-20 20:22:05 +03:00
|
|
|
interface SelectLibraryItemProps extends Styling {
|
2025-02-19 22:32:50 +03:00
|
|
|
value: ILibraryItem | null;
|
|
|
|
onChange: (newValue: ILibraryItem | null) => void;
|
2024-07-30 15:59:37 +03:00
|
|
|
|
2025-02-19 22:32:50 +03:00
|
|
|
items?: ILibraryItem[];
|
2024-07-30 15:59:37 +03:00
|
|
|
placeholder?: string;
|
|
|
|
noBorder?: boolean;
|
|
|
|
}
|
|
|
|
|
2025-02-12 15:12:59 +03:00
|
|
|
export function SelectLibraryItem({
|
2024-07-30 15:59:37 +03:00
|
|
|
className,
|
|
|
|
items,
|
|
|
|
value,
|
2025-02-04 20:35:18 +03:00
|
|
|
onChange,
|
2024-07-30 15:59:37 +03:00
|
|
|
placeholder = 'Выберите схему',
|
|
|
|
...restProps
|
|
|
|
}: SelectLibraryItemProps) {
|
2024-12-13 21:30:49 +03:00
|
|
|
const options =
|
|
|
|
items?.map(cst => ({
|
|
|
|
value: cst.id,
|
|
|
|
label: `${cst.alias}: ${cst.title}`
|
|
|
|
})) ?? [];
|
|
|
|
|
2025-02-17 15:11:32 +03:00
|
|
|
function filter(option: { value: string | undefined; label: string }, query: string) {
|
|
|
|
const item = items?.find(item => item.id === Number(option.value));
|
|
|
|
return !item ? false : matchLibraryItem(item, query);
|
2024-12-13 21:30:49 +03:00
|
|
|
}
|
2024-07-30 15:59:37 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SelectSingle
|
|
|
|
className={clsx('text-ellipsis', className)}
|
|
|
|
options={options}
|
|
|
|
value={value ? { value: value.id, label: `${value.alias}: ${value.title}` } : null}
|
2025-02-19 22:32:50 +03:00
|
|
|
onChange={data => onChange(items?.find(cst => cst.id === data?.value) ?? null)}
|
2024-07-30 15:59:37 +03:00
|
|
|
filterOption={filter}
|
|
|
|
placeholder={placeholder}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|