import React from 'react'; import { WeaFormItem, WeaSearchGroup, } from 'ecCom'; import { WeaSwitch } from 'comsMobx'; import { toJS } from 'mobx'; import { Row, Col, } from 'antd'; class MagicForm extends React.Component { constructor(props) { super(props); this.state = { usedAsForceUpdate: new Date() } } //监听表单元素事件的回调函数 handleFormChange = (cb) => { const { onFormElementsChange, dynamicFields, } = this.props; dynamicFields && dynamicFields.map(field => { const { activeKey, } = field; if (Object.keys(cb)[0] === activeKey) { this.forceUpdate(); } }); //当父组件没有传onFormElementsChange属性时,不执行下面的回调函数。 if (!onFormElementsChange) return; onFormElementsChange(cb); } //强制刷新组件 forceUpdate = () => { this.setState({ usedAsForceUpdate: new Date() }); } //判断当前表单元素能否被渲染 canRender = (domkey) => { const { dynamicFields, form } = this.props; //表单元素能否被渲染的标志:true -> 可以被渲染; false -> 不可以被渲染 let flag = true; dynamicFields.map(field => { const { passiveKey, activeKey, showValue } = field; const showValueArray = showValue.split(','); //判断是否为被联动的表单元素(被动) if (domkey === passiveKey) { const activeValue = form.getFormParams()[activeKey]; if (!showValueArray.includes(activeValue)) { flag = false; } } }); return flag; } getSubComponent = (params) => { const { dirtyKeys, //需要被特殊渲染的表单元素的domkey集合 dirtyComponent, //特殊组件 classNames, } = this.props, { domkey, item, form, label } = params; if (dirtyKeys && dirtyKeys.includes(domkey)) { return dirtyComponent(params) } else { return (
{ dynamicTips[domkey].length > 0 && dynamicTips[domkey][_index] }
); } //横向相邻组件 if (this.isObjectHasDomkey(transverseSiblingComponents, domkey)) { siblings.push(transverseSiblingComponents[domkey]); } //纵向相邻组件 if (this.isObjectHasDomkey(longitudinalSiblingComponents, domkey)) { siblings.push(longitudinalSiblingComponents[domkey]); } //自定义组件 if (this.isObjectHasDomkey(customizations, domkey)) { siblings.push(customizations[domkey]); } return siblings; } //判断domkey是否在容器中 isObjectHasDomkey = (obj, domkey) => { return obj && Object.keys(obj).includes(domkey) } render() { const { conditions, form, isFormInit, dynamicFields, //动态域 date, //刷新组件 dirtyKeys, renderBlacklist, //不需要渲染的items, labels, wrapperStyle, errors, } = this.props, { usedAsForceUpdate } = this.state; if (!isFormInit) return null; //WeaFormItem组件的容器 const formItemContainer = []; const translatedConditions = toJS(conditions); //conditions数据类型:数组或者对象 const c = Array.isArray(translatedConditions) ? translatedConditions[0] : conditions; c.items.map((item, index) => { const { domkey, } = item; //判断当前表单元素是否需要被渲染 if (dynamicFields && !this.canRender(domkey[0])) return if (renderBlacklist && renderBlacklist.includes(domkey[0])) return if (labels && Object.keys(labels).includes(domkey[0])) { item.label = labels[domkey[0]]; } const isDirty = dirtyKeys ? dirtyKeys.includes(domkey[0]) : false; let error; if (errors) { error = errors[domkey[0]]; } else { error = !isDirty && form.getError(item); } formItemContainer.push(