import {
observer
} from 'mobx-react';
import {
WeaFormItem,
WeaSearchGroup,
} from 'ecCom';
import {
WeaSwitch
} from 'comsMobx';
import {
isEmpty
} from 'lodash';
import '../style/magicForm.less';
@observer
export default class MagicForm extends React.Component {
//表单是否需要分组
isNeedSearchGroup = (conditions) => {
return conditions.length > 1
}
//表单元素是否显示
isItemShow = (item) => {
const {
store
} = this.props, {
form,
customRenderKeyList
} = store;
const {
domkey
} = item;
//自定义渲染
if (customRenderKeyList && customRenderKeyList.includes(domkey[0])) {
return false;
}
const linkageConfig = this.findLinkageConfig(domkey[0]);
if (linkageConfig) {
const {
activeKey,
showValue
} = linkageConfig;
//active表单元素当前的value
const curActiveVal = form.getFormParams()[activeKey];
//当value包含curActiveVal时,显示当前表单元素
return showValue.includes(curActiveVal)
}
return true;
}
//查找联动当前表单元素的
findLinkageConfig = (passiveKey) => {
const {
store
} = this.props, {
linkageConfigs
} = store;
return linkageConfigs && linkageConfigs.find(config => config.passiveKey === passiveKey);
}
renderForm = (items) => {
const {
store
} = this.props, {
form
} = store;
return items.map( (item,index) => {
if (!this.isItemShow(item)) {
return null;
}
const {
label,
labelcol,
fieldcol,
domkey,
colon = true,
} = item;
return (
{this.renderFormItemChildren(item)}
)
})
}
renderFormItemChildren = (item) => {
const {
store
} = this.props, {
form,
handleFormChange,
} = store;
return (
{this.renderOtherComponents(item)}
)
}
renderOtherComponents = (item) => {
const {
store
} = this.props, {
form,
handleFormChange,
otherComponents
} = store;
if (!otherComponents) {
return null;
}
const {
domkey
} = item;
const fn = otherComponents[domkey[0]];
if (fn) {
return fn(item, form)
} else {
return null;
}
}
//表单元素控制组
getFormGroup = (index,condition) => {
const {
store
} = this.props, {
groupShowConfigs,
form,
} = store;
const {
title,
items,
defaultshow = true,
} = condition;
if (groupShowConfigs) {
return groupShowConfigs.map((config, i) => {
const {
activeKey,
passiveIndex,
showValue
} = config;
const activeVal = form.getFormParams()[activeKey];
if (
(index === passiveIndex && showValue.includes(activeVal)) ||
(index !== passiveIndex)
) {
return (
)
}
})
}else{
return (
)
}
}
getLabel = (item, index) => {
const {
store
} = this.props, {
hasOrderNumber,
labelPres
} = store;
const {label, domkey} = item;
if (!hasOrderNumber && isEmpty(labelPres)) {
return label
}
if (hasOrderNumber) {
return (
{index+1}
{label}
)
}
if (!isEmpty(labelPres)) {
return (
{
labelPres[domkey[0]] && {labelPres[domkey[0]]}
}
{label}
)
}
}
render() {
const {
store
} = this.props, {
conditions,
form,
} = store, {
isFormInit
} = form;
const hasSearchGroup = this.isNeedSearchGroup(conditions);
return (
{
isFormInit && conditions.map( (condition,index) => {
const {
items,
} = condition;
if (hasSearchGroup) {
return this.getFormGroup(index,condition);
} else {
return this.renderForm(items)
}
})
}
);
}
}