lu-lowcode-package-form 0.11.84 → 0.11.86

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,6 +1,6 @@
1
1
  {
2
2
  "name": "lu-lowcode-package-form",
3
- "version": "0.11.84",
3
+ "version": "0.11.86",
4
4
  "dependencies": {
5
5
  "@ant-design/icons": "^4.8.1",
6
6
  "@dnd-kit/core": "^6.1.0",
package/src/App.jsx CHANGED
@@ -109,7 +109,7 @@ function App() {
109
109
  }
110
110
  }
111
111
  const setFormFields = () => {
112
- formRef?.current?.setFieldsValue({ "__id": 1, "userselect": "1213131", "remark11": { "label": "选项1", "value": "1", "name": "1111", "table": "[{\"price\":1,\"num\":2},{\"price\":2,\"num\":2},{\"price\":3,\"num\":3},{\"price\":3,\"num\":3}]" }, "table2": [{}], "table": [{ "product_num1": "123", "product_sum1": "", "node_oclxmzswzti": "", "select2": "", "switch_table": false, "remark11": { "label": "选项2", "value": "2" }, "product_price12": "", "tianchong1": { "label": "选项1", "value": "1", "tianchong2": { "label": "选项2", "value": "2" }, "tcinput1": "1111" }, "tianchong2": { "label": "选项2", "value": "2", "tcinput1": "8989", "tcinput2": "2222" }, "tcinput1": "8989", "tcinput2": "2222", "tcinput3": "2222" }, { "product_num1": "213", "product_sum1": "", "node_oclxmzswzti": "", "select2": "", "switch_table": false, "datetime2": "2024-08-22 11:09:07", "product_price13": 1, "product_price14": 2, "product_price12": "", "remark11": { "label": "选项3", "value": "3" }, "product_price11": 3 }], "product_total_price": "0.00", "DeptSelect": ["leaf11"], "PostSelect": ["parent 1-1", "leaf11"], "searchuser": [{ "id": 2, "name": "2222", "label": "2222", "value": 2 }, { "id": 4, "name": "4444", "label": "4444", "value": 4 }], "product_price": "213", "product_num": "21", "product_num_range": [1, 22], "product_sum": "4473", "switch": false, "datetime": "2024-08-25", "datetime2": "2024-08-25", "datetime3": "", "datetime4": "2024-08-22 11:09:04", "remark12": JSON.stringify([{ "label": "选项1", "value": "1" }, { "label": "选项2", "value": "2" }]) })
112
+ formRef?.current?.setFieldsValue({ "__id": 1, "routeType":1, "userselect": "1213131", "remark11": { "label": "选项1", "value": "1", "name": "1111", "table": "[{\"price\":1,\"num\":2},{\"price\":2,\"num\":2},{\"price\":3,\"num\":3},{\"price\":3,\"num\":3}]" }, "table2": [{}], "table": [{ "product_num1": "123", "product_sum1": "", "node_oclxmzswzti": "", "select2": "", "switch_table": false, "remark11": { "label": "选项2", "value": "2" }, "product_price12": "", "tianchong1": { "label": "选项1", "value": "1", "tianchong2": { "label": "选项2", "value": "2" }, "tcinput1": "1111" }, "tianchong2": { "label": "选项2", "value": "2", "tcinput1": "8989", "tcinput2": "2222" }, "tcinput1": "8989", "tcinput2": "2222", "tcinput3": "2222" }, { "product_num1": "213", "product_sum1": "", "node_oclxmzswzti": "", "select2": "", "switch_table": false, "datetime2": "2024-08-22 11:09:07", "product_price13": 1, "product_price14": 2, "product_price12": "", "remark11": { "label": "选项3", "value": "3" }, "product_price11": 3 }], "product_total_price": "0.00", "DeptSelect": ["leaf11"], "PostSelect": ["parent 1-1", "leaf11"], "searchuser": [{ "id": 2, "name": "2222", "label": "2222", "value": 2 }, { "id": 4, "name": "4444", "label": "4444", "value": 4 }], "product_price": "213", "product_num": "21", "product_num_range": [1, 22], "product_sum": "4473", "switch": false, "datetime": "2024-08-25", "datetime2": "2024-08-25", "datetime3": "", "datetime4": "2024-08-22 11:09:04", "remark12": JSON.stringify([{ "label": "选项1", "value": "1" }, { "label": "选项2", "value": "2" }]) })
113
113
  // formRef?.current?.setFieldsValue({"tianchong1":{"label":"选项1","value":"1"}, })
114
114
  }
115
115
  const handleCols = () => {
@@ -189,7 +189,7 @@ function App() {
189
189
  <div className='fw-[960px] frounded fbg-slate-50 fflex fflex-col fitems-center fpb-10'>
190
190
 
191
191
 
192
- <FormContainerWrapper cols={cols} key={"formc"} className="" ref={formRef} >
192
+ <FormContainerWrapper denyEdit={true} cols={cols} key={"formc"} className="" ref={formRef} >
193
193
 
194
194
  <Field.WithSingleSelect
195
195
  ref={testRef}
@@ -50,7 +50,7 @@ function batchElements(elements, groupSize, dmap) {
50
50
  }
51
51
 
52
52
 
53
- const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) => {
53
+ const FormContainer = forwardRef(({ cols = 1, children, mode = "view", denyEdit = false }, ref) => {
54
54
  const [form] = Form.useForm();
55
55
  const formContentRef = React.useRef(null);
56
56
  const [formContent, setFormContent] = React.useState(null);
@@ -114,6 +114,8 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
114
114
  setFieldsValue: (values) => {
115
115
  lockStatus.current = 1;
116
116
  let formData = { ...values, __id: values?.id }
117
+
118
+ // denyEdit 模式下仍需要记录字段变化用于显示/隐藏逻辑
117
119
  for (let key in values) {
118
120
  changedFieldsState.current[key] = { name: key, value: values[key] }
119
121
  if (dependencyMap.current.has(key) && dependencyMap.current.get(key).componentName == "Field.Table" && Array.isArray(values[key])) {
@@ -126,12 +128,19 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
126
128
  }
127
129
  }
128
130
  form.setFieldsValue(formData)
129
- debounceHandleFieldsChange()
131
+
132
+ // denyEdit 模式下跳过复杂的级联处理,但保留简单的显示/隐藏处理
133
+ if (denyEdit) {
134
+ // 只处理显示/隐藏逻辑,不处理填充和数据获取
135
+ debounceHandleFieldsChange_ReadOnly()
136
+ } else {
137
+ debounceHandleFieldsChange()
138
+ }
130
139
  },
131
140
  initializeFieldVisibility: (reloadFields) => {
132
141
  initializeFieldVisibility(reloadFields);
133
142
  },
134
- }), []);
143
+ }), [denyEdit]);
135
144
 
