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

fix: List nest Field should correct removed #737

Merged
merged 4 commits into from
Nov 27, 2024
Merged

fix: List nest Field should correct removed #737

merged 4 commits into from
Nov 27, 2024

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Nov 27, 2024

ref ant-design/ant-design#51702

Summary by CodeRabbit

  • 新功能
    • 改进了 Field 组件对列表字段的处理,确保上下文正确合并并发出适当的警告。
  • 错误修复
    • 增强了测试套件,确保在各种操作(如添加、删除和修改字段)中,表单状态保持一致。
    • 添加了新测试,验证在 preserve 属性设置为 false 时嵌套字段的行为。

Copy link

vercel bot commented Nov 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
field-form ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 27, 2024 8:45am

Copy link

coderabbitai bot commented Nov 27, 2024

Walkthrough

该拉取请求对 src/Field.tsx 中的 Field 组件进行了修改,主要集中在 WrapperField 函数上。引入了一个新的变量 isMergedListField,用于判断字段是否属于列表上下文,并更新了生成 Field 组件的 key 的逻辑。警告条件也进行了调整,以确保在合并上下文中正确触发关于 preserve 属性不适用于列表字段的警告。此外,测试文件 tests/list.test.tsx 进行了扩展,以验证嵌套字段的行为,并增强了对列表操作的测试。

Changes

文件路径 更改摘要
src/Field.tsx 引入 isMergedListField 变量,更新 key 生成逻辑,调整警告条件,确保 Field 组件正确反映上下文。
tests/list.test.tsx 更新导入语句,添加新测试以验证嵌套字段的行为,增强现有测试,确保列表操作的正确性和错误处理。

Possibly related PRs

  • fix: change #731: 此PR修改了 Field 组件中的 getControlled 方法,与主PR中的 WrapperField 函数的更改直接相关。
  • fix: change not trigger on Date #734: 此PR也改变了 Field 组件中的 getControlled 方法,特别是更改了值比较逻辑,可能影响主PR中描述的 Field 组件的行为。

Poem

在草地上跳跃的小兔,
变更如春风,轻轻拂过。
列表字段更聪明,
警告声声,真不多。
测试也更稳固,
让我们一起欢呼! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.44%. Comparing base (60f43be) to head (2bebdb4).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #737   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files          18       18           
  Lines        1256     1257    +1     
  Branches      295      315   +20     
=======================================
+ Hits         1249     1250    +1     
  Misses          7        7           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zombieJ zombieJ marked this pull request as ready for review November 27, 2024 08:50
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (5)
src/Field.tsx (1)

Line range hint 699-707: 完善列表字段 preserve 属性的警告逻辑

使用 isMergedListField 替代原有判断,使警告更准确地针对列表字段触发。建议添加测试用例验证这个警告逻辑。

需要我帮您编写相关的测试用例吗?可以验证以下场景:

  1. 直接的列表字段设置 preserve=false 时触发警告
  2. 嵌套列表字段的 preserve 行为
tests/list.test.tsx (4)

901-914: 建议优化测试组件的结构

当前的测试组件结构可以通过以下方式改进:

  1. 提取重复的 InfoField 渲染逻辑到单独的组件
  2. 使用更具描述性的变量名替代硬编码的索引值
 <Form ref={formRef} preserve={false}>
   <Form.List name="list">
     {(fields, { remove }) => {
+      const handleRemove = (index: number) => remove(index);
       return (
         <>
           {fields.map(field => (
             <InfoField key={field.key} name={[field.name, 'user']} />
           ))}
-          <button onClick={() => remove(1)}>remove</button>
+          <button onClick={() => handleRemove(1)}>remove</button>
         </>
       );
     }}
   </Form.List>
 </Form>

917-923: 建议使用更简洁的测试数据结构

当前的测试数据结构可以简化,因为第三个对象使用了不必要的多行格式。

 formRef.current!.setFieldValue('list', [
   { user: '1' },
   { user: '2' },
-  {
-    user: '3',
-  },
+  { user: '3' },
 ]);

925-927: 建议封装通用的异步等待逻辑

多处使用了相同的超时等待逻辑,建议将其封装为测试辅助函数。

+const waitForUpdate = () => act(async () => {
+  await timeout();
+});
+
-await act(async () => {
-  await timeout();
-});
+await waitForUpdate();

Also applies to: 934-936


938-938: 建议增强断言的完整性

当前只验证了字段值,建议添加额外的断言来确保 UI 状态也正确更新。

 expect(formRef.current!.getFieldValue('list')).toEqual([{ user: '1' }, { user: '3' }]);
+expect(container.querySelectorAll('input')).toHaveLength(2);
+expect(container.querySelectorAll('input')[0].value).toBe('1');
+expect(container.querySelectorAll('input')[1].value).toBe('3');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 60f43be and 2bebdb4.

📒 Files selected for processing (2)
  • src/Field.tsx (3 hunks)
  • tests/list.test.tsx (2 hunks)
🔇 Additional comments (5)
src/Field.tsx (4)

692-693: 优化列表字段上下文判断逻辑

通过引入 isMergedListField 变量,合并了来自 props 和 context 的列表字段状态,使得字段类型判断更加准确和统一。


695-697: 改进字段 key 的生成逻辑

基于 isMergedListField 优化了 key 的生成策略,确保列表字段和非列表字段有不同的 key 处理方式。


713-715: 更新 Field 组件属性传递

使用 isMergedListField 统一处理列表字段标识,使组件属性传递更加清晰。


Line range hint 692-715: 建议添加单元测试覆盖新增场景

为确保 isMergedListField 在各种场景下的正确性,建议补充以下测试场景:

  1. 列表上下文中的嵌套字段行为
  2. preserve 属性在不同层级的影响
  3. key 生成策略的正确性验证
tests/list.test.tsx (1)

896-897: 测试用例描述清晰且与 issue 关联

测试用例名称准确描述了要验证的功能,并通过注释链接到相关的 issue,这是一个很好的实践。

@zombieJ zombieJ merged commit 4c37153 into master Nov 27, 2024
12 checks passed
@yoyo837 yoyo837 deleted the fix-list branch November 27, 2024 08:58
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.

1 participant