Skip to content

Commit e0590b6

Browse files
authored
fix: should not trigger onFieldsChange when submit (#598)
* test: test driven * fix: not trigger if no need * fix: check logic
1 parent 04b1667 commit e0590b6

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

docs/examples/basic.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ export default () => {
66
const [form] = Form.useForm();
77

88
return (
9-
<Form form={form} preserve={false}>
9+
<Form
10+
form={form}
11+
preserve={false}
12+
onFieldsChange={fields => {
13+
console.error('fields:', fields);
14+
}}
15+
>
1016
<Field name="name">
1117
<Input placeholder="Username" />
1218
</Field>
@@ -32,6 +38,8 @@ export default () => {
3238
) : null;
3339
}}
3440
</Field>
41+
42+
<button type="submit">Submit</button>
3543
</Form>
3644
);
3745
};

src/useForm.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,10 @@ export class FormStore {
830830
const changedFields = fields.filter(({ name: fieldName }) =>
831831
containsNamePath(namePathList, fieldName as InternalNamePath),
832832
);
833-
onFieldsChange(changedFields, fields);
833+
834+
if (changedFields.length) {
835+
onFieldsChange(changedFields, fields);
836+
}
834837
}
835838
};
836839

@@ -856,6 +859,10 @@ export class FormStore {
856859
// Collect result in promise list
857860
const promiseList: Promise<FieldError>[] = [];
858861

862+
// We temp save the path which need trigger for `onFieldsChange`
863+
const TMP_SPLIT = String(Date.now());
864+
const validateNamePathList = new Set<string>();
865+
859866
this.getFieldEntities(true).forEach((field: FieldEntity) => {
860867
// Add field if not provide `nameList`
861868
if (!provideNameList) {
@@ -883,6 +890,8 @@ export class FormStore {
883890
}
884891

885892
const fieldNamePath = field.getNamePath();
893+
validateNamePathList.add(fieldNamePath.join(TMP_SPLIT));
894+
886895
// Add field validate rule in to promise list
887896
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
888897
const promise = field.validateRules({
@@ -961,7 +970,10 @@ export class FormStore {
961970
returnPromise.catch<ValidateErrorEntity>(e => e);
962971

963972
// `validating` changed. Trigger `onFieldsChange`
964-
this.triggerOnFieldsChange(namePathList);
973+
const triggerNamePathList = namePathList.filter(namePath =>
974+
validateNamePathList.has(namePath.join(TMP_SPLIT)),
975+
);
976+
this.triggerOnFieldsChange(triggerNamePathList);
965977

966978
return returnPromise as Promise<Store>;
967979
};

tests/validate.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,43 @@ describe('Form.Validate', () => {
869869
expect(onMetaChange).toHaveBeenNthCalledWith(4, false);
870870
});
871871

872+
it('should not trigger onFieldsChange if no rules', async () => {
873+
const onFieldsChange = jest.fn();
874+
const onFinish = jest.fn();
875+
876+
const App = () => {
877+
return (
878+
<Form
879+
onFieldsChange={onFieldsChange}
880+
initialValues={{
881+
list: ['hello'],
882+
}}
883+
onFinish={onFinish}
884+
>
885+
<Form.List name="list">
886+
{fields =>
887+
fields.map(field => (
888+
<InfoField key={field.key} {...field}>
889+
<Input />
890+
</InfoField>
891+
))
892+
}
893+
</Form.List>
894+
</Form>
895+
);
896+
};
897+
const wrapper = mount(<App />);
898+
899+
wrapper.find('form').simulate('submit');
900+
901+
await timeout();
902+
903+
expect(onFieldsChange).not.toHaveBeenCalled();
904+
expect(onFinish).toHaveBeenCalledWith({
905+
list: ['hello'],
906+
});
907+
});
908+
872909
it('validateOnly', async () => {
873910
const formRef = React.createRef<FormInstance>();
874911
const { container } = render(

0 commit comments

Comments
 (0)