lu-lowcode-package-form 0.9.31 → 0.9.33

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.
@@ -0,0 +1,302 @@
1
+ import React, { forwardRef, useEffect } from "react";
2
+ import { Form, Row, Col, message } from "antd";
3
+
4
+ import { debounce } from 'lodash';
5
+ import { evalFormula } from '../../utils/formula'
6
+
7
+
8
+ function batchElements(elements, groupSize) {
9
+ const groupedElements = [];
10
+ let tempArray = [];
11
+
12
+ const fillWithReactElement = (size, array) => {
13
+ const missingElementsCount = size - array.length;
14
+ array.push(...new Array(missingElementsCount).fill(React.createElement('div')));
15
+ };
16
+
17
+ for (const element of elements) {
18
+ const { _componentName } = element?.props || {};
19
+ const componentName = element.type?.displayName || _componentName;
20
+ if (componentName && componentName.startsWith('Layout.')) {
21
+ if (tempArray.length > 0) {
22
+ fillWithReactElement(groupSize, tempArray);
23
+ groupedElements.push(tempArray);
24
+ tempArray = [];
25
+ }
26
+ groupedElements.push([element]);
27
+ } else {
28
+ tempArray.push(element);
29
+ if (tempArray.length === groupSize) {
30
+ groupedElements.push(tempArray);
31
+ tempArray = [];
32
+ }
33
+ }
34
+ }
35
+
36
+ if (tempArray.length > 0) {
37
+ fillWithReactElement(groupSize, tempArray);
38
+ groupedElements.push(tempArray);
39
+ }
40
+
41
+ return groupedElements;
42
+ }
43
+
44
+
45
+ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) => {
46
+ const [form] = Form.useForm();
47
+ const [formContent, setFormContent] = React.useState(null);
48
+ const dependencyMap = React.useRef(null);
49
+
50
+ React.useImperativeHandle(ref, () => ({
51
+ formRef: form,
52
+ initializeFieldVisibility,
53
+ }), []);
54
+
55
+ useEffect(() => {
56
+ initializeDependencyMap();
57
+ setFormContent(renderChildren());
58
+ }, [children, cols]);
59
+
60
+ const initializeDependencyMap = () => {
61
+ const fields = [];
62
+ function traverse(currentNode) {
63
+ var componentName = currentNode.type?.displayName || currentNode.props?._componentName;
64
+ const { props } = currentNode;
65
+ if (componentName && (componentName.startsWith('Field.') || componentName.startsWith('Layout.'))) {
66
+ fields.push(currentNode);
67
+ }
68
+
69
+ if (props?.children) {
70
+ let children = React.Children.toArray(props?.children)
71
+ children.forEach(child => traverse(child));
72
+ }
73
+ }
74
+ dependencyMap.current = new Map();
75
+ const childrenArray = React.Children.toArray(children);
76
+ for (let index = 0; index < childrenArray.length; index++) {
77
+ const element = childrenArray[index];
78
+ traverse(element)
79
+ }
80
+ fields.forEach(child => {
81
+ const { componentId, __id, ...props } = child?.props;
82
+ const identifier = componentId || __id;
83
+ dependencyMap.current.set(identifier, {
84
+ children: childrenArray.filter(item => item.props.withId === identifier || item.props.withIds?.some(item => {
85
+ return identifier == item || item.startsWith(identifier + '.')
86
+ })),
87
+ show: true,
88
+ });
89
+ });
90
+ console.log("dependencyMap", dependencyMap.current)
91
+ initializeFieldVisibility();
92
+ };
93
+
94
+ // 初始化字段的级联关系
95
+ const initializeFieldVisibility = (reloadFields = false) => {
96
+ const fieldValues = form.getFieldsValue();
97
+ for (let key of dependencyMap.current.keys()) {
98
+ handleFieldsWith(key, fieldValues);
99
+ }
100
+ if (reloadFields) setFormContent(renderChildren());
101
+ };
102
+
103
+ // 计算字段级联关系
104
+ const handleFieldsWith = (identifier, fieldValues) => {
105
+ let needRefresh = false;
106
+ if (dependencyMap.current.has(identifier)) {
107
+ const dependentChildren = dependencyMap.current.get(identifier).children;
108
+ dependentChildren.forEach(child => {
109
+ if (child.props.withFunc && typeof child.props.withFunc === 'function') {
110
+ needRefresh = handleFieldsVisible(fieldValues, child)
111
+ }
112
+
113
+ if (child.props.withFill)
114
+ handleFieldsWithFill(fieldValues, child)
115
+
116
+ });
117
+ }
118
+ return needRefresh;
119
+ };
120
+
121
+ // 处理级联显示隐藏 @return {boolean} 是否需要重新渲染表单的字段
122
+ const handleFieldsVisible = (fieldValues, child) => {
123
+ let needRefresh = false;
124
+ const childShouldBeVisible = child.props.withFunc(fieldValues);
125
+ const childIdentifier = child.props.componentId || child.props.__id;
126
+ if (dependencyMap.current.has(childIdentifier)) {
127
+ const childData = dependencyMap.current.get(childIdentifier);
128
+ if (childData.show !== childShouldBeVisible) {
129
+ childData.show = childShouldBeVisible;
130
+ dependencyMap.current.set(childIdentifier, childData);
131
+ needRefresh = true;
132
+ }
133
+ }
134
+ return needRefresh
135
+ }
136
+ // 处理级联数据源
137
+ // 处理级联填充
138
+ const handleFieldsWithFill = async (fieldValues, child) => {
139
+ const withFill = child.props.withFill;
140
+ const withDataFetch = child.props.withDataFetch;
141
+ console.log("withFill", withFill)
142
+ console.log("fieldValues", fieldValues)
143
+ console.log("child", child)
144
+
145
+ let withDatas = [];
146
+ // 先处理依赖数据
147
+ if (withFill?.withData && withFill?.withData.length > 0 && withDataFetch && typeof withDataFetch === 'function') {
148
+ for (let index = 0; index < withFill?.withData.length; index++) {
149
+ const element = withFill?.withData[index];
150
+ let params = {}
151
+ params.tableName = element.withTable.table_name
152
+ params.filter = {}
153
+ for (let index = 0; index < element.withCondition.length; index++) {
154
+ const { value: condition_value, column: condition_column } = element.withCondition[index];
155
+ params.filter[condition_column.column_name] = getParamValue(condition_value.group_key, condition_value.field_key, fieldValues, withDatas)
156
+ }
157
+
158
+ // 访问接口获取数据
159
+ const response = await withDataFetch(params)
160
+ if (response.code === 0 && response.data.list) {
161
+ withDatas.push({
162
+ id: element.id,
163
+ data: response.data.list
164
+ })
165
+ }
166
+ }
167
+ }
168
+
169
+ // 构造计算公式
170
+ let formula;
171
+ if (withFill.value && withFill.value.length > 0) {
172
+ formula = withFill.value.map(item => {
173
+ let result = "";
174
+ const { insert, attributes } = item
175
+ if (typeof insert !== "string") {
176
+ if (insert?.span && attributes && attributes.tagKey && attributes.id) {
177
+ result = getParamValue(attributes.tagKey, attributes.id, fieldValues, withDatas)
178
+ if (Array.isArray(result)) result = JSON.stringify(result)
179
+ else if (result) result = `"${result}"`
180
+ }
181
+ }
182
+ else result = insert
183
+ return result
184
+ })
185
+ }
186
+ console.log("formula", formula)
187
+ if (formula && formula.length > 0) {
188
+ const childIdentifier = child.props.componentId || child.props.__id;
189
+ form.setFieldValue(childIdentifier, evalFormula(formula))
190
+ }
191
+ }
192
+
193
+ const getParamValue = (tagKey, id, fieldValues, withDatas) => {
194
+ let result = "";
195
+ // 从当前表单字段取值
196
+ if (tagKey == "fieldsValue") {
197
+ if (id.indexOf(".") >= 0) {
198
+ const [parentKey, childKey] = id.split(".");
199
+ const parentValue = fieldValues?.[parentKey] || [];
200
+ if (Array.isArray(parentValue))
201
+ result = parentValue.map(item => {
202
+ return item?.[childKey] || ""
203
+ })
204
+ else result = parentValue?.[childKey] || ""
205
+ }
206
+ else result = fieldValues?.[id] || ""
207
+ }
208
+ // 从依赖数据取值
209
+ else {
210
+ let withData = withDatas.find(item => item.id === tagKey)
211
+ if (withData && withData.data && withData.data.length > 0) {
212
+ // 暂时只取一条数据,后续再想 sum 函数等问题
213
+ result = withData.data[0]?.[id] || ""
214
+ }
215
+ }
216
+ return result
217
+ }
218
+
219
+ const handleFieldsChange = debounce((changedFields, allFields) => {
220
+ const fieldValues = form.getFieldsValue();
221
+ let needRefresh = false;
222
+ changedFields.forEach(field => {
223
+ if (field.name && field.name.length > 0) {
224
+ needRefresh = handleFieldsWith(field.name[0], fieldValues);
225
+ }
226
+ });
227
+
228
+ if (needRefresh) {
229
+ setFormContent(renderChildren());
230
+ }
231
+ }, 200);
232
+
233
+ const renderChildren = () => {
234
+ const childrenArray = React.Children.toArray(children);
235
+ console.log("childrenArray", childrenArray)
236
+ const groupedChildren = batchElements(
237
+ childrenArray.filter(child => {
238
+ const identifier = child.props.componentId || child.props.__id;
239
+ return dependencyMap.current.has(identifier) && dependencyMap.current.get(identifier).show;
240
+ }),
241
+ cols
242
+ );
243
+
244
+ return groupedChildren.map((group, index) => (
245
+ <Row key={`row-${index}`} gutter={[24, 24]}>
246
+ {group.map((child, index) => {
247
+ const { componentId, __id, _componentName, ...props } = child.props;
248
+ const componentName = child.type?.displayName || _componentName;
249
+ const identifier = componentId || __id;
250
+ const isLayoutComponent = componentName && componentName.startsWith('Layout.');
251
+ const rules = []
252
+ if (props.isRequired)
253
+ rules.push({ required: true, message: `${props.label}必须填写` });
254
+ if (props.rules)
255
+ if (Array.isArray(props.rules)) {
256
+ const pattern = props.rules.join("|")
257
+ rules.push({ pattern: new RegExp(pattern), message: props.rulesFailMessage ? props.rulesFailMessage : `${props.label}格式错误` })
258
+ }
259
+ else {
260
+ rules.push({ pattern: new RegExp(props.rules), message: props.rulesFailMessage ? props.rulesFailMessage : `${props.label}格式错误` })
261
+ }
262
+ return (
263
+ <Col key={identifier || `col-${index}`} span={isLayoutComponent ? 24 : 24 / group.length} style={{ marginBottom: 0 }}>
264
+ {isLayoutComponent && child}
265
+ {!isLayoutComponent && <Form.Item
266
+ style={{ marginBottom: 0 }}
267
+ label=""
268
+ name={identifier}
269
+ rules={rules}
270
+ >
271
+ {child}
272
+ </Form.Item>
273
+ }
274
+
275
+ </Col>
276
+ );
277
+ })}
278
+ </Row>
279
+ ));
280
+ };
281
+
282
+ return (
283
+ <Form form={form} className={"form-container fp-0 fw-full fh-full box-border fflex fflex-col fgap-y-2" + (mode == "desgin" ? " fp-6" : "")} onFieldsChange={handleFieldsChange}>
284
+ {formContent}
285
+ </Form>
286
+ );
287
+ });
288
+
289
+ export function withWrap(Component) {
290
+ return forwardRef((props, ref) => <Component {...props} ref={ref} forwardedRef={ref} />);
291
+ }
292
+
293
+ export class FormContainerClass extends React.PureComponent {
294
+ render() {
295
+ const { forwardedRef, ...otherProps } = this.props;
296
+ return <FormContainer {...otherProps} ref={forwardedRef} />
297
+ }
298
+ }
299
+ const FormContainerWrapper = withWrap(({ forwardedRef, ...props }) => <FormContainer {...props} ref={forwardedRef} />);
300
+
301
+ export { LayoutFormRow, LayoutFormGroupTitle } from './layout';
302
+ export { FormContainer, FormContainerWrapper };
@@ -59,16 +59,27 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
59
59
 
