Skip to content

fix(form): 解决form表单的name路径有数 字的时候赋值失败的问题 #3508

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

Conversation

wonkzhang
Copy link
Collaborator

@wonkzhang wonkzhang commented Apr 16, 2025

解决form表单的name路径有数 字的时候赋值失败的问题

#3294

🤔 这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

🔗 相关 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

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

解决form表单的name路径有数 字的时候赋值失败的问题

Tencent#3294
Copy link

pkg-pr-new bot commented Apr 16, 2025

Open in StackBlitz

npm i https://pkg.pr.new/tdesign-react@3508

commit: 7851852

Copy link
Contributor

完成

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

Successfully merging this pull request may close these issues.

2 participants