Portal/rsconcept/frontend/src/features/library/components/SelectLibraryItem.tsx

51 lines
1.3 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
import { SelectSingle } from '@/components/Input';
2025-01-28 23:23:03 +03:00
import { CProps } from '@/components/props';
2025-02-18 19:39:54 +03:00
import { ILibraryItem } from '../backend/types';
import { matchLibraryItem } from '../models/libraryAPI';
interface SelectLibraryItemProps extends CProps.Styling {
items?: ILibraryItem[];
value?: ILibraryItem;
onChange: (newValue?: ILibraryItem) => void;
placeholder?: string;
noBorder?: boolean;
}
2025-02-12 15:12:59 +03:00
export function SelectLibraryItem({
className,
items,
value,
onChange,
placeholder = 'Выберите схему',
...restProps
}: SelectLibraryItemProps) {
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);
}
return (
<SelectSingle
className={clsx('text-ellipsis', className)}
options={options}
value={value ? { value: value.id, label: `${value.alias}: ${value.title}` } : null}
onChange={data => onChange(items?.find(cst => cst.id === data?.value))}
filterOption={filter}
placeholder={placeholder}
{...restProps}
/>
);
}