@hzab/list-render 1.10.21-alpha.6 → 1.10.21-alpha.7

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,309 +0,0 @@
1
- import React, { memo, useEffect, useMemo, useRef, useState } from "react";
2
- import { ReactGrid, Column, Row, CellChange, Cell, CellTemplate } from "@silevis/reactgrid";
3
- import "@silevis/reactgrid/styles.css";
4
- import { TextTemplate } from "./Template/TextCellTemplate";
5
- import { antdComponents, customComponents } from "@hzab/form-render/src/index";
6
- import { WidthWrapHoc } from "./common/WidthWrapHoc";
7
- import { NumberTemplate } from "./Template/NumberCellTemplate";
8
- import { Checkbox, Spin } from "antd";
9
- import { ExpandListTemplate } from "./Template/ExpandListTemplate";
10
- import { convertColumnsToRows, flattenColumns } from "./common/utils";
11
- import { childAntdComponent } from "./common/childAntdComponent";
12
- import { GetBaseTemplate } from "./Template/GetBaseTemplate";
13
- import "./index.less";
14
-
15
- export default function CellEditTable(props: any) {
16
- const {
17
- columns,
18
- dataSource,
19
- formulaConfig,
20
- onChange,
21
- reactGridProps = {},
22
- onEditSubmit,
23
- headerRowHeight,
24
- rowHeight,
25
- reactGridStyle,
26
- className,
27
- Slots,
28
- rowSelection,
29
- rowKey,
30
- query,
31
- loading,
32
- } = props;
33
-
34
- const [headerList, setHeaderList] = useState<any>([]);
35
- const [rows, setRows] = useState<any>([]);
36
- const [tableColumns, setTableColumns] = useState<any>([]);
37
- const [selectedRowKeys, setSelectedRowKeys] = useState<any>([]);
38
- const [newDataSource, setNewDataSource] = useState<any>([]);
39
- const [expandKeys, setExpandKeys] = useState<any>([]);
40
- const gridRef = useRef<any>();
41
- useEffect(() => {
42
- setNewDataSource(dataSource);
43
- }, [dataSource]);
44
- /**将数组转为对象 */
45
- const handleArrayToObj = (item) => {
46
- const record = item?.cells?.[0]?.record;
47
- const fieldList = ["_$actions", "rowSelection"];
48
- let obj: any = {
49
- id: item?.rowId,
50
- };
51
- item?.cells?.forEach((el) => {
52
- if (el?.columnId && !fieldList?.includes(el?.columnId)) {
53
- obj[el?.columnId] = el?.text || el?.value || el?.selectedValue || "";
54
- }
55
- });
56
-
57
- return {
58
- ...record,
59
- ...obj,
60
- };
61
- };
62
- /** 表头 */
63
- useEffect(() => {
64
- if (Array.isArray(columns) && columns?.length > 0) {
65
- if (rowSelection) {
66
- columns.unshift({
67
- type: "rowSelection",
68
- width: rowSelection?.width || 30,
69
- key: "rowSelection",
70
- style: rowSelection?.style || {
71
- background: "rgba(128, 128, 128, 0.1)",
72
- display: "flex",
73
- justifyContent: "center",
74
- },
75
- });
76
- }
77
- setHeaderList(convertColumnsToRows(columns, headerRowHeight));
78
- const newColumns = flattenColumns(columns);
79
- setTableColumns(
80
- newColumns?.map((item, index) => ({
81
- ...item,
82
- columnId: item?.dataIndex || item?.key || "",
83
- })),
84
- );
85
- }
86
- }, [columns, rowSelection]);
87
-
88
- /** 数据行 */
89
- useEffect(() => {
90
- const columnsIds = tableColumns?.map((el) => el?.columnId);
91
- const tempDataSource = newDataSource?.map((item, index) => {
92
- return {
93
- rowId: item[rowKey],
94
- height: rowHeight,
95
- cells: columnsIds?.map((el, ind) => {
96
- let newCell = {};
97
- const columnsItem = tableColumns[ind];
98
- const cellItem = item[el];
99
- let type: any = columnsItem?.type ?? "text";
100
- /** 是否有复选框 */
101
- if (!rowSelection && ind == 0) {
102
- type = "ExpandListTemplate";
103
- }
104
- if (rowSelection && ind == 1) {
105
- type = "ExpandListTemplate";
106
- }
107
- //设置单元格颜色及回显
108
- if (formulaConfig) {
109
- for (let key in formulaConfig) {
110
- if (key == el) {
111
- const it = formulaConfig[key];
112
- newCell = {
113
- text: it?.setCellData(item),
114
- value: it?.setCellData(item),
115
- style: it?.setCellStyle(it?.setCellData(item)),
116
- };
117
- }
118
- }
119
- }
120
-
121
- return {
122
- ...columnsItem,
123
- record: item,
124
- rowId: item[rowKey] || columnsItem?.type,
125
- type: type,
126
- nonEditable: columnsItem?.nonEditable,
127
- text: String(cellItem || ""),
128
- value: cellItem,
129
- selectedValue: cellItem || "",
130
- style: columnsItem?.type == "rowSelection" ? { display: "flex", justifyContent: "center" } : {},
131
- ...((columnsItem?.onCell && columnsItem?.onCell(item, index, ind)) || {}),
132
- ...(columnsItem?.cellProps || {}),
133
- setNewDataSource,
134
- setExpandKeys,
135
- rowKey,
136
- expandKeys,
137
- Slots,
138
- ...newCell,
139
- };
140
- }),
141
- };
142
- });
143
- setRows([...headerList, ...tempDataSource]);
144
- }, [newDataSource, tableColumns]);
145
-
146
- // 单元格编辑回调
147
- const handleCellsChanged = (changes) => {
148
- if (!Array.isArray(changes) || changes.length === 0) return;
149
- /** 批量提交 */
150
- let isBatchSubmit = false;
151
- if (changes?.length > 1) {
152
- isBatchSubmit = true;
153
- }
154
- setRows((prevRows) => {
155
- changes.forEach((change) => {
156
- const hasDiff = JSON.stringify((window as any).preCell?.text) != JSON.stringify(change.newCell?.text);
157
- if (!hasDiff) return;
158
- const changeRowIdx = prevRows.findIndex((el) => el.rowId == change.rowId);
159
- const changeColumnIdx = tableColumns.findIndex((el) => el.columnId == change.columnId);
160
- prevRows[changeRowIdx].cells[changeColumnIdx] = { ...change.newCell };
161
- if (formulaConfig) {
162
- for (let key in formulaConfig) {
163
- const item = formulaConfig[key];
164
- const resultColumnIdx = tableColumns.findIndex((el) => el.columnId == key);
165
- const newCell = {
166
- ...prevRows[changeRowIdx].cells[resultColumnIdx],
167
- text: item?.setCellData(prevRows[changeRowIdx]),
168
- value: item?.setCellData(prevRows[changeRowIdx]),
169
- style: item?.setCellStyle(item?.setCellData(prevRows[changeRowIdx])),
170
- };
171
- prevRows[changeRowIdx].cells[resultColumnIdx] = newCell;
172
- }
173
- }
174
- if (change?.newCell?.commit == "submit") {
175
- onEditSubmit && onEditSubmit(handleArrayToObj(prevRows[changeRowIdx]));
176
- }
177
- onChange && onChange(prevRows[changeRowIdx]);
178
- });
179
- return [...prevRows];
180
- });
181
- };
182
- /** 自定义组件注册 */
183
- const baseTemplate = useMemo(() => {
184
- const mergeTemplate: any = { ...(antdComponents || {}), ...(customComponents || {}), ...childAntdComponent };
185
- const newMergeTemplate: any = {};
186
- for (let key in mergeTemplate) {
187
- const NewCom = (props) => {
188
- return <GetBaseTemplate {...props} Com={mergeTemplate[key]} />;
189
- };
190
- newMergeTemplate[key] = WidthWrapHoc(NewCom);
191
- }
192
- return newMergeTemplate;
193
- }, [antdComponents, customComponents]);
194
-
195
- const ActionDom = (props) => {
196
- return (
197
- <div onPointerDown={(e) => e.stopPropagation()}>
198
- {Slots?.tableActionsSlot && <Slots.tableActionsSlot {...props?.cell} />}
199
- {Slots?.actionPrefixSlot && <Slots.actionPrefixSlot {...props?.cell} />}
200
- {Slots?.actionCenterSlot && <Slots.actionCenterSlot {...props?.cell} />}
201
- {Slots?.actionSuffixSlot && <Slots.actionSuffixSlot {...props?.cell} />}
202
- </div>
203
- );
204
- };
205
-
206
- const CheckBoxDom = (props) => {
207
- const { record, rowId } = props?.cell;
208
- return (
209
- <Checkbox
210
- {...props?.componentProps}
211
- checked={
212
- rowSelection?.selectedRowKeys?.includes((record && record[rowKey]) || "") ||
213
- (rowSelection?.selectedRowKeys?.length == dataSource?.length && dataSource?.length > 0)
214
- }
215
- indeterminate={
216
- !rowId &&
217
- rowSelection?.selectedRowKeys?.length > 0 &&
218
- rowSelection?.selectedRowKeys?.length < dataSource?.length
219
- }
220
- onChange={(e) => {
221
- if (e.target.checked && rowId) {
222
- const newSelectedRowKeys = [...rowSelection?.selectedRowKeys, record[rowKey]];
223
- const rows = dataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
224
-
225
- rowSelection?.onChange(newSelectedRowKeys, rows);
226
- } else if (!e.target.checked && rowId) {
227
- const newSelectedRowKeys = rowSelection?.selectedRowKeys?.filter((el) => el != record[rowKey]);
228
- const rows = dataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
229
-
230
- rowSelection?.onChange(
231
- rowSelection?.selectedRowKeys?.filter((el) => el != record[rowKey]),
232
- rows,
233
- );
234
- } else if (e.target.checked && !rowId) {
235
- const newSelectedRowKeys = dataSource?.map((el) => el[rowKey]);
236
- const rows = dataSource?.filter((el) => newSelectedRowKeys?.includes(el[rowKey]));
237
-
238
- rowSelection?.onChange(
239
- dataSource?.map((el) => el[rowKey]),
240
- rows,
241
- );
242
- } else if (!e.target.checked && !rowId) {
243
- rowSelection?.onChange([], []);
244
- }
245
- }}
246
- ></Checkbox>
247
- );
248
- };
249
- useEffect(() => {
250
- if (rowSelection) {
251
- const rows = dataSource?.filter((el) => selectedRowKeys?.includes(el[rowKey]));
252
- rowSelection?.onChange(selectedRowKeys, rows);
253
- }
254
- }, [selectedRowKeys, dataSource]);
255
- useEffect(() => {
256
- setSelectedRowKeys([]);
257
- }, [query?.pageNum]);
258
-
259
- const customCellTemplates = useMemo(() => {
260
- const templatesObj: any = {};
261
- for (let key in reactGridProps?.customCellTemplates) {
262
- const item = reactGridProps?.customCellTemplates[key];
263
- templatesObj[key] = WidthWrapHoc(item);
264
- }
265
- return templatesObj;
266
- }, [reactGridProps?.customCellTemplates]);
267
-
268
- return (
269
- <Spin spinning={loading}>
270
- <div className={`grid-container ${className}`} style={reactGridStyle}>
271
- <ReactGrid
272
- ref={gridRef}
273
- rows={rows}
274
- columns={tableColumns}
275
- onCellsChanged={handleCellsChanged}
276
- enableFillHandle={true}
277
- enableRangeSelection={true}
278
- horizontalStickyBreakpoint={120}
279
- {...reactGridProps}
280
- onFocusLocationChanging={(cell: any) => {
281
- const datatypeId = cell?.columnId + cell?.rowId;
282
- (window as any).gridEditId = datatypeId;
283
- if (["_$actions", "rowSelection"]?.includes(cell?.columnId)) {
284
- return false;
285
- }
286
- if (reactGridProps?.onFocusLocationChanging) {
287
- return reactGridProps?.onFocusLocationChanging(cell, rows);
288
- }
289
- return cell;
290
- }}
291
- customCellTemplates={{
292
- ExpandListTemplate: WidthWrapHoc(ExpandListTemplate),
293
- ...baseTemplate,
294
- ...customCellTemplates,
295
- _$actions: WidthWrapHoc(ActionDom),
296
- rowSelection: WidthWrapHoc(CheckBoxDom),
297
- Input: TextTemplate,
298
- Number: NumberTemplate,
299
- // ExpandListTemplate: WidthWrap({ WrapComponent: ExpandListTemplate }, true),
300
- }}
301
-
302
- // onSelectionChanged={(newSelection) => {
303
- // console.log("选区发生变化", newSelection[0]);
304
- // }}
305
- />
306
- </div>
307
- </Spin>
308
- );
309
- }