From c4fe13cedc5fda788d145d6b02c26bcc8ff040e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Thu, 12 Dec 2024 11:23:57 +0800 Subject: [PATCH] fix: setFieldValue should reset validation (#744) --- src/useForm.ts | 2 ++ tests/index.test.tsx | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/useForm.ts b/src/useForm.ts index 1be9f70d..7879cf85 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -787,6 +787,8 @@ export class FormStore { { name, value, + errors: [], + warnings: [], }, ]); }; diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 7f7ef718..a9a0e740 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -996,4 +996,31 @@ describe('Form.Basic', () => { expect(formRef.current?.getFieldError('light')).toHaveLength(0); expect(formRef.current?.getFieldError('bamboo')).toHaveLength(0); }); + + it('setFieldValue should reset errors', async () => { + const formRef = React.createRef(); + + const Demo: React.FC = () => ( +
+ Promise.reject('No!') }]}> + + +
+ ); + + render(); + + // Mock error first + await act(async () => { + await formRef.current?.validateFields().catch(() => {}); + }); + expect(formRef.current?.getFieldError('light')).toHaveLength(1); + + // setFieldValue + await act(async () => { + formRef.current?.setFieldValue('light', 'Bamboo'); + await Promise.resolve(); + }); + expect(formRef.current?.getFieldError('light')).toHaveLength(0); + }); });