Edit.tsx
6.81 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { useEffect, useState } from "react";
import {
Drawer,
Form,
Space,
Input,
Button,
message,
Select,
InputNumber,
} from "antd";
import {
projectUpdateApi,
projectAddApi,
getProjectDetailApi,
} from "@/api/project";
import { getTargetListApi } from "@/api/target";
import { ITarget } from "@/views/Target/model";
import { TargetType } from "@/utils/constant";
// const roleList: any = constantMng.formatGroup("role", "value", "label");
const formItemLayout = {
labelCol: { span: 4 },
wrapperCol: { span: 20 },
};
interface IProps {
visible: boolean;
id: number;
onClose: () => void;
}
const Edit: React.FC<IProps> = (props) => {
const [form] = Form.useForm();
const { id, visible, onClose } = props;
// 提交时的loading状态
// const [loading, setLoading] = useState(false)
// const [detail, setDetail] = useState({
// type: 1,
// projectScore: "",
// targetId: "",
// targetName: "",
// projectName: "",
// projectDesc: "",
// });
const [targetList, setTargetList] = useState<ITarget[]>([]);
// 获取详情
useEffect(() => {
if (!id) {
form.resetFields();
getTargetList(form.getFieldValue("type"));
return;
}
getProjectDetailApi({ id }).then((res) => {
form.setFieldsValue(res.data);
getTargetList(form.getFieldValue("type"));
});
}, [id]);
const changeTarget = () => {
form.setFieldsValue({ targetId: "", targetName: "" });
getTargetList(form.getFieldValue("type"));
};
const getTargetList = async (values: any) => {
const res = await getTargetListApi({
params: { pageNum: 1, pageSize: 1000, type: values },
});
setTargetList(res.data.rows);
};
const handleSubmit = (values: any) => {
if (values.min_score > values.max_score) {
return message.error("分数范围输入错误");
}
values.targetName = targetList.find(
(item) => item.id == values.targetId
)?.name;
if (id) {
projectUpdateApi({ ...values, id }).then((res) => {
if (res.code == 200) {
onClose();
message.success("修改成功");
}
});
} else {
projectAddApi(values).then((res) => {
if (res.code == 200) {
onClose();
message.success("新增成功");
}
});
}
};
// const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// const { value: inputValue } = e.target;
// console.log(inputValue);
// const reg = /^-?\d*(\.\d*)?$/;
// if (reg.test(inputValue) || inputValue === "" || inputValue === "-") {
// return 8;
// }
// };
const handleFinishFailed = () => {
message.warning("请按照正确格式填写信息!");
};
return (
<Drawer
title={`${id ? "修改" : "新增"}信息`}
width={600}
open={visible}
onClose={onClose}
footer={null}
destroyOnClose={true}
>
<Form
layout="horizontal"
colon
form={form}
initialValues={{ type: 1 }}
labelAlign="left"
{...formItemLayout}
onFinish={handleSubmit}
onFinishFailed={handleFinishFailed}
>
<Form.Item
label="奖惩表类型"
name="type"
rules={[
{
required: true,
message: "请选择奖惩表类型!",
},
]}
>
<Select
style={{ width: 300 }}
options={TargetType}
onChange={changeTarget}
fieldNames={{ label: "name", value: "id" }}
/>
</Form.Item>
<Form.Item
label="所属指标"
name="targetId"
rules={[
{
required: true,
message: "请选择所属指标!",
},
]}
>
<Select
style={{ width: 300 }}
options={targetList}
fieldNames={{ label: "name", value: "id" }}
/>
</Form.Item>
<Form.Item
label="奖惩分类项"
name="projectName"
rules={[
{
required: true,
message: "请输入奖惩分类项!",
},
]}
>
<Input placeholder="请输入奖惩分类项" />
</Form.Item>
<Form.Item
label="分类描述"
name="projectDesc"
rules={[
{
required: true,
message: "请输入分类描述!",
},
]}
>
<Input placeholder="请输入分类描述" />
</Form.Item>
{/* <Form.Item
label="分数"
name="min_score"
rules={[
{
required: true,
message: "请输入分数!",
},
() => ({
validator(_, value) {
if (/^-?\d*(\.\d*)?$/.test(value)) {
return Promise.resolve();
}
return Promise.reject(new Error("请输入数字"));
},
}),
]}
>
<Input placeholder="请输入分数" />
</Form.Item> */}
<Form.Item label="分数范围">
<Space align="start">
<Form.Item
name="min_score"
rules={[
{
required: true,
message: "请输入分数最小值",
},
// () => ({
// validator(_, value) {
// if (value < form.getFieldValue("max_score")) {
// return Promise.resolve();
// }
// return Promise.reject(new Error("请输入正确的最小值"));
// },
// }),
]}
>
<InputNumber />
</Form.Item>
{"-"}
<Form.Item
name="max_score"
rules={[
{
required: true,
message: "请输入分数最大值",
},
// () => ({
// validator(_, value) {
// if (value > form.getFieldValue("min_score")) {
// return Promise.resolve();
// }
// return Promise.reject(new Error("请输入正确的最大值"));
// },
// }),
]}
>
<InputNumber />
</Form.Item>
</Space>
</Form.Item>
<Form.Item wrapperCol={{ offset: 8 }}>
<Button onClick={onClose}>取消</Button>
<Button
type="primary"
htmlType="submit"
style={{ marginLeft: "20px" }}
>
确认
</Button>
</Form.Item>
</Form>
</Drawer>
);
};
export default Edit;