@hzab/list-render 1.10.0-beta7 → 1.10.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/package.json CHANGED
@@ -1,66 +1,66 @@
1
- {
2
- "name": "@hzab/list-render",
3
- "version": "1.10.0-beta7",
4
- "description": "",
5
- "main": "src",
6
- "scripts": {
7
- "dev": "webpack serve -c ./config/webpack.config.js --env local",
8
- "build": "webpack -c ./config/webpack.config.js --env production",
9
- "publish-beta": "npm publish --beta",
10
- "publish-patch": "npm version patch && npm publish --access public",
11
- "publish-minor": "npm version minor && npm publish --access public",
12
- "publish-major": "npm version major && npm publish --access public"
13
- },
14
- "files": [
15
- "src",
16
- "docs",
17
- "CHANGELOG.md"
18
- ],
19
- "keywords": [],
20
- "author": "CaiYansong",
21
- "license": "ISC",
22
- "devDependencies": {
23
- "@ant-design/icons": "^4.8.1",
24
- "@hzab/data-model": "^1.7.4",
25
- "@hzab/form-render": "^1.6.12",
26
- "@hzab/formily-result-utils": "^1.2.0",
27
- "@hzab/permissions": "^1.0.0",
28
- "@hzab/schema-descriptions": "^1.3.0",
29
- "@hzab/webpack-config": "^0.7.2",
30
- "@types/react": "^17.0.62",
31
- "@types/react-dom": "^17.0.20",
32
- "antd": "^4.24.12",
33
- "axios": "^1.4.0",
34
- "eslint": "^8.30.0",
35
- "mobx": "^6.7.0",
36
- "mobx-react": "^7.6.0",
37
- "less": "^4.1.3",
38
- "lodash": "^4.17.21",
39
- "react": "^17.0.2",
40
- "react-dom": "^17.0.2",
41
- "react-router-dom": "^6.8.1",
42
- "typescript": "^4.9.4"
43
- },
44
- "peerDependencies": {
45
- "@formily/core": "2.3.1",
46
- "@formily/react": "2.3.1",
47
- "@formily/reactive-react": "2.3.1",
48
- "@hzab/data-model": ">=1.7.4",
49
- "@hzab/form-render": ">=1.0.0",
50
- "@hzab/formily-result-utils": ">=1.2.0",
51
- "@hzab/schema-descriptions": ">=1.0.0",
52
- "antd": "4.x",
53
- "axios": ">=1.4.0",
54
- "c-formily-antd": "2.3.1",
55
- "lodash": ">=4.17.21",
56
- "react": ">=16.8.0",
57
- "react-dom": ">=16.8.0"
58
- },
59
- "directories": {
60
- "lib": "lib"
61
- },
62
- "dependencies": {
63
- "array-move": "^4.0.0",
64
- "react-sortable-hoc": "^2.0.0"
65
- }
66
- }
1
+ {
2
+ "name": "@hzab/list-render",
3
+ "version": "1.10.0",
4
+ "description": "",
5
+ "main": "src",
6
+ "scripts": {
7
+ "dev": "webpack serve -c ./config/webpack.config.js --env local",
8
+ "build": "webpack -c ./config/webpack.config.js --env production",
9
+ "publish-beta": "npm publish --beta",
10
+ "publish-patch": "npm version patch && npm publish --access public",
11
+ "publish-minor": "npm version minor && npm publish --access public",
12
+ "publish-major": "npm version major && npm publish --access public"
13
+ },
14
+ "files": [
15
+ "src",
16
+ "docs",
17
+ "CHANGELOG.md"
18
+ ],
19
+ "keywords": [],
20
+ "author": "CaiYansong",
21
+ "license": "ISC",
22
+ "devDependencies": {
23
+ "@ant-design/icons": "^4.8.1",
24
+ "@hzab/data-model": "^1.7.4",
25
+ "@hzab/form-render": "^1.6.12",
26
+ "@hzab/formily-result-utils": "^1.2.0",
27
+ "@hzab/permissions": "^1.0.0",
28
+ "@hzab/schema-descriptions": "^1.3.0",
29
+ "@hzab/webpack-config": "^0.7.2",
30
+ "@types/react": "^17.0.62",
31
+ "@types/react-dom": "^17.0.20",
32
+ "antd": "^4.24.12",
33
+ "axios": "^1.4.0",
34
+ "eslint": "^8.30.0",
35
+ "mobx": "^6.7.0",
36
+ "mobx-react": "^7.6.0",
37
+ "less": "^4.1.3",
38
+ "lodash": "^4.17.21",
39
+ "react": "^17.0.2",
40
+ "react-dom": "^17.0.2",
41
+ "react-router-dom": "^6.8.1",
42
+ "typescript": "^4.9.4"
43
+ },
44
+ "peerDependencies": {
45
+ "@formily/core": "2.3.1",
46
+ "@formily/react": "2.3.1",
47
+ "@formily/reactive-react": "2.3.1",
48
+ "@hzab/data-model": ">=1.7.4",
49
+ "@hzab/form-render": ">=1.0.0",
50
+ "@hzab/formily-result-utils": ">=1.2.0",
51
+ "@hzab/schema-descriptions": ">=1.0.0",
52
+ "antd": "4.x",
53
+ "axios": ">=1.4.0",
54
+ "c-formily-antd": "2.3.1",
55
+ "lodash": ">=4.17.21",
56
+ "react": ">=16.8.0",
57
+ "react-dom": ">=16.8.0"
58
+ },
59
+ "directories": {
60
+ "lib": "lib"
61
+ },
62
+ "dependencies": {
63
+ "array-move": "^4.0.0",
64
+ "react-sortable-hoc": "^2.0.0"
65
+ }
66
+ }
@@ -1,252 +1,252 @@
1
- import React, { useState, useCallback, useRef } from "react";
2
- import { Card, Slider, Rate, Form, Button } from "antd";
3
- import { EditOutlined, CheckOutlined, CloseOutlined } from "@ant-design/icons";
4
- import { createSchemaField, FormProvider, FormConsumer } from "@formily/react";
5
- import { cloneDeep } from "lodash";
6
-
7
- import { antdComponents, customComponents } from "@hzab/form-render";
8
-
9
- export interface Item {
10
- key: string;
11
- name: string;
12
- age: number;
13
- address: string;
14
- }
15
-
16
- export interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
17
- SchemaField: any;
18
- field: {
19
- type: string;
20
- name: string;
21
- };
22
- editing: boolean;
23
- dataIndex: string;
24
- title: any;
25
- inputType: "number" | "text";
26
- record: Item;
27
- index: number;
28
- editable: boolean;
29
- onEdit: (record: Partial<Item> & { key: React.Key }, dataIndex: string) => void;
30
- onSave: () => void;
31
- onCancel: () => void;
32
- topProps: {
33
- config: { hasEdit: boolean | Function };
34
- editMode: "modal" | "line" | "line-cell" | "cell";
35
- };
36
- children: React.ReactNode;
37
- }
38
-
39
- export const EditableCell: React.FC<EditableCellProps> = ({
40
- SchemaField,
41
- field,
42
- editing,
43
- dataIndex,
44
- title,
45
- inputType,
46
- record,
47
- index,
48
- onEdit,
49
- onSave,
50
- onCancel,
51
- editable,
52
- topProps,
53
- children,
54
- ...restProps
55
- }) => {
56
- const fPattern = field?.["x-pattern"];
57
- const fDisplay = field?.["x-display"];
58
- const { editMode = "modal" } = topProps || {};
59
- const { hasEdit } = topProps?.config || {};
60
-
61
- // 弹窗编辑模式
62
- if (editMode === "modal") {
63
- return <td {...restProps}>{children}</td>;
64
- }
65
- // 无编辑权限或禁止编辑
66
- const _hasEdit = hasEdit && typeof hasEdit === "function" ? hasEdit(record, index) : hasEdit !== false;
67
- let _editable = _hasEdit && editable && !(fPattern === "disabled" || fPattern === "readOnly" || fDisplay === "none");
68
- if (!_editable) {
69
- return <td {...restProps}>{children}</td>;
70
- }
71
-
72
- // 编辑中状态
73
- if (editing) {
74
- // 使用 Schema 渲染模式
75
- return (
76
- <td {...restProps}>
77
- <div className="editable-table-cell table-cell-editing">
78
- <SchemaField
79
- schema={{
80
- type: "object",
81
- properties: {
82
- [field.name]: { ...field, title: "" },
83
- },
84
- }}
85
- />
86
- {editMode !== "line" ? (
87
- <div className="cell-editing-actions">
88
- <CheckOutlined className="table-cell-confirm-icon" onClick={onSave} />
89
- <CloseOutlined className="table-cell-cancel-icon" onClick={onCancel} />
90
- </div>
91
- ) : null}
92
- </div>
93
- </td>
94
- );
95
- }
96
-
97
- // 行内单元格编辑按钮
98
- const showEditIcon = editMode === "line-cell" || editMode === "cell";
99
- if (showEditIcon) {
100
- return (
101
- <td {...restProps}>
102
- <div
103
- className="editable-table-cell"
104
- onDoubleClick={(e) => {
105
- e?.preventDefault();
106
- e?.stopPropagation();
107
- onEdit(record, dataIndex);
108
- }}
109
- >
110
- {children}
111
- <EditOutlined
112
- className="table-cell-edit-icon"
113
- onClick={(e) => {
114
- e?.preventDefault();
115
- e?.stopPropagation();
116
- onEdit(record, dataIndex);
117
- }}
118
- />
119
- </div>
120
- </td>
121
- );
122
- }
123
- return <td {...restProps}>{children}</td>;
124
- };
125
-
126
- export const useEditTable = (props) => {
127
- const { topProps, idKey, columns, onEditSubmit, formRender } = props;
128
- const { editMode = "modal" } = topProps || {};
129
-
130
- // 创建 formily 表单相关参数
131
- /** schema scope 解决父级无 schema Scope 导致 scope 对象刷新的问题 */
132
- const schemaScopeRef = useRef<{ _$tempData: Object }>();
133
- if (props.schemaScope && !schemaScopeRef.current) {
134
- schemaScopeRef.current = props.schemaScope;
135
- } else if (!schemaScopeRef.current) {
136
- schemaScopeRef.current = { _$tempData: {} };
137
- }
138
- if (!schemaScopeRef.current?._$tempData) {
139
- schemaScopeRef.current._$tempData = {};
140
- }
141
- const SchemaField = useCallback(
142
- createSchemaField({
143
- components: {
144
- // Upload,
145
- Card,
146
- Slider,
147
- Rate,
148
- ...antdComponents,
149
- ...customComponents,
150
- ...props.components,
151
- },
152
- scope: schemaScopeRef.current,
153
- }),
154
- [],
155
- );
156
-
157
- // 是否可编辑状态逻辑处理
158
- const [editingId, setEditingId] = useState("");
159
- // 当前编辑的字段
160
- const [editingKey, setEditingKey] = useState("");
161
- /** 判断当前 行 是否处于编辑状态 */
162
- const isEditing = (record: Item) => record[idKey] === editingId;
163
- /** 判断当前 单元格 是否处于编辑状态 */
164
- const isEditingCell = (key) => key === editingKey;
165
-
166
- /**
167
- * 点击编辑按钮
168
- * @param record
169
- */
170
- const onEdit = async (record: Partial<Item> & { key: React.Key }, key) => {
171
- const value = await (props.onEdit && props.onEdit(record));
172
- formRender?.setValues(value);
173
- setEditingId(record[idKey]);
174
- editMode === "cell" && setEditingKey(key);
175
- };
176
-
177
- const onCancel = () => {
178
- setEditingId("");
179
- setEditingKey("");
180
- };
181
-
182
- /**
183
- * 保存数据
184
- */
185
- const onSave = async () => {
186
- try {
187
- // 点击保存,数据抛出
188
- (await formRender?.validate()) as Item;
189
- onEditSubmit && onEditSubmit(formRender.values);
190
- // 清除编辑目标
191
- setEditingId("");
192
- setEditingKey("");
193
- } catch (errInfo) {
194
- console.error("Validate Failed:", errInfo);
195
- }
196
- };
197
-
198
- const mergedColumns = cloneDeep(columns)?.map((col) => {
199
- const isActions = col.key === "_$actions";
200
- if (!col.editable && !isActions) {
201
- return col;
202
- }
203
-
204
- const resCol = {
205
- ...col,
206
- onCell: (record: Item, ...args) => ({
207
- ...(col.onCell ? col.onCell(record, ...args) : {}),
208
- record,
209
- SchemaField,
210
- field: col.field,
211
- dataIndex: col.dataIndex,
212
- title: col.title,
213
- editable: col.editable,
214
- editing: isActions
215
- ? false
216
- : editMode === "cell"
217
- ? isEditing(record) && isEditingCell(col.dataIndex)
218
- : isEditing(record),
219
- topProps: props.topProps,
220
- onEdit,
221
- onSave,
222
- onCancel,
223
- }),
224
- };
225
- // 操作列编辑态改成保存、取消按钮
226
- if (isActions) {
227
- resCol.render = (record, index) => {
228
- // 编辑态操作栏变成保存和取消按钮
229
- if (editMode === "line" && record[idKey] === editingId) {
230
- return (
231
- <div className="edit-table-actions">
232
- <Button type="link" onClick={onSave}>
233
- 保存
234
- </Button>
235
- <Button type="text" onClick={onCancel}>
236
- 取消
237
- </Button>
238
- </div>
239
- );
240
- }
241
- return col.render(record, index);
242
- };
243
- }
244
- return resCol;
245
- });
246
-
247
- return {
248
- onEdit,
249
- SchemaField,
250
- mergedColumns,
251
- };
252
- };
1
+ import React, { useState, useCallback, useRef } from "react";
2
+ import { Card, Slider, Rate, Form, Button } from "antd";
3
+ import { EditOutlined, CheckOutlined, CloseOutlined } from "@ant-design/icons";
4
+ import { createSchemaField, FormProvider, FormConsumer } from "@formily/react";
5
+ import { cloneDeep } from "lodash";
6
+
7
+ import { antdComponents, customComponents } from "@hzab/form-render";
8
+
9
+ export interface Item {
10
+ key: string;
11
+ name: string;
12
+ age: number;
13
+ address: string;
14
+ }
15
+
16
+ export interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
17
+ SchemaField: any;
18
+ field: {
19
+ type: string;
20
+ name: string;
21
+ };
22
+ editing: boolean;
23
+ dataIndex: string;
24
+ title: any;
25
+ inputType: "number" | "text";
26
+ record: Item;
27
+ index: number;
28
+ editable: boolean;
29
+ onEdit: (record: Partial<Item> & { key: React.Key }, dataIndex: string) => void;
30
+ onSave: () => void;
31
+ onCancel: () => void;
32
+ topProps: {
33
+ config: { hasEdit: boolean | Function };
34
+ editMode: "modal" | "line" | "line-cell" | "cell";
35
+ };
36
+ children: React.ReactNode;
37
+ }
38
+
39
+ export const EditableCell: React.FC<EditableCellProps> = ({
40
+ SchemaField,
41
+ field,
42
+ editing,
43
+ dataIndex,
44
+ title,
45
+ inputType,
46
+ record,
47
+ index,
48
+ onEdit,
49
+ onSave,
50
+ onCancel,
51
+ editable,
52
+ topProps,
53
+ children,
54
+ ...restProps
55
+ }) => {
56
+ const fPattern = field?.["x-pattern"];
57
+ const fDisplay = field?.["x-display"];
58
+ const { editMode = "modal" } = topProps || {};
59
+ const { hasEdit } = topProps?.config || {};
60
+
61
+ // 弹窗编辑模式
62
+ if (editMode === "modal") {
63
+ return <td {...restProps}>{children}</td>;
64
+ }
65
+ // 无编辑权限或禁止编辑
66
+ const _hasEdit = hasEdit && typeof hasEdit === "function" ? hasEdit(record, index) : hasEdit !== false;
67
+ let _editable = _hasEdit && editable && !(fPattern === "disabled" || fPattern === "readOnly" || fDisplay === "none");
68
+ if (!_editable) {
69
+ return <td {...restProps}>{children}</td>;
70
+ }
71
+
72
+ // 编辑中状态
73
+ if (editing) {
74
+ // 使用 Schema 渲染模式
75
+ return (
76
+ <td {...restProps}>
77
+ <div className="editable-table-cell table-cell-editing">
78
+ <SchemaField
79
+ schema={{
80
+ type: "object",
81
+ properties: {
82
+ [field.name]: { ...field, title: "" },
83
+ },
84
+ }}
85
+ />
86
+ {editMode !== "line" ? (
87
+ <div className="cell-editing-actions">
88
+ <CheckOutlined className="table-cell-confirm-icon" onClick={onSave} />
89
+ <CloseOutlined className="table-cell-cancel-icon" onClick={onCancel} />
90
+ </div>
91
+ ) : null}
92
+ </div>
93
+ </td>
94
+ );
95
+ }
96
+
97
+ // 行内单元格编辑按钮
98
+ const showEditIcon = editMode === "line-cell" || editMode === "cell";
99
+ if (showEditIcon) {
100
+ return (
101
+ <td {...restProps}>
102
+ <div
103
+ className="editable-table-cell"
104
+ onDoubleClick={(e) => {
105
+ e?.preventDefault();
106
+ e?.stopPropagation();
107
+ onEdit(record, dataIndex);
108
+ }}
109
+ >
110
+ {children}
111
+ <EditOutlined
112
+ className="table-cell-edit-icon"
113
+ onClick={(e) => {
114
+ e?.preventDefault();
115
+ e?.stopPropagation();
116
+ onEdit(record, dataIndex);
117
+ }}
118
+ />
119
+ </div>
120
+ </td>
121
+ );
122
+ }
123
+ return <td {...restProps}>{children}</td>;
124
+ };
125
+
126
+ export const useEditTable = (props) => {
127
+ const { topProps, idKey, columns, onEditSubmit, formRender } = props;
128
+ const { editMode = "modal" } = topProps || {};
129
+
130
+ // 创建 formily 表单相关参数
131
+ /** schema scope 解决父级无 schema Scope 导致 scope 对象刷新的问题 */
132
+ const schemaScopeRef = useRef<{ _$tempData: Object }>();
133
+ if (props.schemaScope && !schemaScopeRef.current) {
134
+ schemaScopeRef.current = props.schemaScope;
135
+ } else if (!schemaScopeRef.current) {
136
+ schemaScopeRef.current = { _$tempData: {} };
137
+ }
138
+ if (!schemaScopeRef.current?._$tempData) {
139
+ schemaScopeRef.current._$tempData = {};
140
+ }
141
+ const SchemaField = useCallback(
142
+ createSchemaField({
143
+ components: {
144
+ // Upload,
145
+ Card,
146
+ Slider,
147
+ Rate,
148
+ ...antdComponents,
149
+ ...customComponents,
150
+ ...props.components,
151
+ },
152
+ scope: schemaScopeRef.current,
153
+ }),
154
+ [],
155
+ );
156
+
157
+ // 是否可编辑状态逻辑处理
158
+ const [editingId, setEditingId] = useState("");
159
+ // 当前编辑的字段
160
+ const [editingKey, setEditingKey] = useState("");
161
+ /** 判断当前 行 是否处于编辑状态 */
162
+ const isEditing = (record: Item) => record[idKey] === editingId;
163
+ /** 判断当前 单元格 是否处于编辑状态 */
164
+ const isEditingCell = (key) => key === editingKey;
165
+
166
+ /**
167
+ * 点击编辑按钮
168
+ * @param record
169
+ */
170
+ const onEdit = async (record: Partial<Item> & { key: React.Key }, key) => {
171
+ const value = await (props.onEdit && props.onEdit(record));
172
+ formRender?.setValues(value);
173
+ setEditingId(record[idKey]);
174
+ editMode === "cell" && setEditingKey(key);
175
+ };
176
+
177
+ const onCancel = () => {
178
+ setEditingId("");
179
+ setEditingKey("");
180
+ };
181
+
182
+ /**
183
+ * 保存数据
184
+ */
185
+ const onSave = async () => {
186
+ try {
187
+ // 点击保存,数据抛出
188
+ (await formRender?.validate()) as Item;
189
+ onEditSubmit && onEditSubmit(formRender.values);
190
+ // 清除编辑目标
191
+ setEditingId("");
192
+ setEditingKey("");
193
+ } catch (errInfo) {
194
+ console.error("Validate Failed:", errInfo);
195
+ }
196
+ };
197
+
198
+ const mergedColumns = cloneDeep(columns)?.map((col) => {
199
+ const isActions = col.key === "_$actions";
200
+ if (!col.editable && !isActions) {
201
+ return col;
202
+ }
203
+
204
+ const resCol = {
205
+ ...col,
206
+ onCell: (record: Item, ...args) => ({
207
+ ...(col.onCell ? col.onCell(record, ...args) : {}),
208
+ record,
209
+ SchemaField,
210
+ field: col.field,
211
+ dataIndex: col.dataIndex,
212
+ title: col.title,
213
+ editable: col.editable,
214
+ editing: isActions
215
+ ? false
216
+ : editMode === "cell"
217
+ ? isEditing(record) && isEditingCell(col.dataIndex)
218
+ : isEditing(record),
219
+ topProps: props.topProps,
220
+ onEdit,
221
+ onSave,
222
+ onCancel,
223
+ }),
224
+ };
225
+ // 操作列编辑态改成保存、取消按钮
226
+ if (isActions) {
227
+ resCol.render = (record, index) => {
228
+ // 编辑态操作栏变成保存和取消按钮
229
+ if (editMode === "line" && record[idKey] === editingId) {
230
+ return (
231
+ <div className="edit-table-actions">
232
+ <Button type="link" onClick={onSave}>
233
+ 保存
234
+ </Button>
235
+ <Button type="text" onClick={onCancel}>
236
+ 取消
237
+ </Button>
238
+ </div>
239
+ );
240
+ }
241
+ return col.render(record, index);
242
+ };
243
+ }
244
+ return resCol;
245
+ });
246
+
247
+ return {
248
+ onEdit,
249
+ SchemaField,
250
+ mergedColumns,
251
+ };
252
+ };