60
60
  const initializeDependencyMap = () => {
61
61
  const fields = [];
62
- function traverse(currentNode) {
62
+ function traverse(currentNode,parentNode = null) {
63
63
  var componentName = currentNode.type?.displayName || currentNode.props?._componentName;
64
64
  const { props } = currentNode;
65
65
  if (componentName && (componentName.startsWith('Field.') || componentName.startsWith('Layout.'))) {
66
- fields.push(currentNode);
66
+ let identifier = props?.componentId || props?.__id;
67
+ let withIds = []
68
+ console.log("currentNode",currentNode)
69
+ if(parentNode && parentNode?.props) {
70
+ identifier = `${parentNode?.props?.componentId || parentNode?.props?.__id}.${identifier}`
71
+ }
72
+ if (props?.withId)
73
+ withIds.push(props?.withId)
74
+ if (props?.withIds)
75
+ withIds = [...withIds,...props?.withIds]
76
+
77
+ fields.push({identifier, withIds,component: currentNode});
67
78
  }
68
79
 
69
80
  if (props?.children) {
70
81
  let children = React.Children.toArray(props?.children)
71
- children.forEach(child => traverse(child));
82
+ children.forEach(child => traverse(child,(componentName && (componentName.startsWith('Field.')))?currentNode:null));
72
83
  }
73
84
  }
74
85
  dependencyMap.current = new Map();
@@ -77,17 +88,18 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
77
88
  const element = childrenArray[index];
78
89
  traverse(element)
79
90
  }
80
- fields.forEach(child => {
81
- const { componentId, __id, ...props } = child?.props;
82
- const identifier = componentId || __id;
91
+ // console.log("fields", fields)
92
+ fields.forEach(field => {
93
+ const { identifier } = field;
83
94
  dependencyMap.current.set(identifier, {
84
- children: childrenArray.filter(item => item.props.withId === identifier || item.props.withIds?.some(item => {
85
- return identifier == item || item.startsWith(identifier + '.')
86
- })),
95
+ // children: childrenArray.filter(item => item.props.withId === identifier || item.props.withIds?.some(item => {
96
+ // return identifier == item || item.startsWith(identifier + '.')
97
+ // })),
98
+ children: fields.filter(item=>item.withIds.some(item=>item == identifier)),
87
99
  show: true,
88
100
  });
89
101
  });
90
- console.log("dependencyMap", dependencyMap.current)
102
+ // console.log("dependencyMap", dependencyMap.current)
91
103
  initializeFieldVisibility();
92
104
  };
93
105
 
@@ -103,15 +115,23 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
103
115
  // 计算字段级联关系
104
116
  const handleFieldsWith = (identifier, fieldValues) => {
105
117
  let needRefresh = false;
118
+ let parentIdentifier = [];
119
+ if (Array.isArray(identifier)) {
120
+ parentIdentifier = [...(identifier.slice(0,-1))]
121
+ identifier = identifier.filter(item=>typeof item == "string").join(".")
122
+ }
106
123
  if (dependencyMap.current.has(identifier)) {
124
+ // console.log("dependencyMap.current.get(identifier)",dependencyMap.current.get(identifier))
107
125
  const dependentChildren = dependencyMap.current.get(identifier).children;
126
+ // console.log("dependentChildren", dependentChildren)
127
+
108
128
  dependentChildren.forEach(child => {
109
- if (child.props.withFunc && typeof child.props.withFunc === 'function') {
110
- needRefresh = handleFieldsVisible(fieldValues, child)
129
+ if (child.component.props.withFunc && typeof child.component.props.withFunc === 'function') {
130
+ needRefresh = handleFieldsVisible(fieldValues, child,parentIdentifier)
111
131
  }
112
132
 
113
- if (child.props.withFill)
114
- handleFieldsWithFill(fieldValues, child)
133
+ if (child.component.props.withFill)
134
+ handleFieldsWithFill(fieldValues, child,parentIdentifier)
115
135
 
116
136
  });
117
137
  }
@@ -119,10 +139,10 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
119
139
  };
120
140
 
121
141
  // 处理级联显示隐藏 @return {boolean} 是否需要重新渲染表单的字段
122
- const handleFieldsVisible = (fieldValues, child) => {
142
+ const handleFieldsVisible = (fieldValues, child,parentIdentifier) => {
123
143
  let needRefresh = false;
124
- const childShouldBeVisible = child.props.withFunc(fieldValues);
125
- const childIdentifier = child.props.componentId || child.props.__id;
144
+ const childShouldBeVisible = child?.component?.props?.withFunc(fieldValues);
145
+ const childIdentifier = child?.identifier;
126
146
  if (dependencyMap.current.has(childIdentifier)) {
127
147
  const childData = dependencyMap.current.get(childIdentifier);
128
148
  if (childData.show !== childShouldBeVisible) {
@@ -135,13 +155,19 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
135
155
  }
136
156
  // 处理级联数据源
137
157
  // 处理级联填充
138
- const handleFieldsWithFill = async (fieldValues, child) => {
139
- const withFill = child.props.withFill;
140
- const withDataFetch = child.props.withDataFetch;
141
- console.log("withFill", withFill)
142
- console.log("fieldValues", fieldValues)
143
- console.log("child", child)
144
-
158
+ const handleFieldsWithFill = async (fieldValues, child,parentIdentifier) => {
159
+ const withFill = child?.component?.props.withFill;
160
+ const withDataFetch = child?.component?.props.withDataFetch;
161
+ let withFillIndex = 0
162
+ let withFillGroup = ""
163
+ let childIdentifier = child.identifier;
164
+ if (parentIdentifier && Array.isArray(parentIdentifier) && parentIdentifier.length > 1){
165
+ withFillGroup = parentIdentifier.filter(item=>typeof item == "string").join(".")
166
+ withFillIndex = parentIdentifier[parentIdentifier.length-1]
167
+ if(childIdentifier.startsWith(`${withFillGroup}.`)) {
168
+ childIdentifier = [...parentIdentifier, childIdentifier.replace(`${withFillGroup}.`,"")]
169
+ }
170
+ }
145
171
  let withDatas = [];
146
172
  // 先处理依赖数据
147
173
  if (withFill?.withData && withFill?.withData.length > 0 && withDataFetch && typeof withDataFetch === 'function') {
@@ -174,8 +200,14 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
174
200
  const { insert, attributes } = item
175
201
  if (typeof insert !== "string") {
176
202
  if (insert?.span && attributes && attributes.tagKey && attributes.id) {
203
+
177
204
  result = getParamValue(attributes.tagKey, attributes.id, fieldValues, withDatas)
178
- if (Array.isArray(result)) result = JSON.stringify(result)
205
+ if (Array.isArray(result)){
206
+ if (Array.isArray(childIdentifier) && result.length > withFillIndex) {
207
+ result = result[withFillIndex]
208
+ }
209
+ else result = JSON.stringify(result)
210
+ }
179
211
  else if (result) result = `"${result}"`
180
212
  }
181
213
  }
@@ -183,10 +215,12 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
183
215
  return result
184
216
  })
185
217
  }
186
- console.log("formula", formula)
218
+ // console.log("formula", formula)
187
219
  if (formula && formula.length > 0) {
188
- const childIdentifier = child.props.componentId || child.props.__id;
189
- form.setFieldValue(childIdentifier, evalFormula(formula))
220
+ const formulaResult = evalFormula(formula);
221
+ // console.log("formulaResult", formulaResult)
222
+ form.setFieldValue(childIdentifier, formulaResult)
223
+ handleFieldsWith(childIdentifier, form.getFieldsValue())
190
224
  }
191
225
  }
192
226
 
@@ -219,9 +253,10 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
219
253
  const handleFieldsChange = debounce((changedFields, allFields) => {
220
254
  const fieldValues = form.getFieldsValue();
221
255
  let needRefresh = false;
256
+ console.log("changedFields",changedFields)
222
257
  changedFields.forEach(field => {
223
258
  if (field.name && field.name.length > 0) {
224
- needRefresh = handleFieldsWith(field.name[0], fieldValues);
259
+ needRefresh = handleFieldsWith(field.name, fieldValues);
225
260
  }
226
261
  });
227
262
 
@@ -236,7 +271,7 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
236
271
  const groupedChildren = batchElements(
237
272
  childrenArray.filter(child => {
238
273
  const identifier = child.props.componentId || child.props.__id;
239
- return dependencyMap.current.has(identifier) && dependencyMap.current.get(identifier).show;
274
+ return !dependencyMap.current.has(identifier) || dependencyMap.current.get(identifier)?.show;
240
275
  }),
241
276
  cols
242
277
  );
@@ -248,6 +283,7 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
248
283
  const componentName = child.type?.displayName || _componentName;
249
284
  const identifier = componentId || __id;
250
285
  const isLayoutComponent = componentName && componentName.startsWith('Layout.');
286
+ const isTable = componentName && componentName == 'Field.Table';
251
287
  const rules = []
252
288
  if (props.isRequired)
253
289
  rules.push({ required: true, message: `${props.label}必须填写` });
@@ -261,18 +297,17 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
261
297
  }
262
298
  return (
263
299
  <Col key={identifier || `col-${index}`} span={isLayoutComponent ? 24 : 24 / group.length} style={{ marginBottom: 0 }}>
264
- {isLayoutComponent ? (
265
- child
266
- ) : (
267
- <Form.Item
268
- style={{ marginBottom: 0 }}
269
- label=""
270
- name={identifier}
271
- rules={rules}
272
- >
273
- {child}
274
- </Form.Item>
275
- )}
300
+ {(isLayoutComponent || isTable) && child}
301
+ {!isLayoutComponent && <Form.Item
302
+ style={{ marginBottom: 0 }}
303
+ label=""
304
+ name={identifier}
305
+ rules={rules}
306
+ >
307
+ {child}
308
+ </Form.Item>
309
+ }
310
+
276
311
  </Col>
277
312
  );
278
313
  })}
@@ -11,7 +11,6 @@ const LayoutFormRow = ({ children, layout }) => {
11
11
 
12
12
  const colSpans = useMemo(() => getColSpan(layout), [layout]);
13
13
 
14
- console.log("colSpans", colSpans)
15
14
 
16
15
  const formItems = useMemo(() => {
17
16
  return React.Children.toArray(children).map((child, index) => {