Skip to content

Commit

Permalink
fix: extend hooks and handlers on form instance using set()
Browse files Browse the repository at this point in the history
  • Loading branch information
foxhound87 committed Jan 7, 2024
1 parent 63ea8c8 commit 9d748ab
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 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.4 (master)
- Fix: extend hooks and handlers on form instance using set()

# 6.7.3 (master)
- Fix: reset validation action for mobx strict mode

Expand Down
15 changes: 9 additions & 6 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,13 @@ export default class Base implements BaseInterface {
// UPDATE CUSTOM PROP
if (_.isString(prop) && !_.isUndefined(data)) {
allowedProps(AllowedFieldPropsTypes.editable, [prop]);
const deep = (_.isObject(data) && prop === FieldPropsEnum.value) || _.isPlainObject(data);

const isPlain = ([
FieldPropsEnum.hooks,
FieldPropsEnum.handlers,
] as string[]).includes(prop);

const deep: boolean = (_.isObject(data) && prop === FieldPropsEnum.value) || (_.isPlainObject(data) && !isPlain);
if (deep && this.hasNestedFields) return this.deepSet(prop, data, "", true);

if (prop === FieldPropsEnum.value) {
Expand All @@ -604,11 +610,8 @@ 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 if(isPlain) {
Object.assign(this[`$${prop}`], data);
} else {
_.set(this, `$${prop}`, data);
}
Expand Down
9 changes: 8 additions & 1 deletion tests/data/forms/fixes/form.519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const fields = [
},
};

export default new Form({ fields }, {
const form = new Form({ fields }, {
plugins,
hooks,
name: '$519',
Expand All @@ -75,3 +75,10 @@ const fields = [
retrieveOnlyEnabledFieldsValues: true,
validateDisabledFields: false
}});

form.set('hooks', {
onSuccess(form: FormInterface) {},
onError(form: FormInterface) {}
})

export default form;
2 changes: 2 additions & 0 deletions tests/fixes.labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ describe("Check Fixes $425 labels", () => {
"3a": "3aa",
}));
});


6 changes: 6 additions & 0 deletions tests/fixes.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,10 @@ describe('Check Fixes $519', () => {

it('$519 week value should be equal to empty object', () =>
expect($.$519.$('week').value).to.be.deep.equal({}));
});


describe("Check Fixes $519 hooks", () => {
it("$519 $hooks check onSuccess", () => expect($.$519.$hooks).to.have.property('onSuccess'));
it("$519 $hooks check onError", () => expect($.$519.$hooks).to.have.property('onError'));
});

0 comments on commit 9d748ab

Please sign in to comment.