CondLaddersTable.tsx
8.14 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import React, { useState, useEffect } from "react";
import { Table, Input, InputNumber, Popconfirm, Form, Typography, Button, message, Space, Divider, Modal } from "antd";
import { cloneDeep } from "lodash";
interface Item {
key: string;
lower: number;
upper: number;
scorePercent: number;
}
interface Props {
value?: any[];
onChange?: Function;
readOnly?: boolean;
visible?: boolean;
type?: number; //2 查看得分阶梯
setladderVisible?: Function;
}
interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
editing: boolean;
dataIndex: string;
title: any;
inputType: "number" | "text";
record: Item;
index: number;
children: React.ReactNode;
}
const EditableCell: React.FC<EditableCellProps> = ({
editing,
dataIndex,
title,
inputType,
record,
index,
children,
...restProps
}) => {
const inputNode =
inputType === "number" ? <InputNumber precision={dataIndex === "upper" ? 2 : 0} min={0} /> : <Input />;
return (
<td {...restProps}>
{editing ? (
<Form.Item
name={dataIndex}
style={{ margin: 0 }}
rules={
[
// {
// required: dataIndex !== "upper",
// message: `请输入${title}!`,
// },
// {
// pattern: dataIndex == "upper" ? /^[1-9]\d*$/ : undefined,
// message: "请输入大于0的正整数",
// },
]
}
>
{inputNode}
</Form.Item>
) : (
children
)}
</td>
);
};
const LadderTable = ({ value, onChange, readOnly, visible, type, setladderVisible }: Props) => {
const [form] = Form.useForm();
// 添加阶梯
const [disable, setDisable] = useState(false);
const [editingKey, setEditingKey] = useState("");
const isEditing = (record: Item) => record.key === editingKey;
const edit = (record: Partial<Item> & { key: React.Key }, index: number) => {
form.setFieldsValue({
scorePercent: "",
address: "",
...record,
upper: record.upper == 65536 ? "" : record.upper,
});
// console.log(record);
record.key = index.toString();
setEditingKey(record.key);
};
const cancel = () => {
setEditingKey("");
};
// 添加阶梯
const onAdd = (values: any[], index: number) => {
const preTableData: any[] = [...values.map((i) => ({ ...i }))];
// 编辑不是最后一行,不需要增加阶梯
if (index !== preTableData.length - 1) {
return preTableData;
}
// 如果最后一行没有输入最大值,不需要增加阶梯
if (index === preTableData.length - 1) {
const endData = preTableData[index];
if (!endData.upper) {
return preTableData;
}
}
const newObj: { lower?: number; key?: number } = {};
const lastData = preTableData[preTableData.length - 1];
newObj.lower = lastData.upper;
newObj.key = Number(lastData.key) + 1;
const pa = {
...newObj,
key: newObj.key?.toString(),
scorePercent: 0,
};
preTableData.push(pa);
return preTableData;
};
const checkRange = async (tempData, index) => {
const res = [...tempData.map((i) => ({ ...i }))];
const _tempData = res.concat();
for (let i = 0; i < _tempData.length; i++) {
const item = res[i];
if (item.upper && item.lower && item.lower >= item.upper) {
item.upper = item.lower + 1;
}
if (i >= index && i < res.length - 1) {
res[i + 1].lower = item.upper;
}
}
return res;
};
const save = async (key: React.Key, record: Item) => {
try {
const row = (await form.validateFields()) as Item;
let newData = [...value.map((i) => ({ ...i }))];
const index = newData.findIndex((item) => key === item.key);
const tempData = [...newData].map((i) => (i.key == key ? { ...record, ...row } : { ...i }));
const res = await checkRange([...tempData], index);
const addRow = onAdd([...res], index);
if (index > -1) {
const item = addRow[index];
addRow.splice(index, 1, {
...row,
...item,
});
onChange && onChange([...addRow.map((i) => ({ ...i }))]);
setEditingKey("");
} else {
addRow.push(row);
onChange && onChange([...addRow.map((i) => ({ ...i }))]);
setEditingKey("");
}
} catch (errInfo) {
console.log("Validate Failed:", errInfo);
}
};
// 删除阶梯区间
const _onDelete = (record: Item, index: number) => {
const tmpData = [...(value || [])];
const res = tmpData.filter((item) => item.key !== record.key);
if (index > 0 && index <= res.length - 1) {
res[index].lower = res[index - 1].upper;
}
//校准区间
res.forEach((item, ind) => {
if (ind === 0) {
res[0].lower = 0;
}
});
onChange && onChange([...res]);
};
const columns = [
{
title: "区间下限(≥)",
dataIndex: "lower",
width: "20%",
render: (value: number) => value + "%",
},
{
title: "区间上限(<)",
dataIndex: "upper",
width: "20%",
editable: true,
render: (value: number) => (value && value !== 65536 ? value + "%" : ""),
},
{
title: "绩效分折算比例",
dataIndex: "scorePercent",
width: "15%",
editable: true,
render: (value: number) => value + "%",
},
{
title: "操作",
width: "40%",
dataIndex: "operation",
render: (_: any, record: Item, index: number) => {
const editable = isEditing(record);
return editable ? (
<Space split={<Divider type="vertical" />}>
<Typography.Link onClick={() => save(record.key, record)} style={{ marginRight: 8 }}>
保存
</Typography.Link>
<Popconfirm title="确定取消?" onConfirm={cancel}>
<a>取消</a>
</Popconfirm>
</Space>
) : (
<Space split={<Divider type="vertical" />}>
<Typography.Link disabled={editingKey !== "" || readOnly} onClick={() => edit(record, index)}>
编辑
</Typography.Link>
{index !== 0 && index !== value?.length - 1 && (
<Typography.Link
disabled={editingKey !== "" || readOnly || value?.length === 1}
onClick={() => _onDelete(record, index)}
>
删除
</Typography.Link>
)}
</Space>
);
},
},
];
const getColumns = () => {
const _columns = columns;
if (readOnly) {
_columns.pop();
}
return _columns;
};
// const mergedColumns = columns.map((col) => {
const mergedColumns = getColumns().map((col) => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: (record: Item) => ({
record,
inputType: "number",
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
return (
<>
{type === 2 ? (
<Modal
title="查看阶梯"
visible={visible}
maskClosable={false}
footer={null}
width={700}
onCancel={() => setladderVisible(false)}
>
<Form form={form} component={false}>
<Table
components={{
body: {
cell: EditableCell,
},
}}
bordered
dataSource={value}
columns={mergedColumns}
rowClassName="editable-row"
pagination={{
onChange: cancel,
}}
/>
</Form>
</Modal>
) : (
<Form form={form} component={false}>
<Table
components={{
body: {
cell: EditableCell,
},
}}
bordered
dataSource={value}
columns={mergedColumns}
rowClassName="editable-row"
pagination={{
onChange: cancel,
}}
/>
</Form>
)}
</>
);
};
export default LadderTable;