2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { createColumnHelper } from '@tanstack/react-table';
|
|
|
|
|
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 { IconAccept, IconRemove, IconReset } from '@/components/Icons';
|
|
|
|
|
import RSInput from '@/components/RSInput';
|
|
|
|
|
import PickConstituenta from '@/components/select/PickConstituenta';
|
|
|
|
|
import DataTable, { IConditionalStyle } from '@/components/ui/DataTable';
|
|
|
|
|
import MiniButton from '@/components/ui/MiniButton';
|
2024-06-21 19:16:41 +03:00
|
|
|
|
import NoData from '@/components/ui/NoData';
|
2024-06-26 19:47:05 +03:00
|
|
|
|
import { useConceptOptions } from '@/context/ConceptOptionsContext';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { IConstituenta, IRSForm } from '@/models/rsform';
|
|
|
|
|
import { IArgumentValue } from '@/models/rslang';
|
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
|
2024-06-26 19:47:05 +03:00
|
|
|
|
interface TabArgumentsProps {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
state: IArgumentsState;
|
|
|
|
|
schema: IRSForm;
|
|
|
|
|
partialUpdate: React.Dispatch<Partial<IArgumentsState>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IArgumentsState {
|
|
|
|
|
arguments: IArgumentValue[];
|
|
|
|
|
definition: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const argumentsHelper = createColumnHelper<IArgumentValue>();
|
|
|
|
|
|
2024-06-26 19:47:05 +03:00
|
|
|
|
function TabArguments({ state, schema, partialUpdate }: TabArgumentsProps) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
const { colors } = useConceptOptions();
|
|
|
|
|
|
|
|
|
|
const [selectedCst, setSelectedCst] = useState<IConstituenta | undefined>(undefined);
|
|
|
|
|
const [selectedArgument, setSelectedArgument] = useState<IArgumentValue | undefined>(undefined);
|
|
|
|
|
const [argumentValue, setArgumentValue] = useState('');
|
2024-12-13 21:30:49 +03:00
|
|
|
|
const isModified = selectedArgument && argumentValue !== selectedArgument.value;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!selectedArgument && state.arguments.length > 0) {
|
|
|
|
|
setSelectedArgument(state.arguments[0]);
|
|
|
|
|
}
|
|
|
|
|
}, [state.arguments, selectedArgument]);
|
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
function handleSelectArgument(arg: IArgumentValue) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
setSelectedArgument(arg);
|
|
|
|
|
if (arg.value) {
|
|
|
|
|
setArgumentValue(arg.value);
|
|
|
|
|
}
|
2024-12-13 21:30:49 +03:00
|
|
|
|
}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
function handleSelectConstituenta(cst: IConstituenta) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
setSelectedCst(cst);
|
|
|
|
|
setArgumentValue(cst.alias);
|
2024-12-13 21:30:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleClearArgument(target: IArgumentValue) {
|
|
|
|
|
const newArg = { ...target, value: '' };
|
|
|
|
|
partialUpdate({
|
|
|
|
|
arguments: state.arguments.map(arg => (arg.alias !== target.alias ? arg : newArg))
|
|
|
|
|
});
|
|
|
|
|
setSelectedArgument(newArg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleReset() {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
setArgumentValue(selectedArgument?.value ?? '');
|
2024-12-13 21:30:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleAssignArgument(target: IArgumentValue, value: string) {
|
|
|
|
|
const newArg = { ...target, value: value };
|
|
|
|
|
partialUpdate({
|
|
|
|
|
arguments: state.arguments.map(arg => (arg.alias !== target.alias ? arg : newArg))
|
|
|
|
|
});
|
|
|
|
|
setSelectedArgument(newArg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
|
argumentsHelper.accessor('alias', {
|
|
|
|
|
id: 'alias',
|
|
|
|
|
size: 40,
|
|
|
|
|
minSize: 40,
|
|
|
|
|
maxSize: 40,
|
|
|
|
|
cell: props => <div className='text-center'>{props.getValue()}</div>
|
|
|
|
|
}),
|
|
|
|
|
argumentsHelper.accessor(arg => arg.value || 'свободный аргумент', {
|
|
|
|
|
id: 'value',
|
|
|
|
|
size: 200,
|
|
|
|
|
minSize: 200,
|
|
|
|
|
maxSize: 200
|
|
|
|
|
}),
|
|
|
|
|
argumentsHelper.accessor(arg => arg.typification, {
|
|
|
|
|
id: 'type',
|
|
|
|
|
enableHiding: true,
|
|
|
|
|
cell: props => (
|
|
|
|
|
<div
|
|
|
|
|
className={clsx(
|
|
|
|
|
'min-w-[9.3rem] max-w-[9.3rem]', // prettier: split lines
|
|
|
|
|
'text-sm break-words'
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{props.getValue()}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}),
|
|
|
|
|
argumentsHelper.display({
|
|
|
|
|
id: 'actions',
|
|
|
|
|
size: 0,
|
|
|
|
|
cell: props => (
|
|
|
|
|
<div className='h-[1.25rem] w-[1.25rem]'>
|
|
|
|
|
{props.row.original.value ? (
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Очистить значение'
|
|
|
|
|
noPadding
|
|
|
|
|
noHover
|
|
|
|
|
icon={<IconRemove size='1.25rem' className='icon-red' />}
|
|
|
|
|
onClick={() => handleClearArgument(props.row.original)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const conditionalRowStyles: IConditionalStyle<IArgumentValue>[] = [
|
|
|
|
|
{
|
|
|
|
|
when: (arg: IArgumentValue) => arg.alias === selectedArgument?.alias,
|
|
|
|
|
style: { backgroundColor: colors.bgSelected }
|
|
|
|
|
}
|
|
|
|
|
];
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
return (
|
2024-12-12 13:17:24 +03:00
|
|
|
|
<div className='cc-fade-in'>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<DataTable
|
|
|
|
|
dense
|
|
|
|
|
noFooter
|
|
|
|
|
noHeader
|
|
|
|
|
className={clsx(
|
|
|
|
|
'max-h-[5.8rem] min-h-[5.8rem]', // prettier: split lines
|
|
|
|
|
'cc-scroll-y',
|
|
|
|
|
'text-sm',
|
|
|
|
|
'border',
|
|
|
|
|
'select-none'
|
|
|
|
|
)}
|
|
|
|
|
data={state.arguments}
|
|
|
|
|
columns={columns}
|
|
|
|
|
conditionalRowStyles={conditionalRowStyles}
|
2024-06-21 19:16:41 +03:00
|
|
|
|
noDataComponent={<NoData className='min-h-[3.6rem]'>Аргументы отсутствуют</NoData>}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
onRowClicked={handleSelectArgument}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={clsx(
|
|
|
|
|
'my-4', // prettier: split lines
|
|
|
|
|
'flex gap-2 justify-center items-center',
|
|
|
|
|
'select-none'
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
title='Выберите аргумент из списка сверху и значение из списка снизу'
|
|
|
|
|
className='font-semibold text-center'
|
|
|
|
|
>
|
|
|
|
|
{selectedArgument?.alias || 'ARG'}
|
|
|
|
|
</span>
|
|
|
|
|
<span>=</span>
|
|
|
|
|
<RSInput
|
|
|
|
|
noTooltip
|
|
|
|
|
className='w-[12rem]'
|
|
|
|
|
value={argumentValue}
|
|
|
|
|
onChange={newValue => setArgumentValue(newValue)}
|
|
|
|
|
/>
|
|
|
|
|
<div className='flex'>
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Подставить значение аргумента'
|
|
|
|
|
noHover
|
|
|
|
|
className='py-0'
|
2024-09-04 14:35:03 +03:00
|
|
|
|
icon={<IconAccept size='1.5rem' className='icon-green' />}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
disabled={!argumentValue || !selectedArgument}
|
|
|
|
|
onClick={() => handleAssignArgument(selectedArgument!, argumentValue)}
|
|
|
|
|
/>
|
|
|
|
|
<MiniButton
|
|
|
|
|
title='Очистить поле'
|
|
|
|
|
noHover
|
|
|
|
|
className='py-0'
|
|
|
|
|
disabled={!isModified}
|
|
|
|
|
onClick={handleReset}
|
2024-09-04 14:35:03 +03:00
|
|
|
|
icon={<IconReset size='1.5rem' className='icon-primary' />}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<PickConstituenta
|
|
|
|
|
id='dlg_argument_picker'
|
|
|
|
|
value={selectedCst}
|
|
|
|
|
data={schema?.items}
|
|
|
|
|
onSelectValue={handleSelectConstituenta}
|
|
|
|
|
prefixID={prefixes.cst_modal_list}
|
|
|
|
|
rows={7}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<RSInput
|
|
|
|
|
disabled
|
2024-08-29 12:41:59 +03:00
|
|
|
|
noTooltip
|
2024-06-07 20:17:03 +03:00
|
|
|
|
id='result'
|
|
|
|
|
placeholder='Итоговое определение'
|
|
|
|
|
className='mt-[1.2rem]'
|
|
|
|
|
height='5.1rem'
|
|
|
|
|
value={state.definition}
|
|
|
|
|
/>
|
2024-12-12 13:17:24 +03:00
|
|
|
|
</div>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 19:47:05 +03:00
|
|
|
|
export default TabArguments;
|