Skip to content
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

Input 类组件的值为空时,打印清空时的值,表现不一致 #40426

Closed
chenxiaochun opened this issue Jan 28, 2023 · 3 comments
Closed

Comments

@chenxiaochun
Copy link
Contributor

Reproduction link

https://ant.design/~demos/components-form-demo-nest-messages

Steps to reproduce

1、当第一次打开 demo,直接点击 submit,所有非必填项的值打印的都是 undefined

2、age 字段,随意输入一些值,再清空。然后打印的值变成了 null

3、其它字符串输入框,也随意输入一些值,再清空。然后打印的值变成了空字符串

What is expected?

因此,我个人认为,在上面三种情况中,它们被清空之后的值始终都应该保持为 undefined

What is actually happening?

实际情况中,并不一致

Environment Info
antd 5.1.6
React 17
System mac
Browser chrome
@zombieJ
Copy link
Member

zombieJ commented Jan 28, 2023

Form 的 resetFields 会重置为 undefined,而组件本身的清空则会是 null。两者区别在于 React 中组件 undefined 会被认为是非受控组件,所以在处理的时候会使用 null 表示清空(如果是用户删除的话,在 Input 这类组件则是空字符串)。而 Form 则是将 Form store 恢复成空对象,只是在收集 Fields 的时候遍历 store 中对应的路径组合成一个新对象返回,所以看起来是 undefiend 但是实际上真实的 Form Store 是空值,你可以通过 getFieldsValue(true) 查看真实的 Form Store 值。

Form 本身并不关注组件如果和实现清空的逻辑,其本身清空总是重置 Form Store。组件则可以实现自己的清空逻辑,而 antd 中如上述受控、非受控 因素选择 null 哈~

@zombieJ zombieJ closed this as completed Jan 28, 2023
@chenxiaochun
Copy link
Contributor Author

感谢回复的如此及时哈。我理解一下你说的意思

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants