fix: 条件表达式在input-table列上不生效、条件表达式变量去除子表及内部变量

This commit is contained in:
hezhihang 2024-06-27 17:57:05 +08:00
parent 47a15b6350
commit 17f332b23c
4 changed files with 57 additions and 49 deletions

View File

@ -14,7 +14,11 @@ import {compile} from 'path-to-regexp';
import type {Schema, PlainObject, FunctionPropertyNames} from '../types';
import {evalExpression, filter} from './tpl';
import {
evalExpression,
evalExpressionWithConditionBuilder,
filter
} from './tpl';
import {IIRendererStore} from '../store';
import {IFormStore} from '../store/form';
import {autobindMethod} from './autobind';
@ -433,8 +437,10 @@ export function isVisible(
return !(
schema.hidden ||
schema.visible === false ||
(schema.hiddenOn && evalExpression(schema.hiddenOn, data)) ||
(schema.visibleOn && !evalExpression(schema.visibleOn, data))
(schema.hiddenOn &&
evalExpressionWithConditionBuilder(schema.hiddenOn, data)) ||
(schema.visibleOn &&
!evalExpressionWithConditionBuilder(schema.visibleOn, data))
);
}
@ -479,7 +485,8 @@ export function isDisabled(
) {
return (
schema.disabled ||
(schema.disabledOn && evalExpression(schema.disabledOn, data))
(schema.disabledOn &&
evalExpressionWithConditionBuilder(schema.disabledOn, data))
);
}
@ -492,7 +499,7 @@ export function hasAbility(
return schema.hasOwnProperty(ability)
? schema[ability]
: schema.hasOwnProperty(`${ability}On`)
? evalExpression(schema[`${ability}On`], data || schema)
? evalExpressionWithConditionBuilder(schema[`${ability}On`], data || schema)
: defaultValue;
}

View File

@ -1417,6 +1417,33 @@ export async function getQuickVariables(that: any, filter?: Function) {
return [];
}
export async function getConditionVariables(that: any, filter?: Function) {
const {node, manager} = that.props.formProps || that.props;
const selfName = that.props?.data?.name;
await manager?.getContextSchemas(node);
const isCell = node.type === 'cell';
const options = await manager?.dataSchema?.getDataPropsAsOptions();
if (Array.isArray(options)) {
const finalVars = [];
const [curOption, superOption] = filterVariablesOfScope(options);
// 如果当前选中是子表列,则过滤掉当前层
const variables = (!isCell ? curOption.children || [] : []).filter(
(item: any) =>
item.value !== selfName && item.type && item.type !== 'array'
);
finalVars.push(...variables);
if (superOption?.children?.length) {
const superVars = superOption?.children.filter(
(item: any) => item.type && item.type !== 'array'
);
finalVars.push(...superVars);
}
return finalVars;
}
return [];
}
export function resolveQuickVariables(
options: any,
quickVars?: VariableItem[],

View File

@ -12,7 +12,7 @@ import {
import cx from 'classnames';
import {FormItem, Button, PickerContainer, ConditionBuilderFields} from 'amis';
import {reaction} from 'mobx';
import {getQuickVariables} from 'amis-editor-core';
import {getConditionVariables} from 'amis-editor-core';
interface ConditionFormulaControlProps extends FormControlProps {
/**
@ -99,31 +99,16 @@ export default class ConditionFormulaControl extends React.Component<
let fieldsArr: ConditionBuilderFields = [];
const {requiredDataPropsFields, fields} = this.props;
if (requiredDataPropsFields) {
const variablesArr = await getQuickVariables(this);
// 自身字段
const selfName = this.props?.data?.name;
const variablesArr = await getConditionVariables(this);
fieldsArr = flattenTree(variablesArr, (item: any) => {
if (
item &&
item.type &&
PropsFieldsMapping[item.type] &&
!item.isMember
) {
if (PropsFieldsMapping[item.type]) {
let obj: any = {
label: item.label,
type: PropsFieldsMapping[item.type],
name: item.value
};
if (selfName === item.value) {
obj = {
...obj,
label: item.label + 'self',
disabled: true
};
}
return obj;
}
})?.filter(item => item);

View File

@ -6,11 +6,11 @@ import React, {ReactNode} from 'react';
import groupBy from 'lodash/groupBy';
import remove from 'lodash/remove';
import cx from 'classnames';
import {ConditionBuilderFields, FormItem, flattenTree} from 'amis';
import {FormItem, flattenTree} from 'amis';
import {
autobind,
getQuickVariables,
getConditionVariables,
isObjectShallowModified
} from 'amis-editor-core';
import ValidationItem, {ValidatorData} from './ValidationItem';
@ -32,13 +32,18 @@ export interface ValidationControlProps extends FormControlProps {
tag: ValidatorTag | ((ctx: any) => ValidatorTag);
}
interface fieldItem {
label: string;
value: any;
}
interface ValidationControlState {
avaliableValids: {
moreValidators: Record<string, Validator>;
defaultValidators: Record<string, Validator>;
builtInValidators: Record<string, Validator>;
};
fields: ConditionBuilderFields;
fields: fieldItem[];
}
export default class ValidationControl extends React.Component<
@ -87,30 +92,14 @@ export default class ValidationControl extends React.Component<
@autobind
async buildFieldsData() {
const variablesArr = await getQuickVariables(this);
// 自身字段
const selfName = this.props.data.name;
const variablesArr = await getConditionVariables(this);
const arr: ConditionBuilderFields = flattenTree(
variablesArr,
(item: any) => {
if (item.value && item.type !== 'array' && !item.isMember) {
let obj: any = {
label: item.label,
value: item.value
};
if (selfName === item.value) {
obj = {
...obj,
label: item.label + 'self',
disabled: true
};
}
return obj;
}
}
)?.filter(item => item);
const arr = flattenTree(variablesArr, (item: any) => {
return {
label: item.label,
value: item.value
};
});
return arr;
}