Portal/rsconcept/frontend/src/features/rsform/components/PickMultiConstituenta.tsx

144 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import clsx from 'clsx';
2025-02-05 21:55:26 +03:00
import { useState } from 'react';
2024-06-07 20:17:03 +03:00
import DataTable, { createColumnHelper, RowSelectionState } from '@/components/DataTable';
import { SearchBar } from '@/components/Input';
2025-01-28 23:23:03 +03:00
import { CProps } from '@/components/props';
import { NoData } from '@/components/View';
2024-10-23 15:18:46 +03:00
import { Graph } from '@/models/Graph';
2024-06-07 20:17:03 +03:00
2025-02-11 20:56:11 +03:00
import { describeConstituenta } from '../labels';
import { IConstituenta, IRSForm } from '../models/rsform';
import { isBasicConcept, matchConstituenta } from '../models/rsformAPI';
import { CstMatchMode } from '../stores/cstSearch';
import BadgeConstituenta from './BadgeConstituenta';
import ToolbarGraphSelection from './ToolbarGraphSelection';
2024-06-07 20:17:03 +03:00
interface PickMultiConstituentaProps extends CProps.Styling {
2024-06-07 20:17:03 +03:00
id?: string;
value: number[];
onChange: (newValue: number[]) => void;
2024-10-23 15:18:46 +03:00
schema: IRSForm;
items: IConstituenta[];
2024-10-23 15:18:46 +03:00
2024-06-07 20:17:03 +03:00
rows?: number;
2024-10-28 23:55:12 +03:00
noBorder?: boolean;
2024-06-07 20:17:03 +03:00
}
const columnHelper = createColumnHelper<IConstituenta>();
export function PickMultiConstituenta({
2024-10-23 15:18:46 +03:00
id,
schema,
items,
2024-10-23 15:18:46 +03:00
rows,
2024-10-28 23:55:12 +03:00
noBorder,
value,
onChange,
className,
...restProps
2024-10-23 15:18:46 +03:00
}: PickMultiConstituentaProps) {
const [filterText, setFilterText] = useState('');
2024-06-07 20:17:03 +03:00
2025-02-05 21:55:26 +03:00
const filtered = filterText ? items.filter(cst => matchConstituenta(cst, filterText, CstMatchMode.ALL)) : items;
const rowSelection: RowSelectionState = Object.fromEntries(
filtered.map((cst, index) => [String(index), value.includes(cst.id)])
);
const foldedGraph = (() => {
if (items.length === schema.items.length) {
2024-10-23 15:18:46 +03:00
return schema.graph;
}
const newGraph = new Graph();
schema.graph.nodes.forEach(node => {
newGraph.addNode(node.id);
node.outputs.forEach(output => {
newGraph.addEdge(node.id, output);
});
});
schema.items
.filter(item => items.find(cst => cst.id === item.id) === undefined)
2024-10-23 15:18:46 +03:00
.forEach(item => {
newGraph.foldNode(item.id);
});
return newGraph;
})();
2024-10-23 15:18:46 +03:00
2024-06-07 20:17:03 +03:00
function handleRowSelection(updater: React.SetStateAction<RowSelectionState>) {
if (!items) {
onChange([]);
2024-06-07 20:17:03 +03:00
} else {
const newRowSelection = typeof updater === 'function' ? updater(rowSelection) : updater;
const newSelection: number[] = [];
filtered.forEach((cst, index) => {
2024-06-07 20:17:03 +03:00
if (newRowSelection[String(index)] === true) {
newSelection.push(cst.id);
}
});
2025-02-10 15:48:58 +03:00
onChange([...value.filter(cst_id => !filtered.find(cst => cst.id === cst_id)), ...newSelection]);
2024-06-07 20:17:03 +03:00
}
}
const columns = [
columnHelper.accessor('alias', {
id: 'alias',
header: () => <span className='pl-3'>Имя</span>,
size: 65,
cell: props => <BadgeConstituenta value={props.row.original} />
}),
columnHelper.accessor(cst => describeConstituenta(cst), {
id: 'description',
size: 1000,
header: 'Описание'
})
];
2024-06-07 20:17:03 +03:00
return (
<div className={clsx(noBorder ? '' : 'border', className)} {...restProps}>
2024-10-28 23:55:12 +03:00
<div className={clsx('px-3 flex justify-between items-center', 'clr-input', 'border-b', 'rounded-t-md')}>
<div className='w-[24ch] select-none whitespace-nowrap'>
{items.length > 0 ? `Выбраны ${value.length} из ${items.length}` : 'Конституенты'}
</div>
<SearchBar
id='dlg_constituents_search'
noBorder
className='min-w-[6rem] pr-2 flex-grow'
query={filterText}
onChangeQuery={setFilterText}
/>
2024-10-23 15:18:46 +03:00
<ToolbarGraphSelection
graph={foldedGraph}
isCore={cstID => isBasicConcept(schema.cstByID.get(cstID)?.cst_type)}
isOwned={cstID => !schema.cstByID.get(cstID)?.is_inherited}
value={value}
onChange={onChange}
emptySelection={value.length === 0}
2024-10-23 15:18:46 +03:00
className='w-fit'
/>
2024-06-07 20:17:03 +03:00
</div>
<DataTable
id={id}
dense
noFooter
rows={rows}
contentHeight='1.3rem'
className='cc-scroll-y text-sm select-none rounded-b-md'
data={filtered}
2024-06-07 20:17:03 +03:00
columns={columns}
headPosition='0rem'
enableRowSelection
rowSelection={rowSelection}
onRowSelectionChange={handleRowSelection}
noDataComponent={
2024-06-21 19:16:41 +03:00
<NoData>
2024-06-07 20:17:03 +03:00
<p>Список пуст</p>
2024-06-21 19:16:41 +03:00
</NoData>
2024-06-07 20:17:03 +03:00
}
/>
</div>
);
}