form-driver 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,38 +1,42 @@
1
-
2
1
  import _ from "lodash";
3
- import { Button, message, Popconfirm } from 'antd';
4
- import { CaretDownOutlined, CaretUpOutlined, CloseOutlined } from '@ant-design/icons';
5
- import { BaseViewer } from '../../BaseViewer';
2
+ import { Button, message, Popconfirm } from "antd";
3
+ import {
4
+ CaretDownOutlined,
5
+ CaretUpOutlined,
6
+ CloseOutlined,
7
+ } from "@ant-design/icons";
8
+ import { BaseViewer } from "../../BaseViewer";
6
9
  import { MUtil } from "../../../framework/MUtil";
7
10
  import { MFieldViewer } from "../../../framework/MFieldViewer";
8
11
  import React from "react";
9
- import { assembly } from '../../../framework/Assembly';
12
+ import { assembly } from "../../../framework/Assembly";
13
+ import EnhancedSortDrag, { DragItem } from "../../widget/EnhancedSortDrag";
10
14
 
11
15
  function uuid(len = 8, radix = 16) {
12
- let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
16
+ let chars =
17
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
13
18
  let uuid = [];
14
19
  let i = 0;
15
20
  radix = radix || chars.length;
16
21
 
17
22
  if (len) {
18
- for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
23
+ for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
19
24
  } else {
20
- let r;
21
- uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
22
- uuid[14] = '4';
23
-
24
- for (i = 0; i < 36; i++) {
25
- if (!uuid[i]) {
26
- r = 0 | Math.random() * 16;
27
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
28
- }
25
+ let r;
26
+ uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
27
+ uuid[14] = "4";
28
+
29
+ for (i = 0; i < 36; i++) {
30
+ if (!uuid[i]) {
31
+ r = 0 | (Math.random() * 16);
32
+ uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
29
33
  }
34
+ }
30
35
  }
31
36
 
32
- return uuid.join('');
37
+ return uuid.join("");
33
38
  }
34
39
 
35
-
36
40
  /**
37
41
  * 数据表格
38
42
  * 数据是这样的数组:
@@ -50,17 +54,27 @@ export class AArrayGrid extends BaseViewer {
50
54
  return MUtil.error("arrayMember未定义", schema);
51
55
  }
52
56
 
53
- const members = schema.arrayMember.objectFields // 成员是复杂结构
54
- || [{ name: undefined, ...schema.arrayMember }]; // 成员是简单结构
57
+ const members = schema.arrayMember.objectFields || [
58
+ // 成员是复杂结构
59
+ { name: undefined, ...schema.arrayMember },
60
+ ]; // 成员是简单结构
55
61
  // if(!members) {
56
62
  // return MUtil.error("AArrayGrid只适用于对象数组", schema);
57
63
  // }
58
64
 
59
65
  let data = super.getValue();
60
- if (!_.isArray(data)) { // 只接受数组
66
+ if (!_.isArray(data)) {
67
+ // 只接受数组
61
68
  data = [];
62
69
  }
63
70
 
71
+ data = data.map((d) => {
72
+ if (!d.uniqueId) {
73
+ d.uniqueId = uuid();
74
+ }
75
+ return d;
76
+ });
77
+
64
78
  const cols = 1 + members.length;
65
79
 
66
80
  //let headTh = [<td key=":操作栏" width="40px" align="center" style={{backgroundImage: "linear-gradient(to bottom left, transparent calc(50% - 1px), #d3d3d3, transparent calc(50% + 1px))"}}></td>]
@@ -68,90 +82,204 @@ export class AArrayGrid extends BaseViewer {
68
82
  let rows = [];
69
83
  for (let idx = 0; idx < data.length; idx++) {
70
84
  const i = idx;
71
- rows.push(<tr key={i}>
72
- {/* 各个字段 */}
73
- {
74
- members.map((f, idx) =>
85
+ rows.push(
86
+ <tr key={i}>
87
+ {/* 各个字段 */}
88
+ {members.map((f, idx) => (
75
89
  <td key={f.name + idx}>
76
- <MFieldViewer key={this.state.ctrlVersion + "." + f.name} parent={schema} morph={this.props.morph} schema={f} database={data} path={MUtil.jsonPath("[" + i + "]", f.name)} hideBorder={true} afterChange={(path, v, final): void => {
77
- super.changeValueEx(data, false, final);
78
- }} />
79
- </td>)
80
- }
81
-
82
- {/* 操作栏 */}
83
- <td key=":option" align="center">
84
- <CaretUpOutlined style={{ display: "block" }} hidden={data.length <= 1} onClick={() => {
85
- if (i === 0) {
86
- message.warn("已经到顶了");
87
- } else {
88
- const prev = data[i - 1];
89
- data[i - 1] = data[i];
90
- data[i] = prev;
91
- super.changeValueEx(data, true, true);
92
- }
93
- }} />
94
- <Popconfirm
95
- title="确定要删除吗这一项吗?"
96
- onConfirm={() => {
97
- data.splice(i, 1);
98
- super.changeValueEx(data, true, true);
99
- }}
100
- okText="删除"
101
- cancelText="不删">
102
- <CloseOutlined style={{ display: "block" }} hidden={data.length == (schema.min ?? 0)} />
103
- </Popconfirm>
104
- <CaretDownOutlined style={{ display: "block" }} hidden={data.length <= 1} onClick={() => {
105
- if (i === data.length - 1) {
106
- message.warn("已经到底了");
107
- } else {
108
- const prev = data[i + 1];
109
- data[i + 1] = data[i];
110
- data[i] = prev;
111
- super.changeValueEx(data, true, true);
112
- }
113
- }} />
114
- </td>
115
- </tr>);
90
+ <MFieldViewer
91
+ key={this.state.ctrlVersion + "." + f.name}
92
+ parent={schema}
93
+ morph={this.props.morph}
94
+ schema={f}
95
+ database={data}
96
+ path={MUtil.jsonPath("[" + i + "]", f.name)}
97
+ hideBorder={true}
98
+ afterChange={(path, v, final): void => {
99
+ super.changeValueEx(data, false, final);
100
+ }}
101
+ />
102
+ </td>
103
+ ))}
104
+
105
+ {/* 操作栏 */}
106
+ <td key=":option" align="center">
107
+ <CaretUpOutlined
108
+ style={{ display: "block" }}
109
+ hidden={data.length <= 1}
110
+ onClick={() => {
111
+ if (i === 0) {
112
+ message.warn("已经到顶了");
113
+ } else {
114
+ const prev = data[i - 1];
115
+ data[i - 1] = data[i];
116
+ data[i] = prev;
117
+ super.changeValueEx(data, true, true);
118
+ }
119
+ }}
120
+ />
121
+ <Popconfirm
122
+ title="确定要删除吗这一项吗?"
123
+ onConfirm={() => {
124
+ data.splice(i, 1);
125
+ super.changeValueEx(data, true, true);
126
+ }}
127
+ okText="删除"
128
+ cancelText="不删"
129
+ >
130
+ <CloseOutlined
131
+ style={{ display: "block" }}
132
+ hidden={data.length == (schema.min ?? 0)}
133
+ />
134
+ </Popconfirm>
135
+ <CaretDownOutlined
136
+ style={{ display: "block" }}
137
+ hidden={data.length <= 1}
138
+ onClick={() => {
139
+ console.log("当前选择数据", data);
140
+ if (i === data.length - 1) {
141
+ message.warn("已经到底了");
142
+ } else {
143
+ const prev = data[i + 1];
144
+ data[i + 1] = data[i];
145
+ data[i] = prev;
146
+ super.changeValueEx(data, true, true);
147
+ }
148
+ }}
149
+ />
150
+ </td>
151
+ </tr>
152
+ );
116
153
  }
