Skip to content

Commit

Permalink
feat: handle set()/get() for hooks and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Oct 5, 2023
1 parent a81b951 commit d6b9525
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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()`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 12 additions & 14 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 0 additions & 2 deletions src/models/BaseInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d6b9525

Please sign in to comment.