2024-07-30 15:59:37 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2025-01-28 23:23:03 +03:00
|
|
|
import { CProps } from '@/components/props';
|
|
|
|
import SelectSingle from '@/components/ui/SelectSingle';
|
2024-07-30 15:59:37 +03:00
|
|
|
import { ILibraryItem, LibraryItemID } from '@/models/library';
|
|
|
|
import { matchLibraryItem } from '@/models/libraryAPI';
|
|
|
|
|
|
|
|
interface SelectLibraryItemProps extends CProps.Styling {
|
|
|
|
items?: ILibraryItem[];
|
|
|
|
value?: ILibraryItem;
|
|
|
|
onSelectValue: (newValue?: ILibraryItem) => void;
|
|
|
|
|
|
|
|
placeholder?: string;
|
|
|
|
noBorder?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
function SelectLibraryItem({
|
|
|
|
className,
|
|
|
|
items,
|
|
|
|
value,
|
|
|
|
onSelectValue,
|
|
|
|
placeholder = 'Выберите схему',
|
|
|
|
...restProps
|
|
|
|
}: SelectLibraryItemProps) {
|
2024-12-13 21:30:49 +03:00
|
|
|
const options =
|
|
|
|
items?.map(cst => ({
|
|
|
|
value: cst.id,
|
|
|
|
label: `${cst.alias}: ${cst.title}`
|
|
|
|
})) ?? [];
|
|
|
|
|
|
|
|
function filter(option: { value: LibraryItemID | undefined; label: string }, inputValue: string) {
|
|
|
|
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}
|
|
|
|
onChange={data => onSelectValue(items?.find(cst => cst.id === data?.value))}
|
|
|
|
// @ts-expect-error: TODO: use type definitions from react-select in filter object
|
|
|
|
filterOption={filter}
|
|
|
|
placeholder={placeholder}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SelectLibraryItem;
|