Skip to content

使用useForm 时,当没有初始化 field 的时候调用resetFields无法重置field的值 #7089

@ctocto

Description

@ctocto
  • I have searched the issues of this repository and believe that this is not a duplicate.

Version

4.0.6

Environment

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36, Vue 3.2.36

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  1. 使用 useForm 运行Form;
  2. 定义 form model 时,不定义具体的fields;
  3. 在form 控件(如:Input)中输入值;
  4. 重置按钮调用 resetFields;

What is expected?

field 的值被重置为 undefined

What is actually happening?

field 的值还是输入时候的值


使用useForm 的时候,此种情况无法重置fields 的值,但是使用ref 的方式是可以重置的

查看源码发现在useForm resetFields中是通过 Object.assign 来对象合并覆盖实现的,所以如果没有定义fields 就不会被覆盖。

而用 ref 的方式时resetField 是单个赋值重置的。所以即使没有定义也正好被重置为undefined了。

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions