mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
fix: 修复 formula 在 wizard 里面,初始设置的值无效的问题 (#2598)
This commit is contained in:
parent
673baa72d8
commit
782e369d90
@ -1,7 +1,11 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import {FormItem, FormControlProps, FormBaseControl} from './Item';
|
import {FormItem, FormControlProps, FormBaseControl} from './Item';
|
||||||
import {evalJS, filter} from '../../utils/tpl';
|
import {evalJS, filter} from '../../utils/tpl';
|
||||||
import {isObjectShallowModified} from '../../utils/helper';
|
import {
|
||||||
|
autobind,
|
||||||
|
isObjectShallowModified,
|
||||||
|
setVariable
|
||||||
|
} from '../../utils/helper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 公式功能控件。
|
* 公式功能控件。
|
||||||
@ -56,9 +60,15 @@ export default class FormulaControl extends React.Component<
|
|||||||
any
|
any
|
||||||
> {
|
> {
|
||||||
inited = false;
|
inited = false;
|
||||||
|
unHook?: () => void;
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const {formInited, initSet} = this.props;
|
const {formInited, initSet, addHook} = this.props;
|
||||||
|
|
||||||
|
this.unHook =
|
||||||
|
initSet !== false && addHook
|
||||||
|
? addHook(this.handleFormInit, 'init')
|
||||||
|
: undefined;
|
||||||
|
|
||||||
// 如果在表单中,还是等初始化数据过来才算
|
// 如果在表单中,还是等初始化数据过来才算
|
||||||
if (formInited === false) {
|
if (formInited === false) {
|
||||||
@ -74,12 +84,27 @@ export default class FormulaControl extends React.Component<
|
|||||||
|
|
||||||
if (this.inited) {
|
if (this.inited) {
|
||||||
autoSet === false || this.autoSet(prevProps);
|
autoSet === false || this.autoSet(prevProps);
|
||||||
} else if (formInited === true || typeof formInited === 'undefined') {
|
} else if (typeof formInited === 'undefined') {
|
||||||
this.inited = true;
|
this.inited = true;
|
||||||
initSet === false || this.initSet();
|
initSet === false || this.initSet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
this.unHook?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
@autobind
|
||||||
|
handleFormInit(data: any) {
|
||||||
|
this.inited = true;
|
||||||
|
const {name} = this.props;
|
||||||
|
const result = this.initSet();
|
||||||
|
|
||||||
|
if (typeof name === 'string' && typeof result !== 'undefined') {
|
||||||
|
setVariable(data, name, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
initSet() {
|
initSet() {
|
||||||
const {formula, data, setPrinstineValue, initSet, condition} = this.props;
|
const {formula, data, setPrinstineValue, initSet, condition} = this.props;
|
||||||
|
|
||||||
@ -95,7 +120,8 @@ export default class FormulaControl extends React.Component<
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result: any = evalJS(formula, data as object);
|
const result: any = evalJS(formula, data as object);
|
||||||
result !== null && setPrinstineValue(result);
|
result !== null && setPrinstineValue?.(result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
autoSet(prevProps: FormControlProps) {
|
autoSet(prevProps: FormControlProps) {
|
||||||
|
Loading…
Reference in New Issue
Block a user