1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* 钟是志
* 2019年2月26日
* 用于 新增弹出modal
* 一个按钮 + 点击按钮显示的弹窗
* 弹窗中包含一个表单 并填写数据后点确定 调接口->关闭弹窗 刷新页面
* */
import ButtonDiy from '@/baseComponent/ButtonDiy';
import ModalDiy from '@/baseComponent/ModalDiy';
import React, { Component, Fragment } from 'react';
import { deepCopy } from '@/baseComponent/utils';
import { mustHaveValue, transLateTimeTOUnix } from '../../config/index';
import { message } from 'antd';
import PropTypes from 'prop-types';
import { startProcess } from '../publicApiService';
import FormArray from '../component/FormArray';
export default class ModalForm extends Component {
constructor(props) {
super(props);
const { values } = this.props;
this.state = {
showModal: false,
formValues: { ...values }, // 将默认值传进去 用于新增时可能遇到的需要传值的情况
requireOtherFiled: {},
};
}
componentWillUnmount() {}
changeShow = () => {
const { showModal } = this.state;
this.clearData();
this.setState({
showModal: !showModal,
});
};
formStateChange = (value, key) => {
const { diyFormStateChange } = this.props;
if (diyFormStateChange) {
return diyFormStateChange(value, key, this);
}
let oldValue = this.state.formValues;
if (typeof value === 'object') {
oldValue[key] = deepCopy(value);
} else {
oldValue[key] = value;
}
this.setState({
formValues: oldValue,
});
};
clearData = () => {
const { values } = this.props;
this.setState({
formValues: { ...values },
});
};
handleOk = () => {
let { formValues } = this.state;
const {
fields,
url,
responseCallBack,
getPage,
beforeSubmit,
workId,
seriousOptions,
} = this.props;
if (!mustHaveValue(fields, formValues)) {
return false;
}
let data = {
...formValues,
};
for (let item of fields) {
if (data[item.key]) {
data[item.key] = transLateTimeTOUnix(data[item.key], item.dataType);
}
if (data[item.endKey]) {
data[item.endKey] = transLateTimeTOUnix(data[item.endKey], item.dataType);
}
}
if (beforeSubmit) {
data = beforeSubmit(data, this);
if (!data) {
return false;
}
}
const postData = {
content: JSON.stringify(data),
appId: workId,
};
if (seriousOptions && seriousOptions.length) {
postData.level = seriousOptions[0].key;
}
startProcess(postData).then((response) => {
if (!responseCallBack(response)) {
this.changeShow();
return false;
} else {
message.success('保存成功');
getPage();
this.changeShow();
}
});
};
render() {
const { showModal, formValues } = this.state;
const { name, className, fields, nameSpan, fileSpan, icon, modalWidth } = this.props;
return (
<Fragment>
<ButtonDiy name={name} className={className} icon={icon} handleClick={this.changeShow} />
{showModal ? (
<ModalDiy
handleOk={this.handleOk}
title={name}
handleCancel={this.changeShow}
width={modalWidth}>
<FormArray
config={fields}
value={formValues}
changeValue={this.formStateChange}
nameSpan={nameSpan}
fileSpan={fileSpan}
/>
</ModalDiy>
) : null}
</Fragment>
);
}
}
ModalForm.propTypes = {
name: PropTypes.string, // 按钮名称和 弹窗的标题
className: PropTypes.string, // 按钮样式
fields: PropTypes.array.isRequired, // 填写的字段的配置
values: PropTypes.object, // 如果有默认参数 则在页面的业务逻辑中传进来
url: PropTypes.string.isRequired, // 接口url
icon: PropTypes.string, // 按钮图标
responseCallBack: PropTypes.func, // 接口返回数据检查
getPage: PropTypes.func, // 刷新页面的方法
nameSpan: PropTypes.object, // 页面排版
fileSpan: PropTypes.object, // 页面排版
};
ModalForm.defaultProps = {
name: '新增',
className: 'primaryBlue',
values: {},
url: 'asdasd/asdasd',
responseCallBack: (response) => {
return !!response;
},
getPage: () => {},
nameSpan: { big: 5, small: 5 },
fileSpan: { big: 1, small: 1 },
};