Commit e18b4890 authored by 田翔's avatar 田翔

fix: 自动计算形态默认值设置,支持传值渲染

parent 5be43907
{ {
"name": "panda-xform", "name": "panda-xform",
"version": "2.1.0", "version": "2.1.1",
"description": "2.1.0: 关联表单计算树形", "description": "2.1.1: 自动计算形态默认值设置,支持传值渲染",
"keywords": [ "keywords": [
"panda-xform" "panda-xform"
], ],
......
...@@ -2877,7 +2877,7 @@ const advancedWidgets = [ ...@@ -2877,7 +2877,7 @@ const advancedWidgets = [
name: '自动计算', name: '自动计算',
schema: { schema: {
title: '自动计算', title: '自动计算',
type: 'string', type: 'number',
widget: 'AutoCalculate', widget: 'AutoCalculate',
}, },
setting: { setting: {
...@@ -2902,13 +2902,12 @@ const advancedWidgets = [ ...@@ -2902,13 +2902,12 @@ const advancedWidgets = [
displayType: 'row', displayType: 'row',
labelWidth: 80, labelWidth: 80,
}, },
placeholder: { // placeholder: {
title: '提示语', // title: '提示语',
type: 'string', // type: 'string',
displayType: 'row', // displayType: 'row',
labelWidth: 80, // labelWidth: 80,
// default: '该组件用于自动计算' // },
},
presetValue: { presetValue: {
title: '默认值', title: '默认值',
type: 'number', type: 'number',
......
...@@ -220,7 +220,7 @@ const FormDesigner = (props, ref) => { ...@@ -220,7 +220,7 @@ const FormDesigner = (props, ref) => {
</div> </div>
} }
> >
<FormRender ref={formRenderRef} schema={schema} /> <FormRender ref={formRenderRef} schemaValues={{ schema }} />
</Drag> </Drag>
</div> </div>
</div> </div>
......
...@@ -15,16 +15,46 @@ const XRender = (props, ref) => { ...@@ -15,16 +15,46 @@ const XRender = (props, ref) => {
} }
)) ))
const { schema } = props const { schemaValues } = props
const { schema, values } = schemaValues
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext) const { getPrefixCls } = useContext(ConfigProvider.ConfigContext)
const prefixCls = getPrefixCls('pandaXform') const prefixCls = getPrefixCls('pandaXform')
const pandaXform = getPrefixCls() const pandaXform = getPrefixCls()
const [startTime, setStartTime] = useState(new Date().getTime()) const [startTime, setStartTime] = useState(new Date().getTime())
const form = useForm() const form = useForm()
const schemaForm = useMemo(() => {
if (Array.isArray(values)) {
let parent = schema?.properties
let parentObj = {}
if (isObject(parent)) {
for (let v in parent) {
let child = parent[v]?.properties
let childObj = {}
if (isObject(child)) {
for (let s in child) {
let value = ''
values.forEach(v => {
if (v.fieldName === s) {
value = v.fieldValue
}
})
childObj[s] = { ...child[s], presetValue: value || child[s].presetValue }
}
}
if (JSON.stringify(childObj) !== '{}') {
parentObj[v] = { ...parent[v], properties: childObj }
}
}
}
return { ...schema, properties: parentObj }
}
return schema
}, [schema, values])
const watch = useMemo(() => { const watch = useMemo(() => {
return getWatch(schema, form, startTime) return getWatch(schema, form, startTime)
}, [schema, form, startTime]) }, [schemaForm, form, startTime])
const getValues = async () => { const getValues = async () => {
let { data, errors } = await form.submit() let { data, errors } = await form.submit()
...@@ -48,7 +78,7 @@ const XRender = (props, ref) => { ...@@ -48,7 +78,7 @@ const XRender = (props, ref) => {
<FormRender <FormRender
configProvider={{ prefixCls: pandaXform }} configProvider={{ prefixCls: pandaXform }}
form={form} form={form}
schema={schema} schema={schemaForm}
mapping={{ mapping={{
object: 'Header', object: 'Header',
}} }}
......
import React from 'react' import React, { useEffect } from 'react'
import { InputNumber } from 'antd' import { InputNumber } from 'antd'
const AutoCalculate = (props) => { const AutoCalculate = (props) => {
const { value, onChange, schema, addons } = props const { value, onChange, schema, addons } = props
const { disabled } = schema const { disabled, presetValue } = schema
useEffect(() => {
onChange(presetValue)
}, [presetValue])
return ( return (
<InputNumber <InputNumber
value={value} value={Number(value)}
style={{ width: '100%' }} style={{ width: '100%' }}
disabled={disabled} disabled={disabled}
> >
......
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