Skip to content

Commit

Permalink
fix: setFieldValue should reset validation (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Dec 12, 2024
1 parent 3dae665 commit c4fe13c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ export class FormStore {
{
name,
value,
errors: [],
warnings: [],
},
]);
};
Expand Down
27 changes: 27 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormRef>();

const Demo: React.FC = () => (
<Form ref={formRef}>
<Field name="light" rules={[{ validator: () => Promise.reject('No!') }]}>
<Input />
</Field>
</Form>
);

render(<Demo />);

// 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);
});
});

0 comments on commit c4fe13c

Please sign in to comment.