amis-saas-6892 [Feature] 纯表达式增加requiredDataPropsVariables属性功能

Change-Id: I304aaf359dc6f4011e33377ff4f3ea0cfb9860ac
This commit is contained in:
jiatianqi 2022-11-10 19:27:36 +08:00
parent 1d88ae8011
commit 7b4195c3a6

View File

@ -12,9 +12,21 @@ import type {VariableItem} from 'amis-ui/lib/components/formula/Editor';
import {resolveVariablesFromScope} from './textarea-formula/utils'; import {resolveVariablesFromScope} from './textarea-formula/utils';
interface ExpressionFormulaControlProps extends FormControlProps { interface ExpressionFormulaControlProps extends FormControlProps {
variables?: any; // 公式变量 /**
*
*/
variables?: Array<VariableItem> | Function;
variableMode?: 'tree' | 'tabs'; /**
* variables 使
* props.variables amis数据域中取变量集合 false;
*/
requiredDataPropsVariables?: boolean;
/**
* 'tabs' 'tree'
*/
variableMode?: 'tabs' | 'tree';
} }
interface ExpressionFormulaControlState { interface ExpressionFormulaControlState {
@ -28,7 +40,8 @@ export default class ExpressionFormulaControl extends React.Component<
ExpressionFormulaControlState ExpressionFormulaControlState
> { > {
static defaultProps: Partial<ExpressionFormulaControlProps> = { static defaultProps: Partial<ExpressionFormulaControlProps> = {
variableMode: 'tabs' variableMode: 'tabs',
requiredDataPropsVariables: false
}; };
isUnmount: boolean; isUnmount: boolean;
@ -43,22 +56,12 @@ export default class ExpressionFormulaControl extends React.Component<
componentDidMount() { componentDidMount() {
this.initFormulaPickerValue(this.props.value); this.initFormulaPickerValue(this.props.value);
this.getVariables();
} }
componentDidUpdate(prevProps: ExpressionFormulaControlProps) { componentDidUpdate(prevProps: ExpressionFormulaControlProps) {
// 优先使用props中的变量数据 if (this.props.data !== prevProps.data) {
if (!this.props.variables) { this.getVariables();
// 从amis数据域中取变量数据
const {node, manager} = this.props.formProps || this.props;
resolveVariablesFromScope(node, manager).then(variables => {
if (Array.isArray(variables)) {
if (!this.isUnmount && !isEqual(variables, this.state.variables)) {
this.setState({
variables: variables
});
}
}
});
} }
if (prevProps.value !== this.props.value) { if (prevProps.value !== this.props.value) {
this.initFormulaPickerValue(this.props.value); this.initFormulaPickerValue(this.props.value);
@ -69,6 +72,34 @@ export default class ExpressionFormulaControl extends React.Component<
this.isUnmount = true; this.isUnmount = true;
} }
// 设置 variables
async getVariables() {
let variablesArr: any[] = [];
const {variables, requiredDataPropsVariables} = this.props;
if (!variables || requiredDataPropsVariables) {
// 从amis数据域中取变量数据
const {node, manager} = this.props.formProps || this.props;
const vars = await resolveVariablesFromScope(node, manager);
if (Array.isArray(vars)) {
if (!this.isUnmount && !isEqual(vars, this.state.variables)) {
variablesArr = vars;
}
}
}
if (variables) {
if (Array.isArray(variables)) {
variablesArr = [...variables, ...variablesArr];
}
if (typeof variables === 'function') {
variablesArr = [...variables(), ...variablesArr];
}
}
this.setState({
variables: variablesArr
});
}
@autobind @autobind
initFormulaPickerValue(value: string) { initFormulaPickerValue(value: string) {
const formulaPickerValue = const formulaPickerValue =
@ -115,12 +146,11 @@ export default class ExpressionFormulaControl extends React.Component<
render() { render() {
const {value, className, variableMode, header, ...rest} = this.props; const {value, className, variableMode, header, ...rest} = this.props;
const {formulaPickerValue} = this.state; const {formulaPickerValue, variables} = this.state;
const variables = this.props.variables || this.state.variables;
const highlightValue = FormulaEditor.highlightValue( const highlightValue = FormulaEditor.highlightValue(
formulaPickerValue, formulaPickerValue,
this.state.variables variables
) || { ) || {
html: formulaPickerValue html: formulaPickerValue
}; };