import { createColumnHelper } from '@tanstack/react-table'; import { Dispatch, useCallback, useEffect, useMemo, useState } from 'react'; import MiniButton from '../../components/Common/MiniButton'; import DataTable, { IConditionalStyle } from '../../components/DataTable'; import { ArrowsRotateIcon, CheckIcon, CrossIcon } from '../../components/Icons'; import RSInput from '../../components/RSInput'; import ConstituentaPicker from '../../components/Shared/ConstituentaPicker'; import { useConceptTheme } from '../../context/ThemeContext'; import { IConstituenta, IRSForm } from '../../models/rsform'; import { IArgumentValue } from '../../models/rslang'; import { prefixes } from '../../utils/constants'; interface ArgumentsTabProps { state: IArgumentsState schema: IRSForm partialUpdate: Dispatch> } export interface IArgumentsState { arguments: IArgumentValue[] definition: string } const argumentsHelper = createColumnHelper(); function ArgumentsTab({ state, schema, partialUpdate }: ArgumentsTabProps) { const { colors } = useConceptTheme(); const [selectedCst, setSelectedCst] = useState(undefined); const [selectedArgument, setSelectedArgument] = useState(undefined); const [argumentValue, setArgumentValue] = useState(''); const selectedClearable = useMemo( () => { return argumentValue && !!selectedArgument && !!selectedArgument.value; }, [argumentValue, selectedArgument]); const isModified = useMemo( () => (selectedArgument && argumentValue !== selectedArgument.value), [selectedArgument, argumentValue]); useEffect( () => { if (!selectedArgument && state.arguments.length > 0) { setSelectedArgument(state.arguments[0]); } }, [state.arguments, selectedArgument]); const handleSelectArgument = useCallback( (arg: IArgumentValue) => { setSelectedArgument(arg); if (arg.value) { setArgumentValue(arg.value); } }, []); const handleSelectConstituenta = useCallback( (cst: IConstituenta) => { setSelectedCst(cst); setArgumentValue(cst.alias); }, []); const handleClearArgument = useCallback( (target: IArgumentValue) => { const newArg = { ...target, value: '' } partialUpdate({ arguments: state.arguments.map((arg) => (arg.alias !== target.alias ? arg : newArg)) }); setSelectedArgument(newArg); }, [partialUpdate, state.arguments]); const handleReset = useCallback( () => { setArgumentValue(selectedArgument?.value ?? ''); }, [selectedArgument]); const handleAssignArgument = useCallback( (target: IArgumentValue, value: string) => { const newArg = { ...target, value: value } partialUpdate({ arguments: state.arguments.map((arg) => (arg.alias !== target.alias ? arg : newArg)) }); setSelectedArgument(newArg); }, [partialUpdate, state.arguments]); const columns = useMemo( () => [ argumentsHelper.accessor('alias', { id: 'alias', header: 'Имя', size: 40, minSize: 40, maxSize: 40, cell: props =>
{props.getValue()}
}), argumentsHelper.accessor(arg => arg.value || 'свободный аргумент', { id: 'value', header: 'Значение', size: 200, minSize: 200, maxSize: 200, }), argumentsHelper.accessor(arg => arg.typification, { id: 'type', header: 'Типизация', size: 150, minSize: 150, maxSize: 150, enableHiding: true, cell: props =>
{props.getValue()}
}), argumentsHelper.display({ id: 'actions', size: 50, minSize: 50, maxSize: 50, cell: props =>
{props.row.original.value && } noHover onClick={() => handleClearArgument(props.row.original)} />}
}) ], [handleClearArgument]); const conditionalRowStyles = useMemo( (): IConditionalStyle[] => [{ when: (arg: IArgumentValue) => arg.alias === selectedArgument?.alias, style: { backgroundColor: colors.bgSelected }, }], [selectedArgument, colors]); return (

Аргументы отсутствуют

} onRowClicked={handleSelectArgument} />
{selectedArgument?.alias || 'ARG'} = setArgumentValue(newValue)} />
} disabled={!argumentValue || !selectedArgument} onClick={() => handleAssignArgument(selectedArgument!, argumentValue)} /> } /> } onClick={() => selectedArgument ? handleClearArgument(selectedArgument) : undefined} />
); } export default ArgumentsTab;