Просмотр исходного кода

feat: onSubmit add event params, close #1728 (#1730)

pointhalo 2 лет назад
Родитель
Сommit
9c52eea903

+ 3 - 3
packages/semi-foundation/form/foundation.ts

@@ -248,19 +248,19 @@ export default class FormFoundation extends BaseFoundation<BaseFormAdapter> {
         });
     }
 
-    submit(): void {
+    submit(e: any): void {
         const { values } = this.data;
         // validate form
         this.validate()
             .then((resolveValues: any) => {
                 // if valid do submit
                 const _values = this._adapter.cloneDeep(resolveValues);
-                this._adapter.notifySubmit(_values);
+                this._adapter.notifySubmit(_values, e);
             })
             .catch(errors => {
                 const _errors = this._adapter.cloneDeep(errors);
                 const _values = this._adapter.cloneDeep(values);
-                this._adapter.notifySubmitFail(_errors, _values);
+                this._adapter.notifySubmitFail(_errors, _values, e);
             });
     }
 

+ 2 - 2
packages/semi-foundation/form/interface.ts

@@ -12,8 +12,8 @@ export type BasicFieldError = Array<any>;
 
 export interface BaseFormAdapter<P = Record<string, any>, S = Record<string, any>, Values extends object = any> extends DefaultAdapter<P, S> {
     cloneDeep: (val: any, ...rest: any[]) => any;
-    notifySubmit: (values: any) => void;
-    notifySubmitFail: (errors: Record<keyof Values, BasicFieldError>, values: Partial<Values>) => void;
+    notifySubmit: (values: any, e: any) => void;
+    notifySubmitFail: (errors: Record<keyof Values, BasicFieldError>, values: Partial<Values>, e: any) => void;
     forceUpdate: (callback?: () => void) => void;
     notifyChange: (formState: FormState) => void;
     notifyValueChange: (values: any, changedValues: any) => void;

+ 5 - 5
packages/semi-ui/form/baseForm.tsx

@@ -156,11 +156,11 @@ class Form<Values extends Record<string, any> = any> extends BaseComponent<BaseF
         return {
             ...super.adapter,
             cloneDeep,
-            notifySubmit: (values: Values) => {
-                this.props.onSubmit(values);
+            notifySubmit: (values: Values, e: any) => {
+                this.props.onSubmit(values, e);
             },
-            notifySubmitFail: (errors, values) => {
-                this.props.onSubmitFail(errors, values);
+            notifySubmitFail: (errors, values, e: any) => {
+                this.props.onSubmitFail(errors, values, e);
             },
             forceUpdate: (callback?: () => void) => {
                 this.forceUpdate(callback);
@@ -228,7 +228,7 @@ class Form<Values extends Record<string, any> = any> extends BaseComponent<BaseF
 
     submit(e: React.FormEvent<HTMLFormElement>) {
         e.preventDefault();
-        this.foundation.submit();
+        this.foundation.submit(e);
     }
 
     reset(e: React.FormEvent<HTMLFormElement>) {

+ 2 - 2
packages/semi-ui/form/interface.ts

@@ -100,8 +100,8 @@ export interface FormFCChild<K extends Record<string, any> = any> {
 
 export interface BaseFormProps <Values extends Record<string, any> = any> extends Omit<React.FormHTMLAttributes<HTMLFormElement>, 'children' | 'onChange' | 'onSubmit' | 'onReset'> {
     'aria-label'?: React.AriaAttributes['aria-label'];
-    onSubmit?: (values: Values) => void;
-    onSubmitFail?: (errors: Record<keyof Values, FieldError>, values: Partial<Values>) => void;
+    onSubmit?: (values: Values, e?: React.FormEvent<HTMLFormElement>) => void;
+    onSubmitFail?: (errors: Record<keyof Values, FieldError>, values: Partial<Values>, e?: React.FormEvent<HTMLFormElement>) => void;
     onReset?: () => void;
     onValueChange?: (values: Values, changedValue: Partial<Values>) => void;
     onChange?: (formState: FormState<Values>) => void;