Skip to content

Commit 3ba41a4

Browse files
lemonied轩浪
andauthored
fix: isFieldsTouched return wrong result with param true (#653)
Co-authored-by: 轩浪 <[email protected]>
1 parent 294125e commit 3ba41a4

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed

docs/demo/fieldTouched.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## fieldTouched
2+
3+
<code src="../examples/fieldTouched.tsx"></code>

docs/examples/fieldTouched.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react';
2+
import Form from 'rc-field-form';
3+
4+
export default () => {
5+
6+
const [form] = Form.useForm();
7+
const [, forceUpdate] = React.useState({});
8+
9+
return (
10+
<div>
11+
<Form
12+
form={form}
13+
style={{ padding: 20 }}
14+
initialValues={{
15+
usename: '123',
16+
list: [
17+
{},
18+
],
19+
}}
20+
>
21+
<Form.Field name="username">
22+
<input />
23+
</Form.Field>
24+
<div style={{ paddingLeft: 20 }}>
25+
<Form.List name="list">
26+
{(fields, { add }) => (
27+
<>
28+
{
29+
fields.map((field) => {
30+
return (
31+
<div key={field.key}>
32+
<div>
33+
<Form.Field name={[field.name, 'field1']}>
34+
<input placeholder="field1" />
35+
</Form.Field>
36+
</div>
37+
<div>
38+
<Form.Field name={[field.name, 'field2']}>
39+
<input placeholder="field2" />
40+
</Form.Field>
41+
</div>
42+
</div>
43+
);
44+
})
45+
}
46+
<button onClick={() => add()}>add</button>
47+
</>
48+
)}
49+
</Form.List>
50+
</div>
51+
<div>
52+
<button
53+
onClick={() => {
54+
forceUpdate({});
55+
}}
56+
>forceUpdate</button>
57+
<button
58+
onClick={() => form.resetFields()}
59+
>reset</button>
60+
</div>
61+
<div>form.isFieldsTouched(true): {`${form.isFieldsTouched(true)}`}</div>
62+
<div>{`form.isFieldsTouched(['list'], true)`}: {`${form.isFieldsTouched(['list'], true)}`}</div>
63+
</Form>
64+
</div>
65+
);
66+
}

src/useForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ export class FormStore {
395395
// ===== Will get fully compare when not config namePathList =====
396396
if (!namePathList) {
397397
return isAllFieldsTouched
398-
? fieldEntities.every(isFieldTouched)
398+
? fieldEntities.every(entity => isFieldTouched(entity) || entity.isList())
399399
: fieldEntities.some(isFieldTouched);
400400
}
401401

tests/list.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,4 +845,54 @@ describe('Form.List', () => {
845845
little: 9,
846846
});
847847
});
848+
849+
it('isFieldsTouched with params true', async () => {
850+
const formRef = React.createRef<FormInstance>();
851+
852+
const { container } = render(
853+
<Form
854+
ref={formRef}
855+
initialValues={{
856+
usename: '',
857+
list: [{}],
858+
}}
859+
>
860+
<Form.Field name="username">
861+
<input />
862+
</Form.Field>
863+
<Form.List name="list">
864+
{(fields, { add }) => (
865+
<>
866+
{
867+
fields.map((field) => {
868+
return (
869+
<React.Fragment key={field.key}>
870+
<Form.Field name={[field.name, 'field1']}>
871+
<input placeholder="field1" />
872+
</Form.Field>
873+
<Form.Field name={[field.name, 'field2']}>
874+
<input placeholder="field2" />
875+
</Form.Field>
876+
</React.Fragment>
877+
);
878+
})
879+
}
880+
<button onClick={() => add()}>add</button>
881+
</>
882+
)}
883+
</Form.List>
884+
</Form>
885+
);
886+
887+
expect(formRef.current.isFieldsTouched(true)).toBeFalsy();
888+
889+
await changeValue(getInput(container, 0), 'changed1');
890+
expect(formRef.current.isFieldsTouched(true)).toBeFalsy();
891+
892+
await changeValue(getInput(container, 1), 'changed2');
893+
expect(formRef.current.isFieldsTouched(true)).toBeFalsy();
894+
895+
await changeValue(getInput(container, 2), 'changed3');
896+
expect(formRef.current.isFieldsTouched(true)).toBeTruthy();
897+
});
848898
});

0 commit comments

Comments
 (0)