Skip to content

Commit

Permalink
feat: ability to define or override hooks after initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Oct 1, 2023
1 parent b1aec1c commit a81b951
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.

Expand Down
20 changes: 15 additions & 5 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down Expand Up @@ -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, ".");
Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/models/BaseInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a81b951

Please sign in to comment.