@hzab/edit-table 0.0.2-alpha.0 → 0.1.0

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # @hzab/edit-table@0.0.2
2
+
3
+ feat: 组件优化,批量提交
4
+
1
5
  # @hzab/edit-table@0.0.1
2
6
 
3
7
  组件初始化
package/README.md CHANGED
@@ -18,11 +18,24 @@ import Demo from "@hzab/edit-table";
18
18
 
19
19
  ## API
20
20
 
21
- ### InfoPanel Attributes
21
+ #### cellEditTableProps
22
22
 
23
- | 参数 | 类型 | 必填 | 默认值 | 说明 |
24
- | ------ | ------ | ---- | ------ | ----------------- |
25
- | schema | Object | 是 | - | 数据信息的 schema |
23
+ | 属性名称 | 属性类型 | 必须 | 默认值 | 描述 |
24
+ | --------------- | -------- | ---- | ------ | ------------------ |
25
+ | reactGridProps | Object | 否 | | reactGrid 相关参数 |
26
+ | onChange | Function | 否 | | 单元格回调 |
27
+ | formulaConfig | Object | 否 | | 运算相关配置 |
28
+ | headerRowHeight | number | 否 | | 表头高度 |
29
+ | rowHeight | number | 否 | | 数据行高度 |
30
+
31
+ #### reactGridProps
32
+
33
+ | 属性名称 | 属性类型 | 必须 | 默认值 | 描述 |
34
+ | enableFillHandle | boolean | false | 开启后,选中单元格右下角会出现小方块,可拖拽快速复制 / 序列填充数据(类似 Excel 拖拽填充)|
35
+ | enableRangeSelection | boolean | false | 开启框选单元格区域(批量选中、批量编辑 / 填充必备)|
36
+ | onFocusLocationChanging | (location: CellLocation): boolean | 否 | 焦点 / 进入编辑态前回调,return false 阻止进入编辑)|
37
+ | customCellTemplates | Function | 否 | 注册自定义单元格模板(按钮、下拉、日期等单元格)|
38
+ | onSelectionChanged | Function | 否 | 选择区回调 |
26
39
 
27
40
  # 组件开发流程
28
41
 
@@ -39,6 +52,7 @@ import Demo from "@hzab/edit-table";
39
52
  ## 命令
40
53
 
41
54
  - Mac 执行该命令,设置 pre-commit 为可执行文件
55
+
42
56
  - npm run mac-chmod
43
57
  - chmod +x .husky && chmod +x .husky/pre-commit
44
58
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/edit-table",
3
- "version": "0.0.2-alpha.0",
3
+ "version": "0.1.0",
4
4
  "description": "",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -41,7 +41,10 @@
41
41
  "react-router-dom": "6.14.1",
42
42
  "typedoc": "0.24.8",
43
43
  "typescript": "4.9.4",
44
- "webpack": "5.75.0"
44
+ "webpack": "5.75.0",
45
+ "@hzab/form-render": "^1.7.17",
46
+ "@hzab/list-render": "^1.11.0",
47
+ "@silevis/reactgrid": "^4.1.17"
45
48
  },
46
49
  "directories": {
47
50
  "src": "src"
@@ -51,9 +54,5 @@
51
54
  "prettier --write"
52
55
  ]
53
56
  },
54
- "dependencies": {
55
- "@hzab/form-render": "^1.7.17",
56
- "@hzab/list-render": "^1.11.0",
57
- "@silevis/reactgrid": "^4.1.17"
58
- }
57
+ "dependencies": {}
59
58
  }
@@ -1,5 +1,5 @@
1
- import { MinusCircleOutlined, MinusSquareOutlined, PlusCircleOutlined } from "@ant-design/icons";
2
1
  import { useEffect, useState } from "react";
2
+ import { MinusCircleOutlined, PlusCircleOutlined } from "@ant-design/icons";
3
3
  import "./index.less";
4
4
 
5
5
  export const ExpandListTemplate = (props) => {
@@ -29,10 +29,6 @@ export const ExpandListTemplate = (props) => {
29
29
  }
30
30
  }, [cell?.record]);
31
31
 
32
- const colRender = function (record) {
33
- const Slot = Slots[columnId];
34
- return <Slot record={record} />;
35
- };
36
32
  /** 展开关闭子项 */
