2023-11-06 18:03:23 +03:00
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import ConceptSearch from '@/components/Common/ConceptSearch';
|
|
|
|
|
import DataTable, { createColumnHelper, IConditionalStyle } from '@/components/DataTable';
|
|
|
|
|
import { useConceptTheme } from '@/context/ThemeContext';
|
2023-12-26 14:23:51 +03:00
|
|
|
|
import { CstMatchMode } from '@/models/miscellaneous';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import { IConstituenta } from '@/models/rsform';
|
|
|
|
|
import { matchConstituenta } from '@/models/rsformAPI';
|
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
import { describeConstituenta } from '@/utils/labels';
|
|
|
|
|
|
2023-11-06 18:03:23 +03:00
|
|
|
|
import ConstituentaBadge from './ConstituentaBadge';
|
|
|
|
|
|
|
|
|
|
interface ConstituentaPickerProps {
|
|
|
|
|
prefixID?: string
|
|
|
|
|
data?: IConstituenta[]
|
|
|
|
|
rows?: number
|
|
|
|
|
|
|
|
|
|
prefilterFunc?: (cst: IConstituenta) => boolean
|
|
|
|
|
describeFunc?: (cst: IConstituenta) => string
|
|
|
|
|
matchFunc?: (cst: IConstituenta, filter: string) => boolean
|
|
|
|
|
|
|
|
|
|
value?: IConstituenta
|
|
|
|
|
onSelectValue: (newValue: IConstituenta) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<IConstituenta>();
|
|
|
|
|
|
|
|
|
|
function ConstituentaPicker({
|
|
|
|
|
data, value,
|
|
|
|
|
rows = 4,
|
|
|
|
|
prefixID = prefixes.cst_list,
|
|
|
|
|
describeFunc = describeConstituenta,
|
|
|
|
|
matchFunc = (cst, filter) => matchConstituenta(cst, filter, CstMatchMode.ALL),
|
|
|
|
|
prefilterFunc,
|
|
|
|
|
onSelectValue
|
|
|
|
|
} : ConstituentaPickerProps) {
|
|
|
|
|
const { colors } = useConceptTheme();
|
2023-12-05 01:22:44 +03:00
|
|
|
|
const [filteredData, setFilteredData] = useState<IConstituenta[]>([]);
|
|
|
|
|
const [filterText, setFilterText] = useState('');
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
|
() => {
|
|
|
|
|
if (!data) {
|
|
|
|
|
setFilteredData([]);
|
|
|
|
|
} else {
|
|
|
|
|
const newData = prefilterFunc ? data.filter(prefilterFunc) : data;
|
|
|
|
|
if (filterText) {
|
|
|
|
|
setFilteredData(newData.filter(cst => matchFunc(cst, filterText)));
|
|
|
|
|
} else {
|
|
|
|
|
setFilteredData(newData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, [data, filterText, matchFunc, prefilterFunc]);
|
|
|
|
|
|
|
|
|
|
const columns = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
size: 65,
|
|
|
|
|
minSize: 65,
|
|
|
|
|
maxSize: 65,
|
|
|
|
|
cell: props =>
|
|
|
|
|
<ConstituentaBadge
|
|
|
|
|
theme={colors}
|
|
|
|
|
value={props.row.original}
|
|
|
|
|
prefixID={prefixID}
|
|
|
|
|
/>
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(cst => describeFunc(cst), {
|
|
|
|
|
id: 'description'
|
|
|
|
|
})
|
|
|
|
|
], [colors, prefixID, describeFunc]);
|
|
|
|
|
|
2023-11-10 15:34:59 +03:00
|
|
|
|
const size = useMemo(() => (`calc(2px + (2px + 1.8rem)*${rows})`), [rows]);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
|
|
|
|
|
const conditionalRowStyles = useMemo(
|
|
|
|
|
(): IConditionalStyle<IConstituenta>[] => [{
|
|
|
|
|
when: (cst: IConstituenta) => cst.id === value?.id,
|
|
|
|
|
style: { backgroundColor: colors.bgSelected },
|
|
|
|
|
}], [value, colors]);
|
|
|
|
|
|
|
|
|
|
return (
|
2023-12-15 17:34:50 +03:00
|
|
|
|
<div>
|
|
|
|
|
<ConceptSearch
|
2023-11-06 18:03:23 +03:00
|
|
|
|
value={filterText}
|
|
|
|
|
onChange={newValue => setFilterText(newValue)}
|
|
|
|
|
/>
|
2023-12-17 20:19:28 +03:00
|
|
|
|
<DataTable dense noHeader noFooter
|
2023-11-06 18:03:23 +03:00
|
|
|
|
className='overflow-y-auto text-sm border select-none'
|
|
|
|
|
style={{ maxHeight: size, minHeight: size }}
|
2023-12-17 20:19:28 +03:00
|
|
|
|
data={filteredData}
|
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
|
|
|
|
noDataComponent={
|
|
|
|
|
<span className='p-2 min-h-[5rem] flex flex-col justify-center text-center'>
|
|
|
|
|
<p>Список конституент пуст</p>
|
|
|
|
|
<p>Измените параметры фильтра</p>
|
|
|
|
|
</span>
|
|
|
|
|
}
|
|
|
|
|
onRowClicked={onSelectValue}
|
|
|
|
|
/>
|
2023-12-15 17:34:50 +03:00
|
|
|
|
</div>);
|
2023-11-06 18:03:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
|
export default ConstituentaPicker;
|