From 3ba41a4689e84a7027d06d4362086d694b748882 Mon Sep 17 00:00:00 2001 From: ChenJiYuan <447334358@qq.com> Date: Wed, 28 Feb 2024 14:56:46 +0800 Subject: [PATCH] fix: isFieldsTouched return wrong result with param true (#653) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 轩浪 --- docs/demo/fieldTouched.md | 3 ++ docs/examples/fieldTouched.tsx | 66 ++++++++++++++++++++++++++++++++++ src/useForm.ts | 2 +- tests/list.test.tsx | 50 ++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 docs/demo/fieldTouched.md create mode 100644 docs/examples/fieldTouched.tsx diff --git a/docs/demo/fieldTouched.md b/docs/demo/fieldTouched.md new file mode 100644 index 00000000..eceb43d8 --- /dev/null +++ b/docs/demo/fieldTouched.md @@ -0,0 +1,3 @@ +## fieldTouched + + diff --git a/docs/examples/fieldTouched.tsx b/docs/examples/fieldTouched.tsx new file mode 100644 index 00000000..18770f65 --- /dev/null +++ b/docs/examples/fieldTouched.tsx @@ -0,0 +1,66 @@ +import React from 'react'; +import Form from 'rc-field-form'; + +export default () => { + + const [form] = Form.useForm(); + const [, forceUpdate] = React.useState({}); + + return ( +
+
+ + + +
+ + {(fields, { add }) => ( + <> + { + fields.map((field) => { + return ( +
+
+ + + +
+
+ + + +
+
+ ); + }) + } + + + )} +
+
+
+ + +
+
form.isFieldsTouched(true): {`${form.isFieldsTouched(true)}`}
+
{`form.isFieldsTouched(['list'], true)`}: {`${form.isFieldsTouched(['list'], true)}`}
+
+
+ ); +} \ No newline at end of file diff --git a/src/useForm.ts b/src/useForm.ts index 29ed0c3e..2fdd233c 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -395,7 +395,7 @@ export class FormStore { // ===== Will get fully compare when not config namePathList ===== if (!namePathList) { return isAllFieldsTouched - ? fieldEntities.every(isFieldTouched) + ? fieldEntities.every(entity => isFieldTouched(entity) || entity.isList()) : fieldEntities.some(isFieldTouched); } diff --git a/tests/list.test.tsx b/tests/list.test.tsx index 492bdf47..339bc1c1 100644 --- a/tests/list.test.tsx +++ b/tests/list.test.tsx @@ -845,4 +845,54 @@ describe('Form.List', () => { little: 9, }); }); + + it('isFieldsTouched with params true', async () => { + const formRef = React.createRef(); + + const { container } = render( +
+ + + + + {(fields, { add }) => ( + <> + { + fields.map((field) => { + return ( + + + + + + + + + ); + }) + } + + + )} + +
+ ); + + expect(formRef.current.isFieldsTouched(true)).toBeFalsy(); + + await changeValue(getInput(container, 0), 'changed1'); + expect(formRef.current.isFieldsTouched(true)).toBeFalsy(); + + await changeValue(getInput(container, 1), 'changed2'); + expect(formRef.current.isFieldsTouched(true)).toBeFalsy(); + + await changeValue(getInput(container, 2), 'changed3'); + expect(formRef.current.isFieldsTouched(true)).toBeTruthy(); + }); });