2024-03-01 18:19:33 +03:00
|
|
|
'use client';
|
|
|
|
|
2024-03-21 17:48:42 +03:00
|
|
|
import clsx from 'clsx';
|
2024-03-01 18:19:33 +03:00
|
|
|
import { useCallback, useMemo } from 'react';
|
|
|
|
|
|
|
|
import { CstMatchMode } from '@/models/miscellaneous';
|
2024-03-17 19:24:12 +03:00
|
|
|
import { ConstituentaID, IConstituenta } from '@/models/rsform';
|
2024-03-01 18:19:33 +03:00
|
|
|
import { matchConstituenta } from '@/models/rsformAPI';
|
|
|
|
import { describeConstituenta, describeConstituentaTerm } from '@/utils/labels';
|
|
|
|
|
2024-03-21 17:48:42 +03:00
|
|
|
import { CProps } from '../props';
|
2024-03-20 15:27:32 +03:00
|
|
|
import SelectSingle from '../ui/SelectSingle';
|
2024-03-01 18:19:33 +03:00
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
interface SelectConstituentaProps extends CProps.Styling {
|
2024-03-01 18:19:33 +03:00
|
|
|
items?: IConstituenta[];
|
|
|
|
value?: IConstituenta;
|
|
|
|
onSelectValue: (newValue?: IConstituenta) => void;
|
2024-05-27 20:42:34 +03:00
|
|
|
placeholder?: string;
|
2024-03-01 18:19:33 +03:00
|
|
|
}
|
|
|
|
|
2024-05-27 20:42:34 +03:00
|
|
|
function SelectConstituenta({
|
|
|
|
className,
|
|
|
|
items,
|
|
|
|
value,
|
|
|
|
onSelectValue,
|
|
|
|
placeholder = 'Выберите конституенту',
|
|
|
|
...restProps
|
|
|
|
}: SelectConstituentaProps) {
|
2024-03-01 18:19:33 +03:00
|
|
|
const options = useMemo(() => {
|
|
|
|
return (
|
|
|
|
items?.map(cst => ({
|
|
|
|
value: cst.id,
|
|
|
|
label: `${cst.alias}: ${describeConstituenta(cst)}`
|
|
|
|
})) ?? []
|
|
|
|
);
|
|
|
|
}, [items]);
|
|
|
|
|
|
|
|
const filter = useCallback(
|
2024-03-17 19:24:12 +03:00
|
|
|
(option: { value: ConstituentaID | undefined; label: string }, inputValue: string) => {
|
2024-03-01 18:19:33 +03:00
|
|
|
const cst = items?.find(item => item.id === option.value);
|
|
|
|
return !cst ? false : matchConstituenta(cst, inputValue, CstMatchMode.ALL);
|
|
|
|
},
|
|
|
|
[items]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SelectSingle
|
2024-03-21 17:48:42 +03:00
|
|
|
className={clsx('text-ellipsis', className)}
|
2024-03-01 18:19:33 +03:00
|
|
|
options={options}
|
2024-05-27 20:42:34 +03:00
|
|
|
value={value ? { value: value.id, label: `${value.alias}: ${describeConstituentaTerm(value)}` } : undefined}
|
2024-03-01 18:19:33 +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}
|
2024-05-27 20:42:34 +03:00
|
|
|
placeholder={placeholder}
|
2024-03-21 17:48:42 +03:00
|
|
|
{...restProps}
|
2024-03-01 18:19:33 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-16 22:39:28 +03:00
|
|
|
export default SelectConstituenta;
|