fix: fix the problem of crash caused by infinite loop in antd scene

This commit is contained in:
liujuping 2022-11-25 21:10:13 +08:00 committed by LeoYuan 袁力皓
parent 256df61dba
commit 73acf6f112
2 changed files with 75 additions and 25 deletions

View File

@ -67,19 +67,31 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
this.stageName = stageName; this.stageName = stageName;
} }
render() { get field() {
const { field } = this.props; return this.props.field;
const { extraProps, componentMeta } = field; }
const { condition, defaultValue } = extraProps;
let visible; get visible() {
const { extraProps } = this.field;
const { condition } = extraProps;
try { try {
visible = typeof condition === 'function' ? condition(field.internalToShellPropEntry()) !== false : true; return typeof condition === 'function' ? condition(this.field.internalToShellPropEntry()) !== false : true;
} catch (error) { } catch (error) {
console.error('exception when condition (hidden) is excuted', error); console.error('exception when condition (hidden) is excuted', error);
} }
const { setter } = field; return true;
}
get setterInfo(): {
setterProps: any;
initialValue: any;
setterType: any;
} {
const { extraProps, componentMeta } = this.field;
const { defaultValue } = extraProps;
const { setter } = this.field;
let setterProps: any = {}; let setterProps: any = {};
let setterType: any; let setterType: any;
let initialValue: any = null; let initialValue: any = null;
@ -94,7 +106,7 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
if (setter.props) { if (setter.props) {
setterProps = setter.props; setterProps = setter.props;
if (typeof setterProps === 'function') { if (typeof setterProps === 'function') {
setterProps = setterProps(field.internalToShellPropEntry()); setterProps = setterProps(this.field.internalToShellPropEntry());
} }
} }
if (setter.initialValue != null) { if (setter.initialValue != null) {
@ -104,36 +116,22 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
setterType = setter; setterType = setter;
} }
let value = null;
if (defaultValue != null && !('defaultValue' in setterProps)) { if (defaultValue != null && !('defaultValue' in setterProps)) {
setterProps.defaultValue = defaultValue; setterProps.defaultValue = defaultValue;
if (initialValue == null) { if (initialValue == null) {
initialValue = defaultValue; initialValue = defaultValue;
} }
} }
if (field.valueState === -1) {
if (this.field.valueState === -1) {
setterProps.multiValue = true; setterProps.multiValue = true;
if (!('placeholder' in setterProps)) { if (!('placeholder' in setterProps)) {
setterProps.placeholder = intl('Multiple Value'); setterProps.placeholder = intl('Multiple Value');
} }
} else {
value = field.getValue();
}
// 当前 field 没有 value 值时,将 initialValue 写入 field
// 之所以用 initialValue而不是 defaultValue 是为了保持跟 props.onInitial 的逻辑一致
if (!this.state?.fromOnChange && value === undefined && isInitialValueNotEmpty(initialValue)) {
const _initialValue = typeof initialValue === 'function' ? initialValue(field.internalToShellPropEntry()) : initialValue;
field.setValue(_initialValue);
value = _initialValue;
}
if (!visible) {
return null;
} }
// 根据是否支持变量配置做相应的更改 // 根据是否支持变量配置做相应的更改
const supportVariable = field.extraProps?.supportVariable; const supportVariable = this.field.extraProps?.supportVariable;
// supportVariableGlobally 只对标准组件生效vc 需要单独配置 // supportVariableGlobally 只对标准组件生效vc 需要单独配置
const supportVariableGlobally = engineConfig.get('supportVariableGlobally', false) && isStandardComponent(componentMeta); const supportVariableGlobally = engineConfig.get('supportVariableGlobally', false) && isStandardComponent(componentMeta);
if (supportVariable || supportVariableGlobally) { if (supportVariable || supportVariableGlobally) {
@ -153,6 +151,53 @@ class SettingFieldView extends Component<SettingFieldViewProps, SettingFieldView
} }
} }
return {
setterProps,
initialValue,
setterType,
};
}
get value() {
return this.field.valueState === -1 ? null : this.field.getValue();
}
initDefaultValue() {
const { initialValue } = this.setterInfo;
if (this.state?.fromOnChange ||
!isInitialValueNotEmpty(initialValue) ||
this.value !== undefined
) {
return;
}
// 当前 field 没有 value 值时,将 initialValue 写入 field
// 之所以用 initialValue而不是 defaultValue 是为了保持跟 props.onInitial 的逻辑一致
const _initialValue = typeof initialValue === 'function' ? initialValue(this.field.internalToShellPropEntry()) : initialValue;
this.field.setValue(_initialValue);
}
componentDidMount() {
this.initDefaultValue();
}
render() {
const field = this.field;
const { extraProps } = field;
const visible = this.visible;
if (!visible) {
return null;
}
const {
setterProps = {},
setterType,
initialValue = null,
} = this.setterInfo;
const value = this.value;
let _onChange = extraProps?.onChange; let _onChange = extraProps?.onChange;
let stageName = this.stageName; let stageName = this.stageName;

View File

@ -61,6 +61,11 @@ export interface FieldExtraProps {
* @todo * @todo
*/ */
liveTextEditing?: Omit<LiveTextEditingConfig, 'propTarget'>; liveTextEditing?: Omit<LiveTextEditingConfig, 'propTarget'>;
/**
* onChange
*/
onChange?: (value: any, field: any) => void;
} }
/** /**