Commit 5be43907 authored by 田翔's avatar 田翔

关联表单计算树形

parent d818d58b
{
"name": "panda-xform",
"version": "2.0.9",
"description": "2.0.9: 命名规范问题",
"version": "2.1.0",
"description": "2.1.0: 关联表单计算树形",
"keywords": [
"panda-xform"
],
......
import React, { useMemo, useContext, forwardRef, useImperativeHandle, createContext } from 'react'
import React, { useMemo, useContext, forwardRef, useImperativeHandle, createContext, useState } from 'react'
import FormRender, { useForm } from 'form-render'
import { ConfigProvider } from 'antd'
import widgets from '../widgets'
......@@ -19,11 +19,12 @@ const XRender = (props, ref) => {
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext)
const prefixCls = getPrefixCls('pandaXform')
const pandaXform = getPrefixCls()
const [startTime, setStartTime] = useState(new Date().getTime())
const form = useForm()
const watch = useMemo(() => {
return getWatch(schema, form)
}, [schema])
return getWatch(schema, form, startTime)
}, [schema, form, startTime])
const getValues = async () => {
let { data, errors } = await form.submit()
......
......@@ -48,6 +48,7 @@ export const jsonToValues = (data, paths) => {
}
const getRules = (json) => {
let AutoCalculate = []
let parent = json?.properties
if (isObject(parent)) {
for (let v in parent) {
......@@ -55,17 +56,17 @@ const getRules = (json) => {
if (isObject(child)) {
for (let s in child) {
if (child[s].widget === 'AutoCalculate') {
return {
AutoCalculate.push({
filedName: s,
...child[s],
targetPath: `${v}.${s}`,
}
})
}
}
}
}
}
return {}
return AutoCalculate
}
const getPath = (json, filed) => {
......@@ -85,10 +86,22 @@ const getPath = (json, filed) => {
return ''
}
const getFieldName = (configs, tableName) => {
let filedName = []
configs.forEach(v => {
if (v['台账名称'] === tableName) {
filedName.push(v.fieldName)
}
})
return filedName
}
const automaticComputation = async (params) => {
if ((new Date().getTime() - params.startTime) < 1000) return
let param = {
tableName: params.tableName,
filedFormulas: params.filedFormulas,
relationForm: params.relationForm,
values: jsonToValues(params.form.getValues(), params.paths).formValue
}
const { code, data, msg } = await formAutomaticComputation(param)
......@@ -108,35 +121,56 @@ const automaticComputation = async (params) => {
const setValue = debounce(automaticComputation)
export const getWatch = (schema, form) => {
export const getWatch = (schema, form, startTime) => {
let watch = {}
let { rules, calculateRule, filedName } = getRules(schema)
let paths = []
if (Array.isArray(rules)) {
let item = rules.find(v => v.tableName === schema.tableName)
if (item) {
if (Array.isArray(item.fields)) {
item.fields.forEach(v => {
let path = getPath(schema, v)
if (path) {
paths.push(path)
let { relationForm } = form?.getValues()
const configs = relationForm?.configs || []
if (Array.isArray(configs)) {
configs.forEach(v => {
v['台账名称'] = v.accountName
v['映射字段'] = v.mappedField
})
}
const AutoCalculate = getRules(schema)
if (Array.isArray(AutoCalculate)) {
AutoCalculate.forEach(item => {
const { filedName, rules, calculateRule } = item
let paths = []
if (Array.isArray(rules)) {
rules.forEach(s => {
let item = { tableName: null, fields: [] }
if (schema.tableName === s.tableName) {
item = s
} else {
item = { fields: getFieldName(configs, s.tableName) }
}
if (item) {
if (Array.isArray(item.fields)) {
item.fields.forEach(v => {
let path = getPath(schema, v)
if (path) {
paths.push(path)
}
})
}
}
})
}
}
}
if (Array.isArray(paths)) {
paths.forEach(v => {
watch[v] = () => {
let params = {
tableName: schema.tableName,
filedFormulas: [{ fieldName: filedName, formula: calculateRule }],
form: form,
paths: paths,
schema: schema
}
setValue(params)
if (Array.isArray(paths)) {
paths.forEach(v => {
watch[v] = () => {
let params = {
tableName: schema.tableName,
filedFormulas: [{ fieldName: filedName, formula: calculateRule }],
form: form,
paths: paths,
relationForm,
schema: schema,
startTime
}
setValue(params)
}
})
}
})
}
......
......@@ -291,7 +291,6 @@ const RelationForm = (props) => {
const onOk = async () => {
if (addons) {
console.log('addons', addons)
const { formValue, errors } = await baseFormRef.current.getValues()
if (errors.length) {
return message.error('请按照提示完善表单内容')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment