ConceptPortal-public/rsconcept/frontend/src/pages/RSFormPage/ViewConstituents/TableSideConstituents.tsx

133 lines
3.4 KiB
TypeScript
Raw Normal View History

'use client';
import { useEffect } from 'react';
2023-12-08 19:24:08 +03:00
2024-05-16 22:39:28 +03:00
import BadgeConstituenta from '@/components/info/BadgeConstituenta';
import DataTable, { createColumnHelper, IConditionalStyle } from '@/components/ui/DataTable';
2024-06-21 19:27:36 +03:00
import NoData from '@/components/ui/NoData';
import TextContent from '@/components/ui/TextContent';
import { useConceptOptions } from '@/context/ConceptOptionsContext';
2024-03-17 19:24:12 +03:00
import { ConstituentaID, IConstituenta } from '@/models/rsform';
2024-05-23 13:36:16 +03:00
import { PARAMETER, prefixes } from '@/utils/constants';
import { describeConstituenta } from '@/utils/labels';
2023-12-08 19:24:08 +03:00
const DESCRIPTION_MAX_SYMBOLS = 280;
interface TableSideConstituentsProps {
2023-12-28 14:04:44 +03:00
items: IConstituenta[];
2024-05-23 13:36:16 +03:00
activeCst?: IConstituenta;
2024-03-17 19:24:12 +03:00
onOpenEdit: (cstID: ConstituentaID) => void;
2024-08-24 08:24:32 +03:00
autoScroll?: boolean;
2023-12-28 14:04:44 +03:00
maxHeight: string;
2023-12-08 19:24:08 +03:00
}
const columnHelper = createColumnHelper<IConstituenta>();
function TableSideConstituents({
items,
activeCst,
2024-08-24 08:24:32 +03:00
autoScroll = true,
onOpenEdit,
maxHeight
}: TableSideConstituentsProps) {
2024-04-01 19:07:20 +03:00
const { colors } = useConceptOptions();
2023-12-08 19:24:08 +03:00
useEffect(() => {
2024-05-23 13:36:16 +03:00
if (!activeCst) {
return;
}
2024-08-24 08:24:32 +03:00
if (autoScroll) {
setTimeout(() => {
const element = document.getElementById(`${prefixes.cst_side_table}${activeCst.alias}`);
if (element) {
element.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'end'
});
}
}, PARAMETER.refreshTimeout);
}
}, [activeCst, autoScroll]);
2024-05-23 13:36:16 +03:00
const columns = [
columnHelper.accessor('alias', {
id: 'alias',
header: () => <span className='pl-3'>Имя</span>,
size: 65,
minSize: 65,
footer: undefined,
cell: props => (
<BadgeConstituenta
className='mr-[-0.5rem]'
theme={colors}
value={props.row.original}
prefixID={prefixes.cst_side_table}
/>
)
}),
columnHelper.accessor(cst => describeConstituenta(cst), {
id: 'description',
header: 'Описание',
size: 1000,
minSize: 250,
maxSize: 1000,
cell: props => (
<TextContent
noTooltip
text={props.getValue()}
maxLength={DESCRIPTION_MAX_SYMBOLS}
style={{
textWrap: 'pretty',
fontSize: 12
}}
/>
)
})
];
2023-12-28 14:04:44 +03:00
const conditionalRowStyles: IConditionalStyle<IConstituenta>[] = [
{
when: (cst: IConstituenta) => !!activeCst && cst.id === activeCst?.id,
style: {
backgroundColor: colors.bgSelected
2023-12-08 19:24:08 +03:00
}
},
{
when: (cst: IConstituenta) => !!activeCst && cst.spawner === activeCst?.id && cst.id !== activeCst?.id,
style: {
backgroundColor: colors.bgOrange50
}
},
{
when: (cst: IConstituenta) => activeCst?.id !== undefined && cst.spawn.includes(activeCst.id),
style: {
backgroundColor: colors.bgGreen50
}
}
];
2023-12-08 19:24:08 +03:00
2023-12-28 14:04:44 +03:00
return (
<DataTable
dense
noFooter
2024-05-02 21:34:47 +03:00
className='text-sm select-none cc-scroll-y'
2023-12-28 14:04:44 +03:00
style={{ maxHeight: maxHeight }}
data={items}
columns={columns}
conditionalRowStyles={conditionalRowStyles}
headPosition='0'
enableHiding
noDataComponent={
2024-06-21 19:27:36 +03:00
<NoData className='min-h-[5rem]'>
2023-12-28 14:04:44 +03:00
<p>Список конституент пуст</p>
<p>Измените параметры фильтра</p>
2024-06-21 19:27:36 +03:00
</NoData>
2023-12-28 14:04:44 +03:00
}
onRowClicked={cst => onOpenEdit(cst.id)}
2023-12-28 14:04:44 +03:00
/>
);
2023-12-08 19:24:08 +03:00
}
export default TableSideConstituents;