136
145
  // 添加节流后的 initializeFormRender
137
146
  const throttledInitializeFormRender = React.useCallback(
@@ -310,6 +319,54 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
310
319
 
311
320
  return needRefresh;
312
321
  };
322
+
323
+ // denyEdit 模式专用函数 - 只处理显示/隐藏逻辑
324
+ const handleFieldsWithVisibleOnly = async (identifier, fieldValues, init = false, fieldId = null) => {
325
+ if (mode == "desgin") {
326
+ console.log("设计模式下不进行字段级联计算")
327
+ return false
328
+ }
329
+
330
+ console.log("handleFieldsWithVisibleOnly identifier", identifier)
331
+ let needRefresh = false;
332
+ let parentIdentifier = [];
333
+ if (Array.isArray(identifier)) {
334
+ parentIdentifier = [...(identifier.slice(0, -1))]
335
+ identifier = identifier.filter(item => typeof item == "string").join(".")
336
+ }
337
+
338
+ const currentFieldId = fieldId || identifier;
339
+
340
+ try {
341
+ if (dependencyMap.current.has(identifier)) {
342
+ const dependent = dependencyMap.current.get(identifier)
343
+ const dependentChildren = dependent.children;
344
+
345
+ // 只处理显示/隐藏逻辑,跳过填充规则
346
+ for (let index = 0; index < dependentChildren.length; index++) {
347
+ const child = dependentChildren[index];
348
+
349
+ // 处理 withVisibleFunc
350
+ if (child.component.props.withVisibleFunc && typeof child.component.props.withVisibleFunc === 'function') {
351
+ let needRefresh_ = handleFieldsVisibleFunc(fieldValues, child, parentIdentifier)
352
+ needRefresh = needRefresh || needRefresh_
353
+ }
354
+
355
+ // 处理 withVisible(但跳过数据获取部分)
356
+ if (child.component.props.withVisible) {
357
+ let needRefresh_ = await handleFieldsVisibleReadOnly(fieldValues, child, parentIdentifier, dependent?.componentName)
358
+ needRefresh = needRefresh || needRefresh_
359
+ }
360
+
361
+ // 跳过 withFill 处理
362
+ }
363
+ }
364
+ } catch (error) {
365
+ console.log("handleFieldsWithVisibleOnly error", error)
366
+ }
367
+
368
+ return needRefresh;
369
+ };
313
370
  const removeLastFieldsValues = (name, isTable = false) => {
314
371
  if (!lastFormValues.current) return
315
372
  if (isTable) {
@@ -346,6 +403,7 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
346
403
  const handleFillRules = (current_identifier, parentIdentifier, fieldValues, fillRules) => {
347
404
  // 获取当前变更的字段数据
348
405
  let current_value = getParamValue("fieldsValue", current_identifier, fieldValues, null)
406
+ console.log("handleFillRules current_value" + current_identifier, current_value)
349
407
  let changedFields = {}
350
408
 
351
409
  // 将标识符标准化为字符串
@@ -560,6 +618,61 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
560
618
  }
561
619
  return needRefresh
562
620
  }
621
+
622
+ // denyEdit 模式专用 - 处理级联显示隐藏(跳过数据获取)
623
+ const handleFieldsVisibleReadOnly = async (fieldValues, child, parentIdentifier, componentName) => {
624
+ let needRefresh = false;
625
+ const withFill = child?.component?.props.withVisible;
626
+ let withFillIndex = 0
627
+ let withFillGroup = ""
628
+ let childIdentifier = child.identifier;
629
+
630
+ // 跳过数据获取部分,使用空数组作为 withDatas
631
+ let withDatas = [];
632
+
633
+ // 构造计算公式(但不获取远程数据)
634
+ let formula;
635
+ if (withFill.value && withFill.value.length > 0) {
636
+ formula = withFill.value.map(item => {
637
+ let result = "";
638
+ const { insert, attributes } = item
639
+ if (typeof insert !== "string") {
640
+ if (insert?.span && attributes && attributes.tagKey && attributes.id) {
641
+ result = getParamValue(attributes.tagKey, attributes.id, fieldValues, withDatas)
642
+ if (Array.isArray(result)) {
643
+ if (Array.isArray(childIdentifier) && result.length > withFillIndex) {
644
+ result = result[withFillIndex]
645
+ }
646
+ if (typeof result === "object" && result !== null)
647
+ result = JSON.stringify(result)
648
+ }
649
+ else if (typeof result === "object" && result !== null) {
650
+ result = JSON.stringify(result)
651
+ }
652
+ else if (result.length > 0) result = `"${result}"`
653
+ }
654
+ }
655
+ else result = insert
656
+ return result
657
+ })
658
+ }
659
+
660
+ if (dependencyMap.current.has(childIdentifier)) {
661
+ const childData = dependencyMap.current.get(childIdentifier);
662
+ if (formula && formula.length > 0) {
663
+ const formulaResult = (evalFormula(formula) == "true");
664
+ console.log(`${childIdentifier} 只读模式计算公式:`, formula)
665
+ console.log(`${childIdentifier} 只读模式计算结果:`, formulaResult)
666
+ if (childData.show != formulaResult) {
667
+ childData.show = formulaResult;
668
+ dependencyMap.current.set(childIdentifier, childData);
669
+ needRefresh = true;
670
+ }
671
+ }
672
+ }
673
+ return needRefresh
674
+ }
675
+
563
676
  // 处理级联数据源
564
677
  // 处理级联填充
565
678
  const handleFieldsWithFill = async (fieldValues, child, parentIdentifier, componentName, sourceFieldId) => {
@@ -780,7 +893,59 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
780
893
  updateFormContent();
781
894
  }
782
895
  }, delay);
