diff --git a/CHANGELOG.md b/CHANGELOG.md index eaf758e9..ee8fcbd9 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 6.4.0 (master) +- Feat: ability to define or override hooks after initialization. +- Feat: Introduced `setHooks()` & `getHooks()` methods. + # 6.3.7 (master) - Fix: handle `onSubmit` hook return promise @@ -268,7 +272,7 @@ fix: #371 #399 #408 - Introduced `onDrop` Field Event Handler. - Introduced `onDrop` Field Hook. - Introduced `files` Field prop. -- Introduced `resetting` && `clearing` computed. +- Introduced `resetting` & `clearing` computed. - Dropzone support. - Ability to pass `onChange` & `onToggle` in constructor. diff --git a/src/Base.ts b/src/Base.ts index 39450f4b..6ccd2171 100755 --- a/src/Base.ts +++ b/src/Base.ts @@ -98,16 +98,12 @@ export default class Base implements BaseInterface { $try( fallback[name], this.$hooks[name], - (this as any).hooks && (this as any).hooks.apply(this, [this])[name], this.noop ).apply(this, [this]); 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 && - (this as any).handlers.apply(this, [this])[name] && - (this as any).handlers.apply(this, [this])[name].apply(this, [this]), fallback, this.noop ).apply(this, [...args]), @@ -803,7 +799,7 @@ export default class Base implements BaseInterface { /** Fields Selector - */ + */ select(path: string, fields: any = null, isStrict: boolean = true) { const $path = parsePath(path); const keys = _.split($path, "."); @@ -849,6 +845,20 @@ export default class Base implements BaseInterface { return cpath !== "" ? this.select(cpath, null, false) : this; } + /** + Set Hooks + */ + setHooks(hooks: any = {}): void { + Object.assign(this.$hooks, hooks); + } + + /** + Get Hooks + */ + getHooks(): any { + return this.$hooks; + } + /** Has Field */ diff --git a/src/Field.ts b/src/Field.ts index 0827b5c4..a0798a5a 100755 --- a/src/Field.ts +++ b/src/Field.ts @@ -274,6 +274,10 @@ export default class Field extends Base implements FieldInterface { this.initMOBXEvent(FieldPropsEnum.observers); this.initMOBXEvent(FieldPropsEnum.interceptors); + // setup hooks & handlers from initialization methods + Object.assign(this.$hooks, (this as any).hooks?.apply(this, [this])); + Object.assign(this.$handlers, (this as any).handlers?.apply(this, [this])); + this.execHook(FieldPropsEnum.onInit); // handle Field onChange Hook diff --git a/src/Form.ts b/src/Form.ts index 2e444e6e..47689282 100755 --- a/src/Form.ts +++ b/src/Form.ts @@ -67,6 +67,10 @@ export default class Form extends Base implements FormInterface { : val ); + // setup hooks & handlers from initialization methods + Object.assign(this.$hooks, (this as any).hooks?.apply(this, [this])); + Object.assign(this.$handlers, (this as any).handlers?.apply(this, [this])); + this.state = new State({ form: this, initial: initial.setup, diff --git a/src/models/BaseInterface.ts b/src/models/BaseInterface.ts index 7b637636..57260848 100644 --- a/src/models/BaseInterface.ts +++ b/src/models/BaseInterface.ts @@ -30,6 +30,8 @@ export interface BaseInterface hasIncrementalKeys: boolean; hasNestedFields: boolean; size: number; + getHooks(): any; + setHooks(hooks: any): void; execHook(name: string, fallback?: any): any; execHandler(name: string, args: any, fallback?: any): any; intercept(opt: any): any;