2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2024-10-29 12:44:51 +03:00
|
|
|
|
import clsx from 'clsx';
|
2024-12-13 21:30:49 +03:00
|
|
|
|
import { useEffect, useState } from 'react';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
import DataTable, { createColumnHelper, IConditionalStyle } from '@/components/ui/DataTable';
|
|
|
|
|
import SearchBar from '@/components/ui/SearchBar';
|
2024-06-26 19:47:05 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/ConceptOptionsContext';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { CstMatchMode } from '@/models/miscellaneous';
|
|
|
|
|
import { IConstituenta } from '@/models/rsform';
|
|
|
|
|
import { matchConstituenta } from '@/models/rsformAPI';
|
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
import { describeConstituenta } from '@/utils/labels';
|
|
|
|
|
|
|
|
|
|
import BadgeConstituenta from '../info/BadgeConstituenta';
|
2024-10-29 12:44:51 +03:00
|
|
|
|
import { CProps } from '../props';
|
2024-06-21 19:16:41 +03:00
|
|
|
|
import NoData from '../ui/NoData';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-10-29 12:44:51 +03:00
|
|
|
|
interface PickConstituentaProps extends CProps.Styling {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
id?: string;
|
|
|
|
|
prefixID: string;
|
|
|
|
|
data?: IConstituenta[];
|
|
|
|
|
rows?: number;
|
|
|
|
|
|
|
|
|
|
initialFilter?: string;
|
|
|
|
|
onBeginFilter?: (cst: IConstituenta) => boolean;
|
|
|
|
|
describeFunc?: (cst: IConstituenta) => string;
|
|
|
|
|
matchFunc?: (cst: IConstituenta, filter: string) => boolean;
|
|
|
|
|
|
|
|
|
|
value?: IConstituenta;
|
|
|
|
|
onSelectValue: (newValue: IConstituenta) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columnHelper = createColumnHelper<IConstituenta>();
|
|
|
|
|
|
|
|
|
|
function PickConstituenta({
|
|
|
|
|
id,
|
|
|
|
|
data,
|
|
|
|
|
value,
|
|
|
|
|
initialFilter = '',
|
|
|
|
|
rows = 4,
|
|
|
|
|
prefixID = prefixes.cst_list,
|
|
|
|
|
describeFunc = describeConstituenta,
|
|
|
|
|
matchFunc = (cst, filter) => matchConstituenta(cst, filter, CstMatchMode.ALL),
|
|
|
|
|
onBeginFilter,
|
2024-10-29 12:44:51 +03:00
|
|
|
|
onSelectValue,
|
|
|
|
|
className,
|
|
|
|
|
...restProps
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}: PickConstituentaProps) {
|
|
|
|
|
const { colors } = useConceptOptions();
|
|
|
|
|
const [filteredData, setFilteredData] = useState<IConstituenta[]>([]);
|
|
|
|
|
const [filterText, setFilterText] = useState(initialFilter);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!data) {
|
|
|
|
|
setFilteredData([]);
|
|
|
|
|
} else {
|
|
|
|
|
const newData = onBeginFilter ? data.filter(onBeginFilter) : data;
|
|
|
|
|
if (filterText) {
|
|
|
|
|
setFilteredData(newData.filter(cst => matchFunc(cst, filterText)));
|
|
|
|
|
} else {
|
|
|
|
|
setFilteredData(newData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [data, filterText, matchFunc, onBeginFilter]);
|
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
const columns = [
|
|
|
|
|
columnHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
size: 65,
|
|
|
|
|
minSize: 65,
|
|
|
|
|
maxSize: 65,
|
|
|
|
|
cell: props => <BadgeConstituenta theme={colors} value={props.row.original} prefixID={prefixID} />
|
|
|
|
|
}),
|
|
|
|
|
columnHelper.accessor(cst => describeFunc(cst), {
|
|
|
|
|
id: 'description',
|
|
|
|
|
size: 1000,
|
|
|
|
|
minSize: 1000
|
|
|
|
|
})
|
|
|
|
|
];
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
const conditionalRowStyles: IConditionalStyle<IConstituenta>[] = [
|
|
|
|
|
{
|
|
|
|
|
when: (cst: IConstituenta) => cst.id === value?.id,
|
|
|
|
|
style: { backgroundColor: colors.bgSelected }
|
|
|
|
|
}
|
|
|
|
|
];
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
return (
|
2024-10-29 12:44:51 +03:00
|
|
|
|
<div className={clsx('border divide-y', className)} {...restProps}>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<SearchBar
|
|
|
|
|
id={id ? `${id}__search` : undefined}
|
2024-10-29 12:05:23 +03:00
|
|
|
|
className='clr-input rounded-t-md'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
noBorder
|
2024-11-21 15:09:31 +03:00
|
|
|
|
query={filterText}
|
|
|
|
|
onChangeQuery={newValue => setFilterText(newValue)}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
/>
|
|
|
|
|
<DataTable
|
|
|
|
|
id={id}
|
|
|
|
|
rows={rows}
|
|
|
|
|
contentHeight='1.3rem'
|
|
|
|
|
dense
|
|
|
|
|
noHeader
|
|
|
|
|
noFooter
|
|
|
|
|
className='text-sm select-none cc-scroll-y'
|
|
|
|
|
data={filteredData}
|
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
|
|
|
|
noDataComponent={
|
2024-06-21 19:16:41 +03:00
|
|
|
|
<NoData className='min-h-[6rem]'>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<p>Список конституент пуст</p>
|
|
|
|
|
<p>Измените параметры фильтра</p>
|
2024-06-21 19:16:41 +03:00
|
|
|
|
</NoData>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
onRowClicked={onSelectValue}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PickConstituenta;
|