jsun969 / react-hook-form-antd

📋🐜 Master your Ant Design form with React Hook Form! 用 React Hook Form 拿捏你的 Ant Design 表单!
https://npm.im/react-hook-form-antd
MIT License
61 stars 10 forks source link

`reset` does not work #3

Closed jsun969 closed 8 months ago

jsun969 commented 1 year ago
          reset 也无法正常工作

Originally posted by @HiChen404 in https://github.com/jsun969/react-hook-form-antd/issues/2#issuecomment-1641280919

ReallyBean commented 1 year ago

reset function is not working.

jsun969 commented 1 year ago

@cenxky Thx a lot! Would you like to create a PR for this?

jsun969 commented 1 year ago

resolved by #5

jsun969 commented 11 months ago

I added tests but reset didn't pass. I'll reopen this issue.

https://github.com/jsun969/react-hook-form-antd/blob/d84827e6822eaea6a7c90b75bba4d3a18bc031a2/__tests__/Form.test.tsx#L52-L63

avegatolber commented 8 months ago

@jsun969 Hi, adding this code to FormItem solve the problem and the 'reset' test passes successfully. I should create a PR with these changes or you are not convinced by this solution?

The problem seems to be the one mentioned above. Antd has an internal handling of the input state, so I added a useEffect that updates the internal state of antd every time the value of the input controlled by react-hook-form changes.

const form = AntdForm.useFormInstance();

React.useEffect(() => {
  form.setFieldValue(name, field.value);
}, [field.value]);

image

jsun969 commented 8 months ago

I should create a PR with these changes or you are not convinced by this solution?

@avegatolber That's awesome. Just create a PR for it and I'll be appreciate.

avegatolber commented 8 months ago

@jsun969 PR ready