MSP-3423 fix ConfigurationEditorForm
This commit is contained in:
1 parent
f1abba6352
commit
fa631556b7
1 file changed
+270
-51
@@ -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<ConfigurationEditorPropsType> = ({
|
||||
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<any>()
|
||||
|
||||
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<ConfigurationEditorPropsType> = ({
|
||||
}
|
||||
}
|
||||
|
||||
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<DescriptionPropsType> = ({ description }) =>
|
||||
description ? (
|
||||
<span>
|
||||
|
||||
<Tooltip title={description}></Tooltip>
|
||||
</span>
|
||||
) : 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<FieldTemplatePropsType> = (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 = (
|
||||
<div className={required ? 'ant-form-item-required' : ''}>
|
||||
{depth !== 2 && <AntdText strong>{name}</AntdText>}
|
||||
{errorMessages && (
|
||||
<AntdText type="danger">{` (${errorMessages})`}</AntdText>
|
||||
)}
|
||||
{depth !== 2 && <Description description={rawDescription} />}
|
||||
</div>
|
||||
)
|
||||
|
||||
if (depth === 2 || schema.dynamic) {
|
||||
return (
|
||||
<div style={{ width: '100%' }}>
|
||||
{!schema.dynamic && title}
|
||||
{children}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Collapse className="collapse" defaultActiveKey={depth > 1 ? '' : id}>
|
||||
<Collapse.Panel
|
||||
key={id}
|
||||
className="configEditor_collapseObject"
|
||||
header={title}
|
||||
>
|
||||
{children}
|
||||
</Collapse.Panel>
|
||||
</Collapse>
|
||||
)
|
||||
}
|
||||
|
||||
// Для остальных типов возвращаем форму с элементами
|
||||
return (
|
||||
<FormAntd.Item
|
||||
className={classNames}
|
||||
validateStatus={errorMessages ? 'error' : undefined}
|
||||
help={errorMessages || undefined}
|
||||
extra={rawHelp}
|
||||
required={required}
|
||||
label={
|
||||
displayLabel && !schema.dynamic ? (
|
||||
<>
|
||||
{name}
|
||||
<Description description={rawDescription} />
|
||||
</>
|
||||
) : null
|
||||
}
|
||||
>
|
||||
{children}
|
||||
</FormAntd.Item>
|
||||
)
|
||||
}
|
||||
|
||||
const ArrayFieldTemplate: FC<ArrayFieldTemplatePropsType> = ({
|
||||
items,
|
||||
onAddClick,
|
||||
canAdd,
|
||||
title,
|
||||
idSchema,
|
||||
schema: { description }
|
||||
}) => {
|
||||
return (
|
||||
<Collapse defaultActiveKey={idSchema.$id}>
|
||||
<Collapse.Panel
|
||||
key={idSchema.$id}
|
||||
className="collapseArray"
|
||||
header={
|
||||
<Space
|
||||
direction="horizontal"
|
||||
style={{ justifyContent: 'space-between', width: '100%' }}
|
||||
>
|
||||
<Space direction="horizontal">
|
||||
<AntdText>{title}</AntdText>
|
||||
<Badge count={items ? items.length : 0} showZero />
|
||||
<Description description={description} />
|
||||
</Space>
|
||||
{canAdd && (
|
||||
<Button
|
||||
type="link"
|
||||
icon={<PlusOutlined />}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
onAddClick(e as unknown as MouseEvent)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Space>
|
||||
}
|
||||
>
|
||||
{items &&
|
||||
items.map((element) => (
|
||||
<div key={element.index} className="collapseArray_item">
|
||||
<div className="collapseArray_item_content">
|
||||
{element.children}
|
||||
</div>
|
||||
<Button
|
||||
type="link"
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={element.onDropIndexClick(element.index)}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</Collapse.Panel>
|
||||
</Collapse>
|
||||
)
|
||||
}
|
||||
|
||||
const ObjectFieldTemplate: FC<ObjectFieldTemplatePropsType> = (props) => {
|
||||
const {
|
||||
properties,
|
||||
idSchema,
|
||||
schema: { properties: schemaProperties }
|
||||
schema: { properties: schemaProperties },
|
||||
activeTabKey,
|
||||
handleTabsChange
|
||||
} = props
|
||||
properties.sort(sortProps)
|
||||
|
||||
properties.sort(sortProps)
|
||||
if (idSchema.$id === 'root') {
|
||||
const propsComplex: [] = []
|
||||
const propsSimple: [] = []
|
||||
const propsComplex: ObjectFieldTemplatePropertyType[] = []
|
||||
const propsSimple: ObjectFieldTemplatePropertyType[] = []
|
||||
|
||||
properties.forEach((prop) => {
|
||||
const fieldType = schemaProperties[prop.name].type
|
||||
const fieldType = schemaProperties[prop.name]?.type
|
||||
if (!fieldType || fieldType === 'array' || fieldType === 'object') {
|
||||
propsComplex.push(prop)
|
||||
} else {
|
||||
@@ -72,41 +265,67 @@ const ConfigurationEditorForm: FC<ConfigurationEditorPropsType> = ({
|
||||
})
|
||||
|
||||
return (
|
||||
<Tabs activeKey={activeTabKey} tabPosition="right" onChange={handleTabsChange} items={[
|
||||
...(propsSimple.length ? [{
|
||||
label: 'Остальные',
|
||||
key: 'General',
|
||||
children: propsSimple.map((element) => element.content),
|
||||
}] : []),
|
||||
...propsComplex.map((element) => ({
|
||||
label: schemaProperties[element.name]?.title || element.name,
|
||||
key: element.name,
|
||||
children: element.content,
|
||||
})),
|
||||
]} />
|
||||
<Tabs
|
||||
activeKey={activeTabKey}
|
||||
tabPosition="right"
|
||||
onChange={handleTabsChange}
|
||||
items={[
|
||||
...(propsSimple.length
|
||||
? [
|
||||
{
|
||||
label: 'Остальные',
|
||||
key: 'General',
|
||||
children: propsSimple.map((element) => element.content)
|
||||
}
|
||||
]
|
||||
: []),
|
||||
...propsComplex.map((element) => ({
|
||||
label: schemaProperties[element.name]?.title || element.name,
|
||||
key: element.name,
|
||||
children: element.content
|
||||
}))
|
||||
]}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return <>{properties.map((element) => element.content)}</>
|
||||
}
|
||||
|
||||
return properties.map((element) => element.content)
|
||||
const transformErrors = (errors: ErrorsObjType[]): ErrorsObjType[] => {
|
||||
if (errors.length > 0) {
|
||||
errors.forEach((error) => {
|
||||
error.message = 'Поле является обязательным'
|
||||
})
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="configuration-editor-page__form">
|
||||
<Form
|
||||
formContext={{
|
||||
// labelCol: { span: 8 },
|
||||
// wrapperCol: { span: 30 },
|
||||
// layout: 'horizontal'
|
||||
labelCol: { span: 8 },
|
||||
wrapperCol: { span: 30 },
|
||||
layout: 'vertical'
|
||||
}}
|
||||
uiSchema={uiSchema}
|
||||
templates={{ ObjectFieldTemplate }}
|
||||
templates={{
|
||||
ObjectFieldTemplate: ObjectFieldTemplate as any,
|
||||
FieldTemplate: FieldTemplate as any,
|
||||
ArrayFieldTemplate: ArrayFieldTemplate as any
|
||||
}}
|
||||
ref={formRef}
|
||||
transformErrors={transformErrors as any}
|
||||
schema={jsonSchema?.schema as ResponseSchemaType}
|
||||
onSubmit={onSubmit}
|
||||
validator={validator}
|
||||
schema={JsonSchema?.schema}
|
||||
formData={id === 'new' ? {} : formState}
|
||||
formData={id === 'new' ? {} : formState}
|
||||
onChange={onFormChange}
|
||||
showErrorList={false}
|
||||
></Form>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
export default ConfigurationEditorForm
|
||||
export default memo(ConfigurationEditorForm)
|
||||
Reference in new issue
Block a user