ConceptPortal-public/rsconcept/frontend/src/features/oss/components/select-operation.tsx

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-07-21 15:19:57 +03:00
'use client';
import clsx from 'clsx';
2025-03-12 12:04:50 +03:00
import { SelectSingle } from '@/components/input';
import { type Styling } from '@/components/props';
import { type IOperation } from '../models/oss';
2025-03-12 11:55:43 +03:00
import { matchOperation } from '../models/oss-api';
2024-07-21 15:19:57 +03:00
interface SelectOperationProps extends Styling {
value: IOperation | null;
onChange: (newValue: IOperation | null) => void;
2024-07-26 21:09:16 +03:00
items?: IOperation[];
2024-07-21 15:19:57 +03:00
placeholder?: string;
2024-07-26 21:09:16 +03:00
noBorder?: boolean;
2024-07-21 15:19:57 +03:00
}
export function SelectOperation({
2024-07-21 15:19:57 +03:00
className,
items,
value,
onChange,
2024-07-21 15:19:57 +03:00
placeholder = 'Выберите операцию',
...restProps
}: SelectOperationProps) {
const options =
items?.map(cst => ({
value: cst.id,
label: `${cst.alias}: ${cst.title}`
})) ?? [];
2025-02-17 15:12:15 +03:00
function filter(option: { value: string | undefined; label: string }, query: string) {
const operation = items?.find(item => item.id === Number(option.value));
return !operation ? false : matchOperation(operation, query);
}
2024-07-21 15:19:57 +03:00
return (
<SelectSingle
className={clsx('text-ellipsis', className)}
options={options}
2024-07-29 16:56:24 +03:00
value={value ? { value: value.id, label: `${value.alias}: ${value.title}` } : null}
onChange={data => onChange(items?.find(cst => cst.id === data?.value) ?? null)}
2024-07-21 15:19:57 +03:00
filterOption={filter}
placeholder={placeholder}
{...restProps}
/>
);
}