Portal/rsconcept/frontend/src/components/select/SelectOperation.tsx

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-07-21 15:17:36 +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-21 15:17:36 +03:00
import { IOperation, OperationID } from '@/models/oss';
import { matchOperation } from '@/models/ossAPI';
interface SelectOperationProps extends CProps.Styling {
items?: IOperation[];
value?: IOperation;
onSelectValue: (newValue?: IOperation) => void;
2024-07-26 21:08:31 +03:00
2024-07-21 15:17:36 +03:00
placeholder?: string;
2024-07-26 21:08:31 +03:00
noBorder?: boolean;
2024-07-21 15:17:36 +03:00
}
function SelectOperation({
className,
items,
value,
onSelectValue,
placeholder = 'Выберите операцию',
...restProps
}: SelectOperationProps) {
const options =
items?.map(cst => ({
value: cst.id,
label: `${cst.alias}: ${cst.title}`
})) ?? [];
function filter(option: { value: OperationID | undefined; label: string }, inputValue: string) {
const operation = items?.find(item => item.id === option.value);
return !operation ? false : matchOperation(operation, inputValue);
}
2024-07-21 15:17:36 +03:00
return (
<SelectSingle
className={clsx('text-ellipsis', className)}
options={options}
2024-07-29 16:55:48 +03:00
value={value ? { value: value.id, label: `${value.alias}: ${value.title}` } : null}
2024-07-21 15:17:36 +03:00
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 SelectOperation;