diff --git a/src/components/ConfigurationEditorForm/index.tsx b/src/components/ConfigurationEditorForm/index.tsx index 5ccd5df..d1b6d0c 100644 --- a/src/components/ConfigurationEditorForm/index.tsx +++ b/src/components/ConfigurationEditorForm/index.tsx @@ -1,30 +1,55 @@ +import { DeleteOutlined, PlusOutlined } from '@ant-design/icons' import { Theme as AntDTheme } from '@rjsf/antd' import { withTheme } from '@rjsf/core' import { UiSchema } from '@rjsf/utils' import validator from '@rjsf/validator-ajv8' -import { FC, useEffect, useState } from 'react' - -import { ConfigurationEditorPropsType } from '@pages/ConfigurationEditorPage' -import { Tabs } from 'antd' -import equal from 'deep-equal' +import { + Badge, + Button, + Tabs, + Tooltip, + Form as FormAntd, + Collapse, + Typography, + Space +} from 'antd' +import equal from 'deep-equal' +import { createRef, FC, memo, useEffect, useState } from 'react' import { useParams } from 'react-router-dom' + + +import { + ArrayFieldTemplatePropsType, + ConfigurationEditorPropsType, + DescriptionPropsType, + ErrorsObjType, + FieldTemplatePropsType, + ObjectFieldTemplatePropertyType, + ObjectFieldTemplatePropsType, + SortPropType +} from '@pages/ConfigurationEditorPage/ConfigurationEditor.type.ts' +import { ResponseSchemaType } from '@pages/ModulesPage/module.type.ts' +import { cleanEmptyParamsObject } from '@utils/objectUtils.ts' +const { Text: AntdText } = Typography + const ConfigurationEditorForm: FC = ({ - bufConfig ={}, - JsonSchema, - setBufConfig, + bufConfig = {}, + jsonSchema = {}, + submitRef, }) => { const Form = withTheme(AntDTheme) - const sortProps = (a, b) => a.name.localeCompare(b.name) + const sortProps = (a: SortPropType, b: SortPropType) => + a.name.localeCompare(b.name) const [formState, setFormState] = useState(bufConfig.data) - const [activeTabKey, setActiveTabKey] = useState() - const {id} = useParams() + const { id } = useParams() + const formRef = createRef() useEffect(() => { - if(!equal(formState, bufConfig.data)) { - setFormState(bufConfig.data) - } + if (!equal(formState, bufConfig.data)) { + setFormState(bufConfig.data) + } }, [bufConfig.data]) const uiSchema: UiSchema = { @@ -33,37 +58,205 @@ const ConfigurationEditorForm: FC = ({ } } - const onFormChange = data => { - if(!equal(data.formData, bufConfig.data)) { - const newData = { - ...bufConfig, - data: { ...data.formData } - } - if(!equal(newData, bufConfig.data)) { - setBufConfig(newData) - } + const onSubmit = (data: any) => { + if(!submitRef) return + if(!formRef.current.validateFormWithFormData(formRef.current.state.formData)) { + submitRef.current = data + } else { + submitRef.current = data.formData + } + } + + const forceSubmit = () => { + if (formRef.current) { + const formData = formRef.current.state.formData + + onSubmit(cleanEmptyParamsObject(formData)) } } - const handleTabsChange = e => { - setActiveTabKey(e) + const onFormChange = () => { + if (!formRef.current) return + if(formRef.current.validateFormWithFormData(formRef.current.state.formData)) { + formRef?.current?.submit() + } else { + forceSubmit() + } } - const ObjectFieldTemplate = (props) => { + + + const Description: FC = ({ description }) => + description ? ( + +   + + + ) : null + + const getDepth = (id: string) => { + if (!id) { + return 0 + } + return id.split('_').length + } + + const getName = (id: string) => { + if (!id) { + return '' + } + const arr = id.split('_') + return `#${arr[arr.length - 1] || ''}` + } + + const FieldTemplate: FC = (props) => { + const { + id, + classNames, + label, + rawHelp, + required, + rawDescription, + rawErrors, + children, + displayLabel, + schema + } = props + + const isObject = schema.type === 'object' + const depth = getDepth(id) + const name = label || getName(id) + + // Проверяем на наличие ошибок и формируем их вывод + const errorMessages = rawErrors ? rawErrors.join('\n') : null + + // Объекты и массивы - Обработка по глубине + if (isObject && depth !== 1) { + const title = ( +
+ {depth !== 2 && {name}} + {errorMessages && ( + {` (${errorMessages})`} + )} + {depth !== 2 && } +
+ ) + + if (depth === 2 || schema.dynamic) { + return ( +
+ {!schema.dynamic && title} + {children} +
+ ) + } + + return ( + 1 ? '' : id}> + + {children} + + + ) + } + + // Для остальных типов возвращаем форму с элементами + return ( + + {name} + + + ) : null + } + > + {children} + + ) + } + + const ArrayFieldTemplate: FC = ({ + items, + onAddClick, + canAdd, + title, + idSchema, + schema: { description } + }) => { + return ( + + + + {title} + + + + {canAdd && ( +