From b61cedecb2762bcbfbe32aa5633ce9dfa478de7e Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Wed, 21 Feb 2024 12:22:52 +0800 Subject: [PATCH] fix(LinkageRules): fix appends (#3537) * fix(LinkageRules): fix appends * fix: fix nested rules (T-3152) --- .../client/src/block-provider/hooks/index.ts | 16 ++++++++++++---- .../src/schema-component/antd/action/utils.ts | 8 ++++---- .../schema-component/antd/form-v2/utils.tsx | 14 +++++++++----- .../schema-component/common/utils/uitls.tsx | 19 +++++++++---------- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index c59fe7ddb9..d669f9700e 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -1222,12 +1222,15 @@ export const useAssociationNames = () => { // 根据联动规则中条件的字段获取一些 appends if (s['x-linkage-rules']) { - const rules = s['x-linkage-rules']; - rules.forEach(({ condition }) => { - const type = Object.keys(condition)[0] || '$and'; - const list = condition[type]; + const collectAppends = (obj) => { + const type = Object.keys(obj)[0] || '$and'; + const list = obj[type]; list.forEach((item) => { + if ('$and' in item || '$or' in item) { + return collectAppends(item); + } + const fieldNames = getTargetField(item); // 只应该收集关系字段,只有大于 1 的时候才是关系字段 @@ -1235,6 +1238,11 @@ export const useAssociationNames = () => { appends.add(fieldNames.join('.')); } }); + }; + + const rules = s['x-linkage-rules']; + rules.forEach(({ condition }) => { + collectAppends(condition); }); } diff --git a/packages/core/client/src/schema-component/antd/action/utils.ts b/packages/core/client/src/schema-component/antd/action/utils.ts index 7de204e563..50b348a812 100644 --- a/packages/core/client/src/schema-component/antd/action/utils.ts +++ b/packages/core/client/src/schema-component/antd/action/utils.ts @@ -89,7 +89,7 @@ export const linkageAction = async ({ switch (operator) { case ActionType.Visible: - if (await conditionAnalyses({ rules: condition, variables, localVariables })) { + if (await conditionAnalyses({ ruleGroup: condition, variables, localVariables })) { displayResult.push(operator); field.data = field.data || {}; field.data.hidden = false; @@ -101,7 +101,7 @@ export const linkageAction = async ({ field.display = last(displayResult); break; case ActionType.Hidden: - if (await conditionAnalyses({ rules: condition, variables, localVariables })) { + if (await conditionAnalyses({ ruleGroup: condition, variables, localVariables })) { field.data = field.data || {}; field.data.hidden = true; } else { @@ -110,7 +110,7 @@ export const linkageAction = async ({ } break; case ActionType.Disabled: - if (await conditionAnalyses({ rules: condition, variables, localVariables })) { + if (await conditionAnalyses({ ruleGroup: condition, variables, localVariables })) { disableResult.push(true); } field.stateOfLinkageRules = { @@ -121,7 +121,7 @@ export const linkageAction = async ({ field.componentProps['disabled'] = last(disableResult); break; case ActionType.Active: - if (await conditionAnalyses({ rules: condition, variables, localVariables })) { + if (await conditionAnalyses({ ruleGroup: condition, variables, localVariables })) { disableResult.push(false); } field.stateOfLinkageRules = { 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 055a001df9..3972c9925a 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 @@ -46,14 +46,18 @@ export const collectFieldStateOfLinkageRules = ({ switch (operator) { case ActionType.Required: - requiredResult.push(getTempFieldState(conditionAnalyses({ rules: condition, variables, localVariables }), true)); + requiredResult.push( + getTempFieldState(conditionAnalyses({ ruleGroup: condition, variables, localVariables }), true), + ); field.stateOfLinkageRules = { ...field.stateOfLinkageRules, required: requiredResult, }; break; case ActionType.InRequired: - requiredResult.push(getTempFieldState(conditionAnalyses({ rules: condition, variables, localVariables }), false)); + requiredResult.push( + getTempFieldState(conditionAnalyses({ ruleGroup: condition, variables, localVariables }), false), + ); field.stateOfLinkageRules = { ...field.stateOfLinkageRules, required: requiredResult, @@ -63,7 +67,7 @@ export const collectFieldStateOfLinkageRules = ({ case ActionType.None: case ActionType.Hidden: displayResult.push( - getTempFieldState(conditionAnalyses({ rules: condition, variables, localVariables }), operator), + getTempFieldState(conditionAnalyses({ ruleGroup: condition, variables, localVariables }), operator), ); field.stateOfLinkageRules = { ...field.stateOfLinkageRules, @@ -74,7 +78,7 @@ export const collectFieldStateOfLinkageRules = ({ case ActionType.ReadOnly: case ActionType.ReadPretty: patternResult.push( - getTempFieldState(conditionAnalyses({ rules: condition, variables, localVariables }), operator), + getTempFieldState(conditionAnalyses({ ruleGroup: condition, variables, localVariables }), operator), ); field.stateOfLinkageRules = { ...field.stateOfLinkageRules, @@ -111,7 +115,7 @@ export const collectFieldStateOfLinkageRules = ({ valueResult.push(getTempFieldState(true, getValue())); } else { valueResult.push( - getTempFieldState(conditionAnalyses({ rules: condition, variables, localVariables }), getValue()), + getTempFieldState(conditionAnalyses({ ruleGroup: condition, variables, localVariables }), getValue()), ); } field.stateOfLinkageRules = { diff --git a/packages/core/client/src/schema-component/common/utils/uitls.tsx b/packages/core/client/src/schema-component/common/utils/uitls.tsx index 7ce4a268a8..bd001b0404 100644 --- a/packages/core/client/src/schema-component/common/utils/uitls.tsx +++ b/packages/core/client/src/schema-component/common/utils/uitls.tsx @@ -66,24 +66,23 @@ function getAllKeys(obj) { } export const conditionAnalyses = async ({ - rules, + ruleGroup, variables, localVariables, }: { - rules; + ruleGroup; variables: VariablesContextType; localVariables: VariableOption[]; }) => { - if (process.env.NODE_ENV !== 'production') { - if (!variables) { - throw new Error(`conditionAnalyses: variables cannot be ${variables}`); - } - } - - const type = Object.keys(rules)[0] || '$and'; - const conditions = rules[type]; + const type = Object.keys(ruleGroup)[0] || '$and'; + const conditions = ruleGroup[type]; let results = conditions.map(async (condition) => { + // fix https://nocobase.height.app/T-3152 + if ('$and' in condition || '$or' in condition) { + return await conditionAnalyses({ ruleGroup: condition, variables, localVariables }); + } + const jsonlogic = getInnermostKeyAndValue(condition); const operator = jsonlogic?.key;