Skip to content

fix: clean up preserve=false nested list field after index change - #816

Open
young-tree wants to merge 1 commit into
react-component:masterfrom
young-tree:fix/814-list-preserve-index
Open

young-tree wants to merge 1 commit into
react-component:masterfrom
young-tree:fix/814-list-preserve-index

Conversation

@young-tree

@young-tree young-tree commented Nov 17, 2026 •

Copy link
Copy Markdown

Problem

When a Field inside Form.List registers, the unregister callback captures the full namePath at registration time. List operations (move / remove) can change a surviving entity's current path while keeping its React identity. On unmount with preserve={false}, cleanup targets the stale path, so the raw store keeps the value at the new path and it reappears when the field remounts. (see #814)

Change

  • useForm.registerField: resolve the entity's current getNamePath() inside the unregister callback, and use it for store cleanup, observer notification and watcher notification instead of the captured registration path.

Test

Added two regression cases in tests/preserve.test.tsx:

  • remove the first row, then unmount preserve={false} memo fields → raw store contains no stale memo
  • move rows, then unmount → raw store contains no stale memo

Both failed before the fix (stale value retained) and pass after. Full suite: 207 passed / 1 skipped; tsc and lint pass.

Fixes #814

Summary by CodeRabbit

  • Bug 修复
    • 修正字段注销后的表单值清理与相关通知,使其使用字段当前路径。
  • 测试
    • 补充嵌套列表行移除或移动后,隐藏非保留字段时的表单值覆盖。

@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown

@young-tree is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

字段注销回调现在使用字段当前的 namePath 执行值清理和通知。新增测试覆盖嵌套列表行移除或移动后,隐藏 preserve={false} 字段时的表单值。

Changes

保留值清理

Layer / File(s) Summary
按当前路径清理字段值
src/hooks/useForm.ts, tests/preserve.test.tsx
注销回调读取当前 namePath,并用该路径执行保留值清理及相关通知。新增测试验证移除或移动嵌套列表行后,隐藏 memo 字段时只保留各行的 id,且行顺序正确。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: zombiej, qdyanbing

Merge Risk: Minimal · up to c6106

The fix keeps stale values out of the form store when a nested list field with preserve=false changes index before unmounting. The change is small and covered by new regression tests, so merge risk is minimal.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive 直接关联 issue #814 的编码要求:当 List 中的 preserve={false} 嵌套 Field 因 move/remove 改变索引后卸载时,store 中不得保留旧值。PR 描述和 AI 摘要称,注销回调改为在注销时读取 entity.getNamePath(),并用该路径执行清理、观察者通知和 watcher 通知。摘要还说明新增两个回归测试(移除首行后卸载;移动行后卸载)… 需要以下证据之一以完成判定:(1) 可读取的 src/hooks/useForm.ts 在 registerField 中 unregister 回调的 diff 或完整代码,用于确认清理路径使用 getNamePath();(2) 可读取的 tests/preserve.test.tsx 新增用例,用于确认覆盖移除首行和移动行两种场景;(3) 可执行的测试结果,显示两个新用例在修复后通过。
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题描述了主要变更:修复 preserve={false} 的嵌套列表字段在索引变化后的值清理问题。该变更与 useForm.ts 中注销回调改为读取当前 namePath 的修改一致,并与 tests/preserve.test.tsx 中新增的回归测试对应。标题简洁,且包含 fix 类型前缀。
Out of Scope Changes check Passed 根据 AI 摘要和 PR 描述,本 PR 只修改两个文件:src/hooks/useForm.ts(+11/-8)和 tests/preserve.test.tsx(+94/-0)。源码改动限定在 registerField 的注销回调内,用于替换清理、通知和 watcher 所用的路径。摘要称清理条件和默认值规则保持不变。新增测试直接覆盖 #814 的两个复现场景。未发现与 #814 无关的改动…
Full details: Out of Scope Changes check

Explanation

根据 AI 摘要和 PR 描述,本 PR 只修改两个文件:src/hooks/useForm.ts(+11/-8)和 tests/preserve.test.tsx(+94/-0)。源码改动限定在 registerField 的注销回调内,用于替换清理、通知和 watcher 所用的路径。摘要称清理条件和默认值规则保持不变。新增测试直接覆盖 #814 的两个复现场景。未发现与 #814 无关的改动。该结论基于摘要和 PR 描述,源码 diff 本身未能读取。

Full details: Linked Issues check

Explanation

直接关联 issue #814 的编码要求:当 List 中的 preserve={false} 嵌套 Field 因 move/remove 改变索引后卸载时,store 中不得保留旧值。PR 描述和 AI 摘要称,注销回调改为在注销时读取 entity.getNamePath(),并用该路径执行清理、观察者通知和 watcher 通知。摘要还说明新增两个回归测试(移除首行后卸载;移动行后卸载),断言 getFieldsValue(true) 不再包含 memo。这与 #814 的根因描述一致。但本次审查无法读取 src/hooks/useForm.ts 的 diff。该文件在 head 上的读取结果被截断,registerField 部分未包含在内。测试文件的 diff 也不可读。因此无法从源代码确认修改行为和测试内容。issue 中关于 @rc-component/form 默认 onFinish 差异的内容属于包版本行为,不属于本 PR 的编码要求。

Resolution

需要以下证据之一以完成判定:(1) 可读取的 src/hooks/useForm.ts 在 registerField 中 unregister 回调的 diff 或完整代码,用于确认清理路径使用 getNamePath();(2) 可读取的 tests/preserve.test.tsx 新增用例,用于确认覆盖移除首行和移动行两种场景;(3) 可执行的测试结果,显示两个新用例在修复后通过。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

我是小兔,轻敲代码,
字段沿着新路径回家。
行移除后,旧值不再留下,
行移动后,顺序清楚无差。
两个测试守着表单,
萝卜庆祝修复完成。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a1ae40e3-a91c-4e15-917f-518f2eda9916
Commits

Reviewing files that changed from the base of the PR and between be0cc09 and c6106e4.

Files selected for processing: 2

  • src/hooks/useForm.ts
  • tests/preserve.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
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.

List nested fields with preserve={false} retain values after move/remove changes their index

1 participant