Skip to content

Commit

Permalink
fix: handle onSubmit hook return promise
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 authored and foxhound87 committed Sep 15, 2023
1 parent bbeb549 commit b1aec1c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 6.3.7 (master)
- Fix: handle `onSubmit` hook return promise

# 6.3.6 (master)
- Fix: composer `error()` method.
- Fix: default models/interfaces have now named exports.
Expand All @@ -7,7 +10,7 @@
- Fix: `parseCheckArray` checks for `incremental` field status.
- Updated `semantic-release`

- # 6.3.4 (master)
# 6.3.4 (master)
- Fix: `isEmptyArray` reimplemented in `isArrayFromStruct` util function;
- Removed lodash `_.isArray()` with `Array.isArray()`
- Introduced `struct` prop in `makeField` method.
Expand Down
21 changes: 14 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default class Base implements BaseInterface {
this.noop
).apply(this, [this]);

execHandler = (name: string, args: any, fallback: any = null, hook = null): any => [
execHandler = (name: string, args: any, fallback: any = null, hook = null, execHook = true): any => [
$try(
this.$handlers[name] && this.$handlers[name].apply(this, [this]),
(this as any).handlers &&
Expand All @@ -111,7 +111,7 @@ export default class Base implements BaseInterface {
fallback,
this.noop
).apply(this, [...args]),
this.execHook(hook || name),
execHook && this.execHook(hook || name),
];

get resetting(): boolean {
Expand Down Expand Up @@ -200,8 +200,8 @@ export default class Base implements BaseInterface {
onSubmit = (...args: any): any =>
this.execHandler(FieldPropsEnum.onSubmit, args, (e: Event, o = {}) => {
isEvent(e) && e.preventDefault();
this.submit(o, { execOnSubmitHook: false });
});
this.submit(o);
}, null, false);

/**
Event Handler: On Add
Expand Down Expand Up @@ -336,15 +336,22 @@ export default class Base implements BaseInterface {
execValidationHooks = true,
validate = true
} = {}): Promise<any> {
execOnSubmitHook && this.execHook(FieldPropsEnum.onSubmit, hooks);
const execOnSubmit = () => this.execHook(FieldPropsEnum.onSubmit, hooks);
const submit = execOnSubmitHook ? execOnSubmit() : undefined;
this.$submitting = true;
this.$submitted += 1;

if (!validate || !this.state.options.get(OptionsEnum.validateOnSubmit, this)) {
return Promise
.resolve(this)
.resolve(submit)
.then(action(() => (this.$submitting = false)))
.then(() => this);
.catch(
action((err: any) => {
this.$submitting = false;
throw err;
})
)
.then(() => this)
}

const exec = (isValid: boolean) => isValid
Expand All @@ -357,13 +364,12 @@ export default class Base implements BaseInterface {
})
.then(({ isValid }: any) => {
const handler = execValidationHooks ? exec(isValid) : undefined;
if (isValid) return handler;
if (isValid) return Promise.all([submit, handler]);
const $err = this.state.options.get(OptionsEnum.defaultGenericError, this);
const $throw = this.state.options.get(OptionsEnum.submitThrowsError, this);
if ($throw && $err) (this as any).invalidate();
return handler;
return Promise.all([submit, handler]);
})
// eslint-disable-next-line
.then(action(() => (this.$submitting = false)))
.catch(
action((err: any) => {
Expand Down

0 comments on commit b1aec1c

Please sign in to comment.