Skip to content

fixed onValuesChange callback is not triggered when resetting #3547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
const resetType = type || resetTypeFromContext;
const resetValue = getResetValue(resetType);
// reset 不校验
updateFormValue(resetValue, false);
updateFormValue(resetValue, false, true);

if (resetValidating) {
setNeedResetField(true);
Expand Down Expand Up @@ -425,9 +425,6 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
// value 变化通知 watch 事件
form?.getInternalHooks?.(HOOK_MARK)?.notifyWatch?.(name);

// 控制是否需要校验
if (!shouldValidate.current) return;

// value change event
if (typeof name !== 'undefined' && shouldEmitChangeRef.current) {
if (formListName) {
Expand All @@ -442,9 +439,12 @@ const FormItem = forwardRef<FormItemInstance, FormItemProps>((originalProps, ref
}
}

const filterRules = innerRules.filter((item) => (item.trigger || 'change') === 'change');
// 控制是否需要校验
if (shouldValidate.current) {
const filterRules = innerRules.filter((item) => (item.trigger || 'change') === 'change');
filterRules.length && validate('change');
}

filterRules.length && validate('change');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [formValue, snakeName]);

Expand Down
7 changes: 5 additions & 2 deletions packages/components/form/__tests__/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ describe('Form 组件测试', () => {
expect(getByPlaceholderText('input1').value).toEqual('');
fireEvent.click(getByText('setFields'));
expect(getByPlaceholderText('input1').value).toEqual('setFields');
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenCalledTimes(1);

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

fireEvent.click(getByText('reset'));
expect(fn).toHaveBeenCalledTimes(3);

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