ConceptPortal-public/rsconcept/frontend/src/components/select/SelectConstituenta.tsx

52 lines
1.6 KiB
TypeScript
Raw Normal View History

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