Skip to content

Commit 691099c

Browse files
authored
fix: onFieldsChange not trigger when validating (#580)
* fix: missing one fields change * test: add test case
1 parent c9490df commit 691099c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/useForm.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,9 @@ export class FormStore {
954954
// Do not throw in console
955955
returnPromise.catch<ValidateErrorEntity>(e => e);
956956

957+
// `validating` changed. Trigger `onFieldsChange`
958+
this.triggerOnFieldsChange(namePathList);
959+
957960
return returnPromise as Promise<Store>;
958961
};
959962

tests/validate.test.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,4 +794,58 @@ describe('Form.Validate', () => {
794794
await timeout();
795795
expect(validateTrigger).toBeCalledWith(true);
796796
});
797+
798+
it('should trigger onFieldsChange 3 times', async () => {
799+
const onFieldsChange = jest.fn();
800+
801+
const wrapper = mount(
802+
<Form onFieldsChange={onFieldsChange}>
803+
<InfoField name="test" rules={[{ required: true }]}>
804+
<Input />
805+
</InfoField>
806+
</Form>,
807+
);
808+
809+
await changeValue(getField(wrapper, 'test'), '');
810+
811+
await timeout();
812+
813+
// `validated: false` -> `validated: false` -> `validated: true`
814+
// `validating: false` -> `validating: true` -> `validating: false`
815+
expect(onFieldsChange).toHaveBeenCalledTimes(3);
816+
817+
expect(onFieldsChange).toHaveBeenNthCalledWith(
818+
1,
819+
[
820+
expect.objectContaining({
821+
name: ['test'],
822+
validated: false,
823+
validating: false,
824+
}),
825+
],
826+
expect.anything(),
827+
);
828+
expect(onFieldsChange).toHaveBeenNthCalledWith(
829+
2,
830+
[
831+
expect.objectContaining({
832+
name: ['test'],
833+
validated: false,
834+
validating: true,
835+
}),
836+
],
837+
expect.anything(),
838+
);
839+
expect(onFieldsChange).toHaveBeenNthCalledWith(
840+
3,
841+
[
842+
expect.objectContaining({
843+
name: ['test'],
844+
validated: true,
845+
validating: false,
846+
}),
847+
],
848+
expect.anything(),
849+
);
850+
});
797851
});

0 commit comments

Comments
 (0)