fix(form): 解决form表单的name路径有数 字的时候赋值失败的问题 #3508
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
解决form表单的name路径有数 字的时候赋值失败的问题
#3294
🤔 这个 PR 的性质是?
🔗 相关 Issue
#3294
💡 需求背景和解决方案
如果Form表单的name中包含数字,则setFieldsValue等赋值方法不能正确赋值。
技术细节:
在Form表单的内部,是通过map的方式来存储name,支持number。
但是在setFieldsValue的时候,受限于js的对象Object会自动将number的key变成字符串key,所以并不支持number的name。
以为如下代码示例,例如:
form.setFieldsValue({ level: 'ok', level1: { 5: 'ok', '2': 'ok' }, });
这里的数字5,在对象内实际上是字符串“5”,导致内部的map数据和设置的object数据匹配不上,赋值失败。
解决方案:
业界通用的解决方案是,name支持number,但是内部代码会主动转换为字符串。也就是说数字 1 和字符串 '1' 会被视为同一路径。如果页面中同时存在 name 为 1 和 “1” 的项,不论哪个在前哪个在后,后者都会覆盖前者的值。
📝 更新日志
fix(Form): 修复 name 路径种有数字的时候通过
setFieldValues
赋值失败的问题本条 PR 不需要纳入 Changelog
☑️ 请求合并前的自查清单