Portal/rsconcept/frontend/src/features/rsform/components/SelectConstituenta.tsx

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import clsx from 'clsx';
import { SelectSingle } from '@/components/Input';
2025-01-28 23:23:03 +03:00
import { CProps } from '@/components/props';
2024-06-07 20:17:03 +03:00
import { describeConstituenta, describeConstituentaTerm } from '@/utils/labels';
import { ConstituentaID, IConstituenta } from '../models/rsform';
import { matchConstituenta } from '../models/rsformAPI';
import { CstMatchMode } from '../stores/cstSearch';
2024-06-07 20:17:03 +03:00
interface SelectConstituentaProps extends CProps.Styling {
value?: IConstituenta;
onChange: (newValue?: IConstituenta) => void;
2024-07-26 21:08:31 +03:00
items?: IConstituenta[];
2024-06-07 20:17:03 +03:00
placeholder?: string;
2024-07-26 21:08:31 +03:00
noBorder?: boolean;
2024-06-07 20:17:03 +03:00
}
function SelectConstituenta({
className,
items,
value,
onChange,
2024-06-07 20:17:03 +03:00
placeholder = 'Выберите конституенту',
...restProps
}: SelectConstituentaProps) {
const options =
items?.map(cst => ({
value: cst.id,
label: `${cst.alias}${cst.is_inherited ? '*' : ''}: ${describeConstituenta(cst)}`
})) ?? [];
function filter(option: { value: ConstituentaID | undefined; label: string }, inputValue: string) {
const cst = items?.find(item => item.id === option.value);
return !cst ? false : matchConstituenta(cst, inputValue, CstMatchMode.ALL);
}
2024-06-07 20:17:03 +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}: ${describeConstituentaTerm(value)}` } : null}
onChange={data => onChange(items?.find(cst => cst.id === data?.value))}
2024-06-07 20:17:03 +03:00
// @ts-expect-error: TODO: use type definitions from react-select in filter object
filterOption={filter}
placeholder={placeholder}
{...restProps}
/>
);
}
export default SelectConstituenta;