Skip to content

Commit

Permalink
fix: mobx strict mode for hooks and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Jan 7, 2024
1 parent d737eeb commit ff08c81
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 6.7.2 (master)
- Fix: mobx strict mode for hooks and handlers

# 6.7.1 (master)
- Fix: #632 observable hooks and handlers

Expand Down
4 changes: 2 additions & 2 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ export default class Base implements BaseInterface {
*/
each(iteratee: any, fields: any = null, depth: number = 0) {
const $fields = fields || this.fields;
_.each(getObservableMapValues($fields), (field: FieldInterface, index) => {
getObservableMapValues($fields).forEach((field: FieldInterface, index) => {
iteratee(field, index, depth);

if (field.fields.size !== 0) {
Expand All @@ -928,7 +928,7 @@ export default class Base implements BaseInterface {
}

reduce(iteratee: any, acc: any): any {
return _.reduce(getObservableMapValues(this.fields), iteratee, acc);
return getObservableMapValues(this.fields).reduce(iteratee, acc);
}

/******************************************************************
Expand Down
9 changes: 5 additions & 4 deletions src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
untracked,
makeObservable,
autorun,
runInAction,
} from "mobx";
import _ from "lodash";
import Base from "./Base";
Expand Down Expand Up @@ -275,8 +276,8 @@ export default class Field extends Base implements FieldInterface {
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]));
runInAction(() => Object.assign(this.$hooks, (this as any).hooks?.apply(this, [this])));
runInAction(() => Object.assign(this.$handlers, (this as any).handlers?.apply(this, [this])));

this.execHook(FieldPropsEnum.onInit);

Expand Down Expand Up @@ -749,7 +750,7 @@ export default class Field extends Base implements FieldInterface {
this.$resetting = false;
this.$clearing = false;
}))
if (deep) this.each((field: any) => field.resetValidation(deep));
if (deep) this.each((field: FieldInterface) => field.resetValidation(deep));
}

clear(deep: boolean = true, execHook: boolean = true): void {
Expand Down Expand Up @@ -813,7 +814,7 @@ export default class Field extends Base implements FieldInterface {
showErrors(show: boolean = true): void {
this.showError = show;
this.errorSync = _.head(this.validationErrorStack) as string;
this.each((field: any) => field.showErrors(show));
this.each((field: FieldInterface) => field.showErrors(show));
}

showAsyncErrors(): void {
Expand Down
10 changes: 5 additions & 5 deletions src/Form.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, computed, observable, makeObservable, autorun } from "mobx";
import { action, computed, observable, makeObservable, autorun, runInAction } from "mobx";
import _ from "lodash";

import Base from "./Base";
Expand Down Expand Up @@ -50,8 +50,8 @@ export default class Form extends Base implements FormInterface {
});

this.name = name;
this.$hooks = hooks;
this.$handlers = handlers;
runInAction(() => (this.$hooks = hooks));
runInAction(() => (this.$handlers = handlers));

// load data from initializers methods
const initial = _.each(
Expand All @@ -68,8 +68,8 @@ export default class Form extends Base implements FormInterface {
);

// 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]));
runInAction(() => Object.assign(this.$hooks, (this as any).hooks?.apply(this, [this])));
runInAction(() => Object.assign(this.$handlers, (this as any).handlers?.apply(this, [this])));

this.state = new State({
form: this,
Expand Down

0 comments on commit ff08c81

Please sign in to comment.