ConceptPortal-public/rsconcept/frontend/src/features/rsform/components/SelectConstituenta.tsx

55 lines
1.6 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
import { SelectSingle } from '@/components/Input';
2025-01-28 23:23:42 +03:00
import { CProps } from '@/components/props';
2025-02-12 21:36:25 +03:00
import { describeConstituenta, describeConstituentaTerm } from '../labels';
import { IConstituenta } from '../models/rsform';
import { matchConstituenta } from '../models/rsformAPI';
import { CstMatchMode } from '../stores/cstSearch';
2024-05-16 22:39:28 +03:00
interface SelectConstituentaProps extends CProps.Styling {
value?: IConstituenta;
onChange: (newValue?: IConstituenta) => void;
2024-07-26 21:09:16 +03:00
items?: IConstituenta[];
2024-05-27 20:42:34 +03:00
placeholder?: string;
2024-07-26 21:09:16 +03:00
noBorder?: boolean;
}
2024-05-27 20:42:34 +03:00
function SelectConstituenta({
className,
items,
value,
onChange,
2024-05-27 20:42:34 +03:00
placeholder = 'Выберите конституенту',
...restProps
}: SelectConstituentaProps) {
const options =
items?.map(cst => ({
value: cst.id,
label: `${cst.alias}${cst.is_inherited ? '*' : ''}: ${describeConstituenta(cst)}`
})) ?? [];
2025-02-17 15:12:15 +03:00
function filter(option: { value: string | undefined; label: string }, query: string) {
const cst = items?.find(item => item.id === Number(option.value));
return !cst ? false : matchConstituenta(cst, query, CstMatchMode.ALL);
}
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}: ${describeConstituentaTerm(value)}` } : null}
onChange={data => onChange(items?.find(cst => cst.id === data?.value))}
filterOption={filter}
2024-05-27 20:42:34 +03:00
placeholder={placeholder}
{...restProps}
/>
);
}
2024-05-16 22:39:28 +03:00
export default SelectConstituenta;