From 6cb78fe5ad5ee4937eaa36adb3733f1391a47ea5 Mon Sep 17 00:00:00 2001 From: wutong25 Date: Fri, 24 Nov 2023 14:55:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E5=99=A8=E9=87=8D=E5=A4=8D=E6=89=A7=E8=A1=8C?= =?UTF-8?q?onChange=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/src/renderers/Form.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/amis-core/src/renderers/Form.tsx b/packages/amis-core/src/renderers/Form.tsx index 5773221b4..78dce7bb0 100644 --- a/packages/amis-core/src/renderers/Form.tsx +++ b/packages/amis-core/src/renderers/Form.tsx @@ -530,7 +530,7 @@ export default class Form extends React.Component { reaction( () => store.initedAt, () => { - store.inited && this.emitChange(!!this.props.submitOnChange); + store.inited && this.emitChange(!!this.props.submitOnChange, true); } ) ); @@ -1012,21 +1012,21 @@ export default class Form extends React.Component { return dispatchEvent(type, data); } - async emitChange(submit: boolean) { + async emitChange(submit: boolean, skipIfNothingChanges: boolean = false) { const {onChange, store, submitOnChange, dispatchEvent, data} = this.props; if (!isAlive(store)) { return; } + const diff = difference(store.data, store.pristine); + if (skipIfNothingChanges && !Object.keys(diff).length) { + return; + } + // 提前准备好 onChange 的参数。 // 因为 store.data 会在 await 期间被 WithStore.componentDidUpdate 中的 store.initData 改变。导致数据丢失 - const changeProps = [ - store.data, - difference(store.data, store.pristine), - this.props - ]; - + const changeProps = [store.data, diff, this.props]; const dispatcher = await dispatchEvent( 'change', createObject(data, store.data)