2024-07-30 15:59:37 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
import { SelectSingle } from '@/components/Input';
|
2025-01-28 23:23:03 +03:00
|
|
|
import { CProps } from '@/components/props';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
2025-02-12 15:12:59 +03:00
|
|
|
import { ILibraryItem } from '../models/library';
|
2025-02-10 01:32:16 +03:00
|
|
|
import { matchLibraryItem } from '../models/libraryAPI';
|
2024-07-30 15:59:37 +03:00
|
|
|
|
|
|
|
interface SelectLibraryItemProps extends CProps.Styling {
|
|
|
|
items?: ILibraryItem[];
|
|
|
|
value?: ILibraryItem;
|
2025-02-04 20:35:18 +03:00
|
|
|
onChange: (newValue?: ILibraryItem) => void;
|
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-12 15:12:59 +03:00
|
|
|
function filter(option: { value: number | undefined; label: string }, inputValue: string) {
|
2024-12-13 21:30:49 +03:00
|
|
|
const item = items?.find(item => item.id === option.value);
|
|
|
|
return !item ? false : matchLibraryItem(item, inputValue);
|
|
|
|
}
|
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-04 20:35:18 +03:00
|
|
|
onChange={data => onChange(items?.find(cst => cst.id === data?.value))}
|
2024-07-30 15:59:37 +03:00
|
|
|
// @ts-expect-error: TODO: use type definitions from react-select in filter object
|
|
|
|
filterOption={filter}
|
|
|
|
placeholder={placeholder}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|