From bb46697e7178a48b9b4d260c139d7a96a988422b Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Wed, 21 Feb 2024 11:22:47 +0800 Subject: [PATCH] fix(LinkageRules): avoid to change observable object (#3538) --- .../client/src/schema-component/antd/form-v2/Form.tsx | 6 ++---- .../client/src/schema-component/antd/form-v2/utils.tsx | 2 -- .../core/client/src/variables/VariablesProvider.tsx | 10 ++++++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/form-v2/Form.tsx b/packages/core/client/src/schema-component/antd/form-v2/Form.tsx index 2906e5223..1643db46c 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/Form.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/Form.tsx @@ -149,7 +149,7 @@ const WithForm = (props: WithFormProps) => { return result; }, - getSubscriber(action, field, rule, form, variables, localVariables), + getSubscriber(action, field, rule, variables, localVariables), { fireImmediately: true }, ), ); @@ -229,7 +229,6 @@ function getSubscriber( action: any, field: any, rule: any, - form: FormilyForm, variables: VariablesContextType, localVariables: VariableOption[], ): (value: string, oldValue: string) => void { @@ -240,7 +239,6 @@ function getSubscriber( value: action.value, field, condition: rule.condition, - values: form.values, variables, localVariables, }); @@ -251,7 +249,7 @@ function getSubscriber( const fieldName = getFieldNameByOperator(action.operator); // 防止重复赋值 - if (!field.stateOfLinkageRules[fieldName]) { + if (!field.stateOfLinkageRules?.[fieldName]) { return; } diff --git a/packages/core/client/src/schema-component/antd/form-v2/utils.tsx b/packages/core/client/src/schema-component/antd/form-v2/utils.tsx index e87c45508..055a001df 100644 --- a/packages/core/client/src/schema-component/antd/form-v2/utils.tsx +++ b/packages/core/client/src/schema-component/antd/form-v2/utils.tsx @@ -14,7 +14,6 @@ interface Props { [key: string]: any; }; condition; - values; variables: VariablesContextType; localVariables: VariableOption[]; } @@ -36,7 +35,6 @@ export const collectFieldStateOfLinkageRules = ({ value, field, condition, - values, variables, localVariables, }: Props) => { diff --git a/packages/core/client/src/variables/VariablesProvider.tsx b/packages/core/client/src/variables/VariablesProvider.tsx index 0dd15911f..ba61b3121 100644 --- a/packages/core/client/src/variables/VariablesProvider.tsx +++ b/packages/core/client/src/variables/VariablesProvider.tsx @@ -1,4 +1,4 @@ -import { untracked } from '@formily/reactive'; +import { raw, untracked } from '@formily/reactive'; import { getValuesByPath } from '@nocobase/utils/client'; import _ from 'lodash'; import React, { createContext, useCallback, useEffect, useMemo, useRef } from 'react'; @@ -124,7 +124,13 @@ const VariablesProvider = ({ children }) => { data = await waitForData; clearRequested(url); } - current[key] = data.data.data; + + // fix https://nocobase.height.app/T-3144,使用 `raw` 方法是为了避免触发 autorun,以修复 T-3144 的错误 + if (!raw(current)[key]) { + // 把接口返回的数据保存起来,避免重复请求 + raw(current)[key] = data.data.data; + } + current = getValuesByPath(current, key); } else { current = getValuesByPath(current, key);