'use client'; import { createColumnHelper } from '@tanstack/react-table'; import clsx from 'clsx'; import { useEffect, useState } from 'react'; import { IconAccept, IconRemove, IconReset } from '@/components/Icons'; import RSInput from '@/components/RSInput'; import PickConstituenta from '@/components/select/PickConstituenta'; import { MiniButton } from '@/components/ui/Control'; import DataTable, { IConditionalStyle } from '@/components/ui/DataTable'; import { NoData } from '@/components/ui/View'; import { IConstituenta, IRSForm } from '@/models/rsform'; import { IArgumentValue } from '@/models/rslang'; import { APP_COLORS } from '@/styling/color'; interface TabArgumentsProps { state: IArgumentsState; schema: IRSForm; partialUpdate: React.Dispatch>; } export interface IArgumentsState { arguments: IArgumentValue[]; definition: string; } const argumentsHelper = createColumnHelper(); function TabArguments({ state, schema, partialUpdate }: TabArgumentsProps) { const [selectedCst, setSelectedCst] = useState(undefined); const [selectedArgument, setSelectedArgument] = useState(undefined); const [argumentValue, setArgumentValue] = useState(''); const isModified = selectedArgument && argumentValue !== selectedArgument.value; useEffect(() => { if (!selectedArgument && state.arguments.length > 0) { setSelectedArgument(state.arguments[0]); } }, [state.arguments, selectedArgument]); function handleSelectArgument(arg: IArgumentValue) { setSelectedArgument(arg); if (arg.value) { setArgumentValue(arg.value); } } function handleSelectConstituenta(cst: IConstituenta) { setSelectedCst(cst); setArgumentValue(cst.alias); } function handleClearArgument(target: IArgumentValue) { const newArg = { ...target, value: '' }; partialUpdate({ arguments: state.arguments.map(arg => (arg.alias !== target.alias ? arg : newArg)) }); setSelectedArgument(newArg); } function handleReset() { setArgumentValue(selectedArgument?.value ?? ''); } 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 =>
{props.getValue()}
}), argumentsHelper.accessor(arg => arg.value || 'свободный аргумент', { id: 'value', size: 200, minSize: 200, maxSize: 200 }), argumentsHelper.accessor(arg => arg.typification, { id: 'type', enableHiding: true, cell: props => (
{props.getValue()}
) }), argumentsHelper.display({ id: 'actions', size: 0, cell: props => (
{props.row.original.value ? ( } onClick={() => handleClearArgument(props.row.original)} /> ) : null}
) }) ]; const conditionalRowStyles: IConditionalStyle[] = [ { when: (arg: IArgumentValue) => arg.alias === selectedArgument?.alias, style: { backgroundColor: APP_COLORS.bgSelected } } ]; return (
Аргументы отсутствуют} onRowClicked={handleSelectArgument} />
{selectedArgument?.alias || 'ARG'} = setArgumentValue(newValue)} />
} disabled={!argumentValue || !selectedArgument} onClick={() => handleAssignArgument(selectedArgument!, argumentValue)} /> } />
); } export default TabArguments;