From d6b95252007771761793649cb3f38695dbf57a44 Mon Sep 17 00:00:00 2001 From: foxhound87 Date: Thu, 5 Oct 2023 10:55:40 +0200 Subject: [PATCH] feat: handle `set()/get()` for `hooks` and `handlers` --- CHANGELOG.md | 8 ++++++-- README.md | 2 +- src/Base.ts | 26 ++++++++++++-------------- src/models/BaseInterface.ts | 2 -- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee8fcbd9..dde9efde 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -# 6.4.0 (master) +# 6.5.0 (master) +- Deprecatad: `setHooks()` & `getHooks()` methods. +- Handle `set()/get()` for `hooks` and `handlers` + +- # 6.4.0 (master) - Feat: ability to define or override hooks after initialization. - Feat: Introduced `setHooks()` & `getHooks()` methods. @@ -44,7 +48,7 @@ # 6.2.2 (master) - Fix: mobx cycle detected in computation (get() strict mode) -# 6.2.1 (master) +# 6.2.1 (master) - DEPRECATED - Refactored set value with input function - Deprecated input function on `initial` and `default` props. - Fix: input function applied 2 times when using `set()` diff --git a/README.md b/README.md index 40124bd6..98636056 100755 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ import MobxReactForm from 'mobx-react-form'; const myForm = new MobxReactForm({ fields }, { plugins, hooks }); ``` -> Learn more on the docs about the [Form Instance](https://foxhound87.github.io/mobx-react-form/docs/form/) +> Learn more on the docs about the [Form Instance](https://foxhound87.github.io/mobx-react-form/docs/form/) and the [Form Options](https://foxhound87.github.io/mobx-react-form/docs/form/form-options.html) #### Pass the myForm to a react component diff --git a/src/Base.ts b/src/Base.ts index 6ccd2171..2a05fa5d 100755 --- a/src/Base.ts +++ b/src/Base.ts @@ -504,6 +504,13 @@ export default class Base implements BaseInterface { allowedProps(AllowedFieldPropsTypes.all, Array.isArray(prop) ? prop : [prop]); if (_.isString(prop)) { + if (([ + FieldPropsEnum.hooks, + FieldPropsEnum.handlers + ] as string[]).includes(prop)) { + return this[`$${prop}`]; + } + if (strict && this.fields.size === 0) { const retrieveNullifiedEmptyStrings = this.state.options.get(OptionsEnum.retrieveNullifiedEmptyStrings, this); return parseCheckOutput(this, prop, strict ? retrieveNullifiedEmptyStrings : false); @@ -590,6 +597,11 @@ export default class Base implements BaseInterface { fallbackValueOption: this.state.options.get(OptionsEnum.fallbackValue, this), separated: data, }); + } else if(([ + FieldPropsEnum.hooks, + FieldPropsEnum.handlers, + ] as string[]).includes(prop)) { + Object.assign(this[`$${prop}`], data) } else { _.set(this, `$${prop}`, data); } @@ -845,20 +857,6 @@ 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/models/BaseInterface.ts b/src/models/BaseInterface.ts index 57260848..7b637636 100644 --- a/src/models/BaseInterface.ts +++ b/src/models/BaseInterface.ts @@ -30,8 +30,6 @@ 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;