117
154
 
118
155
  const isMax = data.length >= (schema.max ?? Number.MAX_VALUE);
156
+
157
+ // 处理拖拽后的数据变化
158
+ const handleDragChange = (newItems: DragItem[]) => {
159
+ // 根据拖拽后的新顺序,重新排列原始表格数据
160
+ const newData = newItems.map((item) => {
161
+ // 从原始数据中找到对应的行数据
162
+ const originalRow = data.find(
163
+ (row) => String(row.uniqueId) === item.id
164
+ );
165
+ // 正常情况下一定能找到对应的行数据,因为我们是基于原始数据创建的拖拽项
166
+ return { ...originalRow! }; // 使用 ! 断言,因为我们确信能找到匹配项
167
+ });
168
+
169
+ super.changeValueEx(newData, true, true);
170
+ };
171
+
172
+ const handleDragFail = () => {};
173
+
119
174
  return (
120
- <table key={this.props.path} className="AExperience M3_table" style={{ width: "100%" }}><tbody>
121
- <tr key=":header">
122
- {members.map((f, i) => <th key={f.name + i + ":first"}>{f.required ? <span style={{ color: "red" }}>*</span> : null}{f.label ?? f.name}</th>)}
123
- <td key=":操作栏" width="40px" align="center"></td>
124
- </tr>
125
- {rows}
126
- <tr key=":footer">
127
- {/* 增加按钮 */}
128
- <th key=":add" colSpan={cols}>
129
- <Button disabled={isMax} key=":add" onClick={() => {
130
- let newItem = assembly.types[schema.arrayMember.type]?.createDefaultValue(assembly, schema.arrayMember)
131
- {/* 新增时支持要带入上一项的数据 */}
132
- if (schema.arrayMember.copyFields && data.length > 0) {
133
- const last = data[data.length - 1]
134
- if (last) {
135
- newItem = {}
136
- schema.arrayMember.copyFields.forEach(item => {
137
- newItem[item] = last[item]
138
- })
139
- }
140
- }
141
- data.push(newItem);
142
- if (schema.autoValue) {
143
- // 自动增加 value 属性
144
- data.forEach(element => {
145
- if (!element.value) element.value = uuid()
146
- });
147
- }
148
- console.log('data', data)
149
- super.changeValue(data);
150
- }}>增加一项</Button>
151
- {this.props.extra}
152
- </th>
153
- </tr>
154
- </tbody></table>
155
- )
175
+ <table
176
+ key={this.props.path}
177
+ className="AExperience M3_table"
178
+ style={{ width: "100%" }}
179
+ >
180
+ <tbody>
181
+ <tr key=":header">
182
+ <th key={"拖拽"}></th>
183
+ {members.map((f, i) => (
184
+ <th key={f.name + i + ":first"}>
185
+ {f.required ? <span style={{ color: "red" }}>*</span> : null}
186
+ {f.label ?? f.name}
187
+ </th>
188
+ ))}
189
+ <td key=":操作栏" width="40px" align="center"></td>
190
+ </tr>
191
+ <EnhancedSortDrag
192
+ items={data.map((row, index) => {
193
+ return {
194
+ id: String(row.uniqueId),
195
+ cpn: (
196
+ <>
197
+ {members.map((f, idx) => (
198
+ <td key={f.name + idx}>
199
+ <MFieldViewer
200
+ key={this.state.ctrlVersion + "." + f.name}
201
+ parent={schema}
202
+ morph={this.props.morph}
203
+ schema={f}
204
+ database={data}
205
+ path={MUtil.jsonPath("[" + index + "]", f.name)}
206
+ hideBorder={true}
207
+ afterChange={(path, v, final): void => {
208
+ super.changeValueEx(data, false, final);
209
+ }}
210
+ />
211
+ </td>
212
+ ))}
213
+
214
+ <Popconfirm
215
+ title="确定要删除吗这一项吗?"
216
+ onConfirm={() => {
217
+ data.splice(index, 1);
218
+ super.changeValueEx(data, true, true);
219
+ }}
220
+ okText="删除"
221
+ cancelText="不删"
222
+ >
223
+ <td>
224
+ <CloseOutlined
225
+ style={{ display: "block" }}
226
+ hidden={data.length == (schema.min ?? 0)}
227
+ />
228
+ </td>
229
+ </Popconfirm>
230
+ </>
231
+ ),
232
+ isChecked: true,
233
+ label: `${row.name}`,
234
+ checkedIndex: index,
235
+ };
236
+ })}
237
+ onChange={handleDragChange}
238
+ enableAnimation={true}
239
+ isTableRow={true}
240
+ onDragFail={handleDragFail}
241
+ />
242
+ {/* {rows} */}
243
+ <tr key=":footer">
244
+ {/* 增加按钮 */}
245
+ <th key=":add" colSpan={cols}>
246
+ <Button
247
+ disabled={isMax}
248
+ key=":add"
249
+ onClick={() => {
250
+ let newItem = assembly.types[
251
+ schema.arrayMember.type
252
+ ]?.createDefaultValue(assembly, schema.arrayMember);
253
+ {
254
+ /* 新增时支持要带入上一项的数据 */
255
+ }
256
+ if (schema.arrayMember.copyFields && data.length > 0) {
257
+ const last = data[data.length - 1];
258
+ if (last) {
259
+ newItem = {};
260
+ schema.arrayMember.copyFields.forEach((item) => {
261
+ newItem[item] = last[item];
262
+ });
263
+ }
264
+ }
265
+ data.push(newItem);
266
+ if (schema.autoValue) {
267
+ // 自动增加 value 属性
268
+ data.forEach((element) => {
269
+ if (!element.value) element.value = uuid();
270
+ });
271
+ }
272
+ console.log("data", data);
273
+ super.changeValue(data);
274
+ }}
275
+ >
276
+ 增加一项
277
+ </Button>
278
+ {this.props.extra}
279
+ </th>
280
+ </tr>
281
+ </tbody>
282
+ </table>
283
+ );
156
284
  }
