From 7f48846bfe7021535aa90955a5cfc7fab31a827b Mon Sep 17 00:00:00 2001 From: pianruijie <13522335863@163.com> Date: Tue, 6 Sep 2022 17:04:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20for=E6=94=AF=E6=8C=81pureSubmit?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh-CN/components/form/index.md | 1 + .../cmpt-event-action/FormEvent.jsx | 37 +++++++++++++++++++ packages/amis-core/src/renderers/Form.tsx | 16 +++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/docs/zh-CN/components/form/index.md b/docs/zh-CN/components/form/index.md index b15fbdfa5..a838fcf97 100755 --- a/docs/zh-CN/components/form/index.md +++ b/docs/zh-CN/components/form/index.md @@ -1458,6 +1458,7 @@ Form 支持轮询初始化接口,步骤如下: | formItemValidateError | `event.data: object` 当前表单数据 | 表单项校验失败时触发 | | validateSucc | `event.data: object` 当前表单数据 | 表单校验成功时触发 | | validateError | `event.data: object` 当前表单数据 | 表单校验失败时触发 | +| pureSubmit | `event.data: object` 当前表单数据 | 表单提交后触发,配置了该事件将不会触发表单提交时默认的校验、提交到api或者target等行为,所有行为需要自己配置 | | submitSucc | `event.data.result: object` api 远程请求成功后返回的结果数据 | 提交成功时触发 | | submitFail | `event.data.error: object` api 远程请求失败后返回的错误信息 | 提交失败时触发 | diff --git a/examples/components/EventAction/cmpt-event-action/FormEvent.jsx b/examples/components/EventAction/cmpt-event-action/FormEvent.jsx index 2dcf3cd81..3617e2b9e 100644 --- a/examples/components/EventAction/cmpt-event-action/FormEvent.jsx +++ b/examples/components/EventAction/cmpt-event-action/FormEvent.jsx @@ -276,6 +276,43 @@ export default { ] } } + }, + { + type: 'form', + debug: true, + api: '/api/mock2/form/saveForm', + title: "表单:配置pureSubmit事件后将不会触发表单提交时默认的校验、提交到api或者target等行为,所有行为需要自己配置", + body: [ + { + type: 'input-text', + name: 'name', + label: '姓名', + required: true, + validateOnChange: true + }, + { + type: 'input-text', + name: 'email', + label: '邮箱', + required: true, + validateOnChange: true, + validations: { + isEmail: true + } + } + ], + onEvent: { + pureSubmit: { + actions: [ + { + actionType: 'toast', + args: { + msg: '提交成功:${event.data|json}' + } + } + ] + }, + } } ] }; diff --git a/packages/amis-core/src/renderers/Form.tsx b/packages/amis-core/src/renderers/Form.tsx index 52de0916c..f87f540b3 100644 --- a/packages/amis-core/src/renderers/Form.tsx +++ b/packages/amis-core/src/renderers/Form.tsx @@ -1014,7 +1014,8 @@ export default class Form extends React.Component { clearPersistDataAfterSubmit, trimValues, dispatchEvent, - translate: __ + translate: __, + $schema } = this.props; // 做动作之前,先把数据同步一下。 @@ -1822,7 +1823,7 @@ export class FormRenderer extends Form { return this.handleAction(undefined, action, data, throwErrors); } - handleAction( + async handleAction( e: React.UIEvent | undefined, action: ActionObject, ctx: object, @@ -1833,6 +1834,17 @@ export class FormRenderer extends Form { // if (this.props.disabled) { // return; // } + + // 配了pureSubmit事件的表示将提交逻辑全部托管给事件 + const {dispatchEvent, $schema} = this.props; + const pureSubmitEvent = $schema?.onEvent?.pureSubmit; + const dispatcher = await dispatchEvent( + 'pureSubmit', + this.props.data + ); + if (dispatcher?.prevented || pureSubmitEvent) { + return; + } if (action.target && action.actionType !== 'reload') { const scoped = this.context as IScopedContext;