Skip to content

Conversation

@QDyanbing
Copy link

@QDyanbing QDyanbing commented Dec 17, 2025

fix ant-design/ant-design#56245

修复更新form-list非最后一项数据导致的数据丢失问题

Summary by CodeRabbit

发布说明

  • Bug Fixes

    • 修复了更新列表字段时列表项可能丢失的问题
  • Tests

    • 添加测试用例以验证列表项更新时保持正确顺序和完整性

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Dec 17, 2025

@QDyanbing is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 17, 2025

Walkthrough

修复了 Form.List 字段值合并逻辑,移除了 mergeWith 中的自定义 prepareArray 回调,使列表字段能正确合并而非被替换为空数组。修改了 updateValue 函数对列表字段的处理方式,确保 onValuesChange 回调中的 allValues 参数包含完整的列表项。

Changes

队列 / 文件 变更摘要
useForm.ts 逻辑更新
src/useForm.ts
调整 updateValue 中列表字段的值合并逻辑,移除 mergeWith 的自定义 prepareArray 选项,改用标准 mergeWith 方式处理列表字段,使得 onValuesChange 回调接收完整的合并后 allValues
列表项保留测试
tests/list.test.tsx
新增测试用例"should not drop list items when updating list item field",验证更新 Form.List 嵌套字段时 onValuesChange 提供正确的 allValues,确保列表项顺序和其他项完整保留

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • 关键关注点:
    • updateValue 中 mergeWith 调用的行为变化,需确认新的合并策略正确处理列表和非列表字段
    • 新增测试的断言逻辑是否充分覆盖问题场景
    • 边界情况验证:空列表、多层嵌套列表、混合类型字段

Possibly related PRs

Poem

🐰 列表不再丢失小伙伴,
mergeWith 轻轻一挥手,
allValues 完整又闪亮,
嵌套字段也开心了呢~

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确反映了主要变更:修复在合并稀疏列表补丁到allValues时的问题,这正是代码变更的核心内容。
Linked Issues check ✅ Passed PR成功解决了链接issue #56245:修改了updateValue中的mergedAllValues计算逻辑,避免对列表字段进行数组替换,从而保留所有列表项,符合期望行为。
Out of Scope Changes check ✅ Passed 所有代码变更均在scope内:useForm.ts中修复了列表合并逻辑,list.test.tsx中添加了验证测试,均直接针对#56245问题。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3247c62 and 0136afb.

📒 Files selected for processing (2)
  • src/useForm.ts (1 hunks)
  • tests/list.test.tsx (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/list.test.tsx (2)
src/Field.tsx (1)
  • render (660-681)
docs/examples/renderProps.tsx (1)
  • render (12-57)
🔇 Additional comments (2)
tests/list.test.tsx (1)

1143-1173: 测试用例有效验证了修复逻辑!

这个测试用例清晰地重现了问题场景并验证了修复效果:

  1. 初始化包含 4 个列表项的 Form.List
  2. 更新非末尾项(索引为 1 的项)
  3. 断言 allValues 包含所有 4 个项目,且更新正确应用

测试覆盖了 PR 目标中描述的核心问题场景。

src/useForm.ts (1)

791-794: 确认列表字段合并逻辑已正确处理各类边界情况。

此修复通过在 isList() 条件下使用 mergeWith 的默认合并策略,已验证可正确处理所有提及的场景:

  • 嵌套列表:测试用例验证了 Form.List 内部字段移除操作的正确行为(list.test.tsx 第 572 行)
  • 稀疏数组:测试确认添加和移除列表项时的 undefined 处理正确(list.test.tsx 第 637 行)
  • 复杂对象深度合并:测试验证列表项内数组字段(如 tags)的更新不会丢失其他字段数据(list.test.tsx 第 1094 行)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @QDyanbing, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical data loss issue within the Ant Design Form.List component. Previously, modifying an item in a dynamic list that was not the last element could result in the unintended removal of other list items. The change refines the internal value merging mechanism to correctly handle updates within list structures, ensuring that all form data is consistently maintained.

Highlights

  • Form.List Data Loss Fix: Addressed a bug in Ant Design's Form.List where updating non-last items led to data loss.
  • Conditional Value Merging: Modified the FormStore's onValuesChange logic to conditionally merge values for list fields, ensuring proper data preservation.
  • New Test Case: Introduced a new test in list.test.tsx to validate that updating a list item does not inadvertently drop other items.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 Pull Request 旨在修复在 Form.List 中更新非末尾项时导致数据丢失的问题。问题根源在于,即便是更新列表中的单个字段,旧代码也会尝试将一个稀疏的 changedValues 对象与 allValues 合并,而这个合并逻辑在处理稀疏数组时存在缺陷,会导致列表中部分数据丢失。

本次修复的方案是,当更新的只是列表内的一个字段时,不再执行合并操作。这是因为 allValues 此时已经从更新后的 store 中获取了完整的最新数据,无需再次合并。合并逻辑仅为列表本身的直接操作(如添加、删除项)保留。

这个改动逻辑清晰,从根本上解决了问题。同时,新增的测试用例很好地覆盖了该场景,确保了修复的有效性。这是一个出色的修复。

@ShenHongFei
Copy link

试了下可以了,谢谢👍

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.

Form.List 未包裹在 Form.Item 内时, Form 的 onValuesChange 回调中第二个参数 allValues 不正确

2 participants