783
- }, []);
896
+ }, [denyEdit]);
897
+
898
+ // denyEdit 模式专用的防抖处理函数 - 只处理显示/隐藏,不处理填充和数据获取
899
+ const debounceHandleFieldsChange_ReadOnly = React.useCallback(() => {
900
+ if (timeoutRef.current) {
901
+ clearTimeout(timeoutRef.current);
902
+ }
903
+
904
+ // 只读模式使用更短的延迟
905
+ const delay = 50;
906
+
907
+ timeoutRef.current = setTimeout(async () => {
908
+ const currentChanges = { ...changedFieldsState.current };
909
+ changedFieldsState.current = {};
910
+
911
+ const fieldValues = form.getFieldsValue();
912
+ let needRefresh = false;
913
+ if (!lastFormValues.current) lastFormValues.current = {}
914
+
915
+ // 创建已处理字段集合,用于避免多次处理同一字段
916
+ const processedFields = new Set();
917
+ for (let key in currentChanges) {
918
+ try {
919
+ let field = currentChanges[key];
920
+ if (!isEqual(field.value || "", getLastFieldValue(field.name) || "")) {
921
+ // 获取字段标识符(字符串形式)
922
+ const fieldId = Array.isArray(field.name)
923
+ ? field.name.map(item => typeof item == "string" ? item : item.toString()).join(".")
924
+ : field.name;
925
+
926
+ // 跳过已处理的字段
927
+ if (processedFields.has(fieldId)) {
928
+ console.log("跳过已处理的字段 processedFields", fieldId)
929
+ continue;
930
+ }
931
+ processedFields.add(fieldId);
932
+
933
+ // 只处理显示/隐藏逻辑,不处理填充和数据获取
934
+ let needRefresh_ = await handleFieldsWithVisibleOnly(field.name, fieldValues, false, fieldId);
935
+ needRefresh = needRefresh || needRefresh_;
936
+
937
+ lastFormValues.current[normalizeFieldPath(field.name)] = field.value;
938
+ }
939
+ } catch (error) {
940
+ console.log("debounceHandleFieldsChange_ReadOnly error", error)
941
+ }
942
+ }
943
+ if (needRefresh) {
944
+ console.log("needRefresh (ReadOnly mode)", needRefresh)
945
+ updateFormContent();
946
+ }
947
+ }, delay);
948
+ }, [denyEdit]);
784
949
 
785
950
  const handleFieldsChange = React.useCallback((changedFields) => {
786
951
  // console.log("handleFieldsChange (disabled, using handleValuesChange instead)", JSON.stringify(changedFields))
@@ -850,9 +1015,15 @@ const FormContainer = forwardRef(({ cols = 1, children, mode = "view" }, ref) =>
850
1015
  });
851
1016
 
852
1017
  console.log("handleValuesChange changedFieldsState", JSON.parse(JSON.stringify(changedFieldsState.current)));
853
- debounceHandleFieldsChange();
1018
+
1019
+ // denyEdit 模式下使用专用的处理函数
1020
+ if (denyEdit) {
1021
+ debounceHandleFieldsChange_ReadOnly();
1022
+ } else {
1023
+ debounceHandleFieldsChange();
1024
+ }
854
1025
  }
855
- }, []);
1026
+ }, [denyEdit]);
856
1027
 
857
1028
  const getTableWithIds = (ids) => {
858
1029
  let withAllIds = []