37
33
  const onClickExpand = () => {
38
34
  setExpandKeys((preExpandKeys) => {
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useState } from "react";
1
+ import { useEffect, useMemo, useRef, useState } from "react";
2
2
 
3
3
  import {
4
4
  Cell,
@@ -12,7 +12,6 @@ import {
12
12
  Uncertain,
13
13
  UncertainCompatible,
14
14
  } from "@silevis/reactgrid";
15
- import { getVal } from "@hzab/list-render/src/common/utils";
16
15
  import { InfoCircleOutlined } from "@ant-design/icons";
17
16
  import { Popover } from "antd";
18
17
 
@@ -108,8 +107,10 @@ export const WidthWrapHoc = (WrapComponent: any) => {
108
107
  const [isOpen, setIsOpen] = useState<boolean>(false);
109
108
  const datatypeId = cell?.columnId + cell?.rowId;
110
109
  const [comValue, setComValue] = useState(cell?.text);
110
+ const comValueInit = useRef<any>(cell?.text);
111
111
  useEffect(() => {
112
112
  setComValue(cell?.text);
113
+ comValueInit.current = cell?.text;
113
114
  }, [cell?.text]);
114
115
 
115
116
  useEffect(() => {
@@ -124,21 +125,12 @@ export const WidthWrapHoc = (WrapComponent: any) => {
124
125
  document.removeEventListener("click", onClick);
125
126
  };
126
127
  }, []);
127
- const echoValue = useMemo(() => {
128
- if (!cell?.getField) {
129
- return;
130
- }
131
- return getVal(
132
- { ...cell?.getField(), enum: cell?.getField()?.enum || cell?.values },
133
- { [cell?.getField().name]: cell.text },
134
- );
135
- }, [cell]);
136
128
 
137
129
  const isValidator = useMemo(() => {
138
130
  let flag = false;
139
131
  let isVerify = false;
140
132
  for (let it of cell?.validatorList || []) {
141
- if (it?.pattern?.test(cell.text)) {
133
+ if (it?.pattern?.test(comValue)) {
142
134
  flag = true;
143
135
  }
144
136
  break;
@@ -146,7 +138,7 @@ export const WidthWrapHoc = (WrapComponent: any) => {
146
138
  isVerify = !flag && cell?.validatorList?.length > 0;
147
139
 
148
140
  return isVerify;
149
- }, [cell?.validatorList, cell.text]);
141
+ }, [cell?.validatorList, comValue]);
150
142
 
151
143
  return (
152
144
  <div
@@ -155,7 +147,6 @@ export const WidthWrapHoc = (WrapComponent: any) => {
155
147
  if (cell?.nonEditable) {
156
148
  return;
157
149
  }
158
- (window as any).preCell = cell;
159
150
  setIsOpen(true);
160
151
  }}
161
152
  >
@@ -168,6 +159,8 @@ export const WidthWrapHoc = (WrapComponent: any) => {
168
159
  cell["commit"] = "change";
169
160
  const targetValue = e?.target?.value ?? e;
170
161
  setComValue(targetValue);
162
+ comValueInit.current = targetValue;
163
+
171
164
  onCellChanged(
172
165
  this.getCompatibleCell({
173
166
  ...cell,
@@ -182,7 +175,7 @@ export const WidthWrapHoc = (WrapComponent: any) => {
182
175
  if (isValidator) {
183
176
  cell["commit"] = "verify";
184
177
  }
185
- const targetValue = cell?.text;
178
+ const targetValue = comValueInit.current;
186
179
  onCellChanged(
187
180
  this.getCompatibleCell({
188
181
  ...cell,
@@ -206,8 +199,8 @@ export const WidthWrapHoc = (WrapComponent: any) => {
206
199
  cell={cell}
207
200
  ></WrapComponent>
208
201
  {isValidator && (
209
- <Popover content="该字段不合法">
210
- <InfoCircleOutlined style={{ cursor: "pointer" }} />
202
+ <Popover content={cell?.validatorMessage ?? "该字段不合法"}>
203
+ <InfoCircleOutlined className="validator-icon" />
211
204
  </Popover>
212
205
  )}
213
206
  </div>
package/src/index.less CHANGED
@@ -13,14 +13,6 @@
13
13
  }
14
14
 
15
15
 
16
- // .reactgrid-content .rg-pane .rg-cell {
17
- // padding: 0;
18
-
19
- // >div:nth-child(1) {
20
- // padding: 0 4px;
21
- // }
22
- // }
23
-
24
16
  .rg-header-cell,
25
17
  .rg-cell {
26
18
  display: flex;
@@ -35,4 +27,23 @@
35
27
  display: flex;
36
28
  align-items: center;
37
29
  justify-content: center;
30
+ }
31
+
32
+ .validator-icon {
33
+ position: relative;
34
+ background: #fff;
35
+ cursor: pointer;
36
+ }
37
+
38
+ .rg-rowSelection-cell {
39
+ label {
40
+ justify-content: center;
41
+ }
42
+ }
43
+
44
+ .grid-container-empty {
45
+ height: 100%;
46
+ display: flex;
47
+ align-items: center;
48
+ justify-content: center;
38
49
  }
package/src/index.tsx CHANGED
@@ -1,10 +1,10 @@
1
1
  import { useEffect, useMemo, useRef, useState } from "react";
2
+ import { Checkbox, Empty, Spin } from "antd";
2
3
  import { ReactGrid } from "@silevis/reactgrid";
3
4
  import { TextTemplate } from "./Template/TextCellTemplate";
4
5
  import { antdComponents, customComponents } from "@hzab/form-render/src/index";
5
6
  import { WidthWrapHoc } from "./common/WidthWrapHoc";
6
7
  import { NumberTemplate } from "./Template/NumberCellTemplate";
7
- import { Checkbox, Spin } from "antd";
8
8
  import { ExpandListTemplate } from "./Template/ExpandListTemplate";
9
9
  import { convertColumnsToRows, flattenColumns, isReactElement } from "./common/utils";
10
10
  import { childAntdComponent } from "./common/childAntdComponent";
@@ -59,6 +59,7 @@ export default function CellEditTable(props: CellEditTableProps) {
59
59
  const [rows, setRows] = useState<any>([]);
60
60
  /** 初始行数据 */
61
61
  const initRowsRef = useRef<any>([]);
62
+ const firstRender = useRef(true);
62
63
  const [tableColumns, setTableColumns] = useState<any>([]);
63
64
  const [selectedRowKeys, setSelectedRowKeys] = useState<any>([]);
64
65
  const [newDataSource, setNewDataSource] = useState<any>([]);
@@ -205,6 +206,10 @@ export default function CellEditTable(props: CellEditTableProps) {
205
206
  }),
206
207
  };
207
208
  });
209
+ if (firstRender.current) {
210
+ initRowsRef.current = [...headerList, ...tempDataSource];
211
+ firstRender.current = false;
212
+ }
208
213
  setRows([...headerList, ...tempDataSource]);
209
214
  }, [newDataSource, tableColumns, headerList]);
210
215
 
@@ -217,10 +222,11 @@ export default function CellEditTable(props: CellEditTableProps) {
217
222
  if (changes?.length > 1) {
218
223
  isBatchSubmit = true;
219
224
  }
225
+
220
226
  setRows((prevRows) => {
221
227
  changes.forEach(async (change) => {
222
- const hasDiff = JSON.stringify((window as any).preCell?.text) != JSON.stringify(change.newCell?.text);
223
- if (!hasDiff) return;
228
+ // const hasDiff = JSON.stringify( change?.newCell?.text) != JSON.stringify(change.previousCell?.text);
229
+ // if (hasDiff) return;
224
230
  const changeRowIdx = prevRows.findIndex((el) => el.rowId == change.rowId);
225
231
  const changeColumnIdx = tableColumns.findIndex((el) => el.columnId == change.columnId);
226
232
  prevRows[changeRowIdx].cells[changeColumnIdx] = { ...change.newCell };
@@ -242,6 +248,7 @@ export default function CellEditTable(props: CellEditTableProps) {
242
248
  prevRows[changeRowIdx].cells[resultColumnIdx] = newCell;
243
249
  }
244
250
  }
251
+
245
252
  if (change?.newCell?.commit == "submit" && onEditSubmit && !isBatchSubmit) {
246
253
  try {
247
254
  await onEditSubmit(batchQuery);
@@ -356,8 +363,6 @@ export default function CellEditTable(props: CellEditTableProps) {
356
363
  <ReactGrid
357
364
  ref={gridRef}
358
365
  rows={loading ? initRowsRef.current : rows}
359
- /** 兼容初始化时吸顶生效 */
360
- key={loading ? 1 : 2}
361
366
  columns={tableColumns}
362
367
  onCellsChanged={handleCellsChanged}
363
368
  enableFillHandle={true}
@@ -385,11 +390,12 @@ export default function CellEditTable(props: CellEditTableProps) {
385
390
  Input: TextTemplate,
386
391
  Number: NumberTemplate,
387
392
  }}
388
-
389
- // onSelectionChanged={(newSelection) => {
390
- // console.log("选区发生变化", newSelection[0]);
391
- // }}
392
393
  />
394
+ {dataSource?.length <= 0 && (
395
+ <div className="grid-container-empty">
396
+ <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
397
+ </div>
398
+ )}
393
399
  </div>
394
400
  </Spin>
395
401
  );