weaver_trunk_cli/pc4mobx/hrmAttendance/public/sweet-form.js

298 lines
6.9 KiB
JavaScript
Raw Normal View History

2023-03-14 09:11:54 +08:00
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 (
<div className={this.getClassName(domkey)}>
2024-12-11 15:32:14 +08:00
<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@kleu6r`}
2023-03-14 09:11:54 +08:00
fieldConfig={item}
form={form}
formParams={form.getFormParams()}
onChange={this.handleFormChange}
/>
{this.getSiblingComponent(domkey, form)}
</div>
)
}
}
getClassName = (domkey) => {
const {
classNames
} = this.props;
return this.isObjectHasDomkey(classNames, domkey) ? classNames[domkey] : '';
}
//获取WeaSwitch组件的相邻组件
getSiblingComponent = (domkey, form) => {
const {
staticTips, //静态提示
dynamicTips, //动态提示
transverseSiblingComponents, //横向相邻组件
longitudinalSiblingComponents, //纵向相邻组件
customizations, //自定义组件
} = this.props;
let siblings = [];
//静态提示
if (this.isObjectHasDomkey(staticTips, domkey)) {
siblings.push(staticTips[domkey]);
}
//动态提示
if (this.isObjectHasDomkey(dynamicTips, domkey)) {
2024-12-11 15:32:14 +08:00
siblings.push(<p style={{padding: '5px 0'}}>{ dynamicTips[domkey][parseInt(form.getFormParams()[domkey]) - 1] }</p>);
2023-03-14 09:11:54 +08:00
}
//横向相邻组件
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,
2024-12-11 15:32:14 +08:00
wrapperStyle
2023-03-14 09:11:54 +08:00
} = 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;
formItemContainer.push(
2024-12-11 15:32:14 +08:00
<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@13eugu@${index}`}
2023-03-14 09:11:54 +08:00
label={!isDirty ? item.label : ''}
colon={!isDirty ? true : false}
labelCol={{span: 6}}
wrapperCol={{span: 18}}
2024-12-11 15:32:14 +08:00
error={!isDirty && form.getError(item) }
2023-03-14 09:11:54 +08:00
tipPosition='bottom'
className={isDirty ? 'hrm-special-form' : ''}
>
{this.getSubComponent({
domkey:domkey[0],
label:item.label,
item,
form,
})}
</WeaFormItem>
)
})
return <div className="wea-form-item-group" style={wrapperStyle ? wrapperStyle : {padding: '20px 15%'}} >{formItemContainer}</div>
}
}
//复合表单组件
class CompositeForm extends React.Component {
constructor(props) {
super(props);
}
render() {
const {
conditions
} = this.props;
return (<div>
{
conditions.map((c, index) => {
const {
title
} = c;
2024-12-11 15:32:14 +08:00
return (<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@g6bu6q@${index}`}
2023-03-14 09:11:54 +08:00
showGroup
title={title}
2024-12-11 15:32:14 +08:00
children={<MagicForm ecId={`${this && this.props && this.props.ecId || ''}_MagicForm@k5nxbk@${index}`} {...this.props} conditions={c} />}
2023-03-14 09:11:54 +08:00
/>)
})
}
</div>)
}
}
//高级搜索表单
class SearchForm extends React.Component {
constructor(props) {
super(props);
}
render() {
const {
form,
conditions,
isFormInit
} = this.props;
if (!isFormInit) return null;
const formItemContainer = [];
conditions.map(c => {
c.items.map((item, index) => {
formItemContainer.push(
2024-12-11 15:32:14 +08:00
<Col ecId={`${this && this.props && this.props.ecId || ''}_Col@3ndj0j@${index}`} span={10} offset={1}>
<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@xm3qxs@${index}`}
2023-03-14 09:11:54 +08:00
label={`${item.label}`}
labelCol={{span: 6}}
wrapperCol={{span: 18}}
style={{paddingTop: 20}}>
2024-12-11 15:32:14 +08:00
{<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@z8s4mf@${index}`} fieldConfig={item} form={form} formParams={form.getFormParams()} />}
2023-03-14 09:11:54 +08:00
</WeaFormItem>
</Col>
)
})
})
return (
2024-12-11 15:32:14 +08:00
<Row ecId={`${this && this.props && this.props.ecId || ''}_Row@zkr9p9`}>{formItemContainer}</Row>
2023-03-14 09:11:54 +08:00
)
}
}
export {
MagicForm,
CompositeForm,
SearchForm,
}