157
285
  }
@@ -95,7 +95,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
95
95
  snapshot?: any
96
96
  ): void {
97
97
  setTimeout(() => {
98
- console.log("DRAG: 组件更新", this.checkFields, this.state.data);
98
+ // console.log("DRAG: 组件更新", this.checkFields, this.state.data);
99
99
  }, 2000);
100
100
  }
101
101
 
@@ -150,7 +150,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
150
150
  this.props.schema,
151
151
  true
152
152
  );
153
- console.log("当前选中的数据ccc", currentCheckValue);
153
+ // console.log("当前选中的数据ccc", currentCheckValue);
154
154
  this.dataRef = currentCheckValue;
155
155
  setTimeout(() => {
156
156
  super.changeValue(currentCheckValue);
@@ -165,7 +165,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
165
165
  </span>
166
166
  <span
167
167
  onBlurCapture={(e) => {
168
- console.log("输入框失去焦点", this.dataRef);
168
+ // console.log("输入框失去焦点", this.dataRef);
169
169
  setTimeout(() => {
170
170
  super.changeValue(this.dataRef);
171
171
  this.setState({
@@ -183,7 +183,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
183
183
  const matchEnum = this.checkFields.find(
184
184
  (e) => e.value === str && e.remark !== "openOption"
185
185
  );
186
- console.log("输入框 afterChange", str, values, matchEnum);
186
+ // console.log("输入框 afterChange", str, values, matchEnum);
187
187
  if (matchEnum) {
188
188
  // 不能让用户输入某个枚举值
189
189
  this._inputBoxValue = "";
@@ -212,11 +212,11 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
212
212
  this.checkFields = this.checkFields.map((e) =>
213
213
  e.remark === "openOption" ? { ...e, value: str } : e
214
214
  );
215
- console.log("输入框数据", {
216
- values,
217
- checkFields: this.checkFields,
218
- dataRef: this.dataRef,
219
- });
215
+ // console.log("输入框数据", {
216
+ // values,
217
+ // checkFields: this.checkFields,
218
+ // dataRef: this.dataRef,
219
+ // });
220
220
  MUtil.set(this.props.database, this.props.path, values);
221
221
  }
222
222
  }
@@ -246,7 +246,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
246
246
  this.props.schema,
247
247
  true
248
248
  );
249
- console.log("当前变化的 value", values, currentCheckValue);
249
+ // console.log("当前变化的 value", values, currentCheckValue);
250
250
  const max = this.props.schema.max;
251
251
  if (max > 0 && e.target.checked) {
252
252
  const len = values ? values.length : 0;
@@ -279,7 +279,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
279
279
 
280
280
  // 定义更换数据源的方法
281
281
  const changeOriginDataSource = (newData) => {
282
- console.log("新数据", newData);
282
+ // console.log("新数据", newData);
283
283
  // 更新排序后的选项数据
284
284
  const sortedCheckFields = newData.map((item) => ({
285
285
  ...item,
@@ -296,7 +296,7 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
296
296
  const isHaveOpenOption = sortedCheckFields.filter(
297
297
  (e) => e.remark === "openOption"
298
298
  );
299
- console.log("isHaveOpenOption", isHaveOpenOption);
299
+ // console.log("isHaveOpenOption", isHaveOpenOption);
300
300
  newSchema.option = isHaveOpenOption
301
301
  ? sortedCheckFields?.filter((e) => e.remark !== "openOption")
302
302
  : sortedCheckFields;
@@ -319,37 +319,43 @@ export class ACheckDrag extends Viewer<ACheckDragState> {
319
319
  }, 0);
320
320
  };
321
321
 
322
+ const sortList = (checkboxs ?? [])?.map((cpn, index) => {
323
+ let checkFieldsValue;
324
+ checkFieldsValue = this.checkFields[index]?.value;
325
+ const isOpenOp = this.checkFields[index]?.remark === "openOption";
326
+ if (isOpenOp) {
327
+ checkFieldsValue = this._inputBoxValue;
328
+ }
329
+ // console.log("DRAG: 实际传递进如 SortDrag的数据", {
330
+ // data,
331
+ // dataRef: this.dataRef,
332
+ // checkFields: this.checkFields,
333
+ // schema: this.props.database,
334
+ // isOpenOp,
335
+ // openValue: this._inputBoxValue,
336
+ // });
337
+
338
+ return {
339
+ isChecked: this.dataRef
340
+ ? this.dataRef?.findIndex((e) => e === checkFieldsValue) !== -1
341
+ : false,
342
+ checkedIndex:
343
+ this.dataRef?.findIndex((e) => e === checkFieldsValue) + 1,
344
+ cpn,
345
+ id: "" + checkFieldsValue,
346
+ label: this.checkFields[index]?.label,
347
+ remark: this.checkFields[index]?.remark,
348
+ };
349
+ });
350
+ const finalSortList = sortList
351
+ .filter((item) => item.isChecked)
352
+ .sort((a, b) => a.checkedIndex - b.checkedIndex)
353
+ .concat(sortList.filter((item) => !item.isChecked));
322
354
  return (
323
355
  <SortDrag
356
+ key={MUtil.unique()}
324
357
  changeOriginDataSource={changeOriginDataSource}
325
- sortList={(checkboxs ?? [])?.map((cpn, index) => {
326
- let checkFieldsValue;
327
- checkFieldsValue = this.checkFields[index]?.value;
328
- const isOpenOp = this.checkFields[index]?.remark === "openOption";
329
- if (isOpenOp) {
330
- checkFieldsValue = this._inputBoxValue;
331
- }
332
- console.log("DRAG: 实际传递进如 SortDrag的数据", {
333
- data,
334
- dataRef: this.dataRef,
335
- checkFields: this.checkFields,
336
- schema: this.props.database,
337
- isOpenOp,
338
- openValue: this._inputBoxValue,
339
- });
340
-
341
- return {
342
- isChecked: this.dataRef
343
- ? this.dataRef?.findIndex((e) => e === checkFieldsValue) !== -1
344
- : false,
345
- checkedIndex:
346
- this.dataRef?.findIndex((e) => e === checkFieldsValue) + 1,
347
- cpn,
348
- id: "" + checkFieldsValue,
349
- label: this.checkFields[index]?.label,
350
- remark: this.checkFields[index]?.remark,
351
- };
352
- })}
358
+ sortList={finalSortList}
353
359
  />
354
360
  );
355
361
  }