weaver_trunk_cli/pc4mobx/hrm/public/valhalla/components/MagicForm.js

273 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-03-14 09:11:54 +08:00
import {
observer
} from 'mobx-react';
import {
WeaFormItem,
WeaSearchGroup,
} from 'ecCom';
import {
WeaSwitch
} from 'comsMobx';
2024-12-11 15:32:14 +08:00
import {
isEmpty
} from 'lodash';
import '../style/magicForm.less';
2023-03-14 09:11:54 +08:00
@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) {
2024-12-11 15:32:14 +08:00
const {
activeKey,
showValue
} = linkageConfig;
2024-10-16 14:33:54 +08:00
2024-12-11 15:32:14 +08:00
//active表单元素当前的value
const curActiveVal = form.getFormParams()[activeKey];
//当value包含curActiveVal时显示当前表单元素
return showValue.includes(curActiveVal)
2023-03-14 09:11:54 +08:00
}
return true;
}
//查找联动当前表单元素的
findLinkageConfig = (passiveKey) => {
const {
store
} = this.props, {
linkageConfigs
} = store;
2024-12-11 15:32:14 +08:00
return linkageConfigs && linkageConfigs.find(config => config.passiveKey === passiveKey);
2023-03-14 09:11:54 +08:00
}
renderForm = (items) => {
const {
store
} = this.props, {
2024-12-11 15:32:14 +08:00
form
2023-03-14 09:11:54 +08:00
} = store;
2024-12-11 15:32:14 +08:00
return items.map( (item,index) => {
2023-03-14 09:11:54 +08:00
if (!this.isItemShow(item)) {
return null;
}
const {
label,
labelcol,
fieldcol,
2024-12-11 15:32:14 +08:00
domkey,
colon = true,
2023-03-14 09:11:54 +08:00
} = item;
return (
2024-12-11 15:32:14 +08:00
<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@8886sw@${index}`}
label={this.getLabel(item, index) }
colon={colon}
labelCol={{span: labelcol}}
wrapperCol={{span:fieldcol}}
2023-03-14 09:11:54 +08:00
error={form.getError(item)}
tipPosition='bottom'
className={domkey[0]}
>
2024-12-11 15:32:14 +08:00
{this.renderFormItemChildren(item)}
2023-03-14 09:11:54 +08:00
</WeaFormItem>
)
})
}
renderFormItemChildren = (item) => {
const {
store
} = this.props, {
form,
handleFormChange,
} = store;
return (
<div>
2024-12-11 15:32:14 +08:00
<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@63wgpb`}
2023-03-14 09:11:54 +08:00
fieldConfig={item}
form={form}
formParams={form.getFormParams()}
onChange={handleFormChange}
/>
{this.renderOtherComponents(item)}
</div>
)
}
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;
}
}
//表单元素控制组
2024-12-11 15:32:14 +08:00
getFormGroup = (index,condition) => {
2023-03-14 09:11:54 +08:00
const {
store
} = this.props, {
groupShowConfigs,
form,
} = store;
const {
title,
items,
defaultshow = true,
} = condition;
if (groupShowConfigs) {
2024-12-11 15:32:14 +08:00
return groupShowConfigs.map((config, i) => {
2023-03-14 09:11:54 +08:00
const {
activeKey,
passiveIndex,
showValue
} = config;
const activeVal = form.getFormParams()[activeKey];
if (
(index === passiveIndex && showValue.includes(activeVal)) ||
(index !== passiveIndex)
) {
return (
2024-12-11 15:32:14 +08:00
<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@te623s@${i}`}
center
2023-03-14 09:11:54 +08:00
showGroup={defaultshow}
title={title}
children={this.renderForm(items)}
/>
)
}
})
2024-12-11 15:32:14 +08:00
}else{
2023-03-14 09:11:54 +08:00
return (
2024-12-11 15:32:14 +08:00
<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@rsf36v`}
center
2023-03-14 09:11:54 +08:00
showGroup={defaultshow}
title={title}
children={this.renderForm(items)}
/>
)
}
}
2024-12-11 15:32:14 +08:00
getLabel = (item, index) => {
const {
store
} = this.props, {
hasOrderNumber,
labelPres
} = store;
const {label, domkey} = item;
if (!hasOrderNumber && isEmpty(labelPres)) {
return label
}
if (hasOrderNumber) {
return (
<span>
<div className='circle'>
<span className='number-icon'>{index+1}</span>
</div>
<span style={{marginLeft:10}}>{label}</span>
</span>
)
}
if (!isEmpty(labelPres)) {
return (
<span>
{
labelPres[domkey[0]] && <span>{labelPres[domkey[0]]}</span>
}
<span style={{marginLeft:10}}>{label}</span>
</span>
)
}
}
2023-03-14 09:11:54 +08:00
render() {
const {
store
} = this.props, {
conditions,
form,
} = store, {
isFormInit
} = form;
const hasSearchGroup = this.isNeedSearchGroup(conditions);
return (
2024-12-11 15:32:14 +08:00
<div className='magicForm'>
{
isFormInit && conditions.map( (condition,index) => {
const {
items,
} = condition;
if (hasSearchGroup) {
return this.getFormGroup(index,condition);
} else {
return this.renderForm(items)
}
})
2023-03-14 09:11:54 +08:00
}
2024-12-11 15:32:14 +08:00
</div>
2023-03-14 09:11:54 +08:00
);
}
}