fix(LinkageRules): fix appends (#3537)

* fix(LinkageRules): fix appends

* fix: fix nested rules (T-3152)
This commit is contained in:
Zeke Zhang 2024-02-21 12:22:52 +08:00 committed by GitHub
parent bb46697e71
commit b61cedecb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 23 deletions

View File

@ -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);
});
}

View File

@ -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 = {

View File

@ -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 = {

View File

@ -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;