fix: linkage rule memory overflow (#2899)

* fix: linkage rule memory overflow

* fix: linkage rule memory overflow

* refactor: linkage rule

* refactor: linkage rule
This commit is contained in:
katherinehhh 2023-10-24 15:52:00 +08:00 committed by GitHub
parent 741e220fd3
commit 266a4ccb08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 28 deletions

View File

@ -61,17 +61,15 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => {
useEffect(() => { useEffect(() => {
const id = uid(); const id = uid();
form.addEffects(id, () => { form.addEffects(id, () => {
linkageFields?.forEach((v) => { if (linkageFields?.length > 0) {
if (v) { //支持深层次子表单
//支持深层次子表单 onFieldChange('*', (fieldPath: any) => {
onFieldChange('*', (fieldPath: any) => { if (linkageFields.includes(fieldPath.props.name) && field.value) {
if (fieldPath.props.name === v && field.value) { props.onChange(null);
props.onChange(null); setInnerValue(null);
setInnerValue(null); }
} });
}); }
}
});
}); });
return () => { return () => {

View File

@ -1,6 +1,14 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { FormLayout } from '@formily/antd-v5'; import { FormLayout } from '@formily/antd-v5';
import { createForm, Field, Form as FormilyForm, onFieldChange, onFieldInit, onFormInputChange } from '@formily/core'; import {
createForm,
Field,
Form as FormilyForm,
onFieldChange,
onFieldInit,
onFieldReact,
onFormInputChange,
} from '@formily/core';
import { FieldContext, FormContext, observer, RecursionField, useField, useFieldSchema } from '@formily/react'; import { FieldContext, FormContext, observer, RecursionField, useField, useFieldSchema } from '@formily/react';
import { autorun } from '@formily/reactive'; import { autorun } from '@formily/reactive';
import { uid } from '@formily/shared'; import { uid } from '@formily/shared';
@ -111,12 +119,14 @@ const WithForm = (props: WithFormProps) => {
if (h.targetFields?.length) { if (h.targetFields?.length) {
const fields = h.targetFields.join(','); const fields = h.targetFields.join(',');
onFieldInit(`*(${fields})`, (field: any, form) => { onFieldInit(`*(${fields})`, (field: any, form) => {
field['initProperty'] = { setTimeout(() => {
display: field.display, field['initProperty'] = field?.['initProperty'] ?? {
required: field.required, display: field.display,
pattern: field.pattern, required: field.required,
value: field.value || field.initialValue, pattern: field.pattern,
}; value: field.value || field.initialValue,
};
});
}); });
onFieldChange(`*(${fields})`, ['value', 'required', 'pattern', 'display'], (field: any) => { onFieldChange(`*(${fields})`, ['value', 'required', 'pattern', 'display'], (field: any) => {
field.linkageProperty = { field.linkageProperty = {
@ -127,9 +137,9 @@ const WithForm = (props: WithFormProps) => {
// `onFieldReact` 有问题,没有办法被取消监听,所以这里用 `onFieldInit` 代替 // `onFieldReact` 有问题,没有办法被取消监听,所以这里用 `onFieldInit` 代替
onFieldInit(`*(${fields})`, (field: any, form) => { onFieldInit(`*(${fields})`, (field: any, form) => {
disposes.push( disposes.push(
autorun(() => { autorun(async () => {
linkagefields.push(field); linkagefields.push(field);
linkageMergeAction({ await linkageMergeAction({
operator: h.operator, operator: h.operator,
value: h.value, value: h.value,
field, field,

View File

@ -43,8 +43,12 @@ export const linkageMergeAction = async ({
...field.linkageProperty, ...field.linkageProperty,
required: requiredResult, required: requiredResult,
}; };
field.required = last(field.linkageProperty?.required); return new Promise((resolve) => {
break; setTimeout(() => {
field.required = last(field.linkageProperty.required);
});
resolve(void 0);
});
case ActionType.InRequired: case ActionType.InRequired:
if (await conditionAnalyses({ rules: condition, formValues: values, variables, localVariables })) { if (await conditionAnalyses({ rules: condition, formValues: values, variables, localVariables })) {
requiredResult.push(false); requiredResult.push(false);
@ -53,8 +57,12 @@ export const linkageMergeAction = async ({
...field.linkageProperty, ...field.linkageProperty,
required: requiredResult, required: requiredResult,
}; };
field.required = last(field.linkageProperty?.required); return new Promise((resolve) => {
break; setTimeout(() => {
field.required = last(field.linkageProperty.required);
});
resolve(void 0);
});
case ActionType.Visible: case ActionType.Visible:
case ActionType.None: case ActionType.None:
case ActionType.Hidden: case ActionType.Hidden:
@ -65,8 +73,12 @@ export const linkageMergeAction = async ({
...field.linkageProperty, ...field.linkageProperty,
display: displayResult, display: displayResult,
}; };
field.display = last(field.linkageProperty?.display); return new Promise((resolve) => {
break; setTimeout(() => {
field.display = last(displayResult);
});
resolve(void 0);
});
case ActionType.Editable: case ActionType.Editable:
case ActionType.ReadOnly: case ActionType.ReadOnly:
case ActionType.ReadPretty: case ActionType.ReadPretty:
@ -77,8 +89,12 @@ export const linkageMergeAction = async ({
...field.linkageProperty, ...field.linkageProperty,
pattern: patternResult, pattern: patternResult,
}; };
field.pattern = last(field.linkageProperty.pattern); return new Promise((resolve) => {
break; setTimeout(() => {
field.pattern = last(patternResult);
});
resolve(void 0);
});
case ActionType.Value: case ActionType.Value:
if ( if (
isConditionEmpty(condition) || isConditionEmpty(condition) ||