Skip to content

Commit

Permalink
fix(Form): setFieldsValue and setFields should trigger onValuesChange (
Browse files Browse the repository at this point in the history
…#3232)

* fix(Form): setFields and setFieldsValue should trigger valueChanges

* fix(Form): setFields and setFieldsValue should trigger valueChanges

* chore: unit test
  • Loading branch information
uyarn authored Nov 27, 2024
1 parent fe9fb7a commit 7c203bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
}
if (typeof value !== 'undefined') {
// 手动设置 status 则不需要校验 交给用户判断
updateFormValue(value, typeof status === 'undefined' ? true : false);
updateFormValue(value, typeof status === 'undefined' ? true : false, true);
}
}

Expand Down Expand Up @@ -458,7 +458,7 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
value: formValue,
isUpdated: isUpdatedRef.current,
getValue: () => valueRef.current,
setValue: (newVal: any) => updateFormValue(newVal),
setValue: (newVal: any) => updateFormValue(newVal, true, true),
setField,
validate,
validateOnly,
Expand Down
8 changes: 7 additions & 1 deletion src/form/__tests__/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ describe('Form 组件测试', () => {
});

test('form instance', async () => {
const fn = vi.fn();

const TestForm = () => {
const [form] = Form.useForm();

Expand Down Expand Up @@ -71,7 +73,7 @@ describe('Form 组件测试', () => {
}

return (
<Form form={form} labelWidth={100} colon>
<Form form={form} labelWidth={100} colon onValuesChange={fn}>
<FormItem label="input1" name="input1" rules={[{ required: true, message: 'input1 未填写', type: 'error' }]}>
<Input placeholder="input1" />
</FormItem>
Expand All @@ -93,8 +95,12 @@ describe('Form 组件测试', () => {
expect(getByPlaceholderText('input1').value).toEqual('');
fireEvent.click(getByText('setFields'));
expect(getByPlaceholderText('input1').value).toEqual('setFields');
expect(fn).toHaveBeenCalled();

fireEvent.click(getByText('setFieldsValue'));
expect(getByPlaceholderText('input1').value).toEqual('setFieldsValue');
expect(fn).toHaveBeenCalled();

fireEvent.click(getByText('setValidateMessage'));
expect(queryByText('message: setValidateMessage')).toBeTruthy();

Expand Down

0 comments on commit 7c203bd

Please sign in to comment.