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(Attachments): fix Attachments onRemove no work #555

Merged
merged 5 commits into from
Mar 10, 2025

Conversation

edison-tianhe
Copy link
Contributor

@edison-tianhe edison-tianhe commented Feb 25, 2025

close #554

Summary by CodeRabbit

  • 新增功能

    • 附件组件现支持通过自定义回调控制文件删除。若删除操作不被允许(返回 false),文件将保留在列表中,提升了文件管理的灵活性。
  • 测试

    • 添加了自动化测试,确保在禁止删除操作时,文件仍维持在附件列表中。

Copy link
Contributor

coderabbitai bot commented Feb 25, 2025

📝 Walkthrough

Walkthrough

此变更在 Attachments 组件中添加了新的 onRemove 属性,并修改了文件移除逻辑。新增逻辑中,调用 onRemove 回调以确定是否允许移除文件。当回调返回 false 时,文件不会被移除。此外,相应的测试用例也已添加,验证当 onRemove 返回 false 时文件确实不会从列表中删除。

Changes

文件路径 变更摘要
components/attachments/index.tsx 新增 onRemove 属性,并修改 onItemRemove 函数,在移除文件前调用 onRemove 回调,根据返回值决定是否取消移除。
components/attachments/tests/index.test.tsx 添加测试用例 "add and remove file but can stop remove",模拟文件添加后尝试移除,验证当 onRemove 返回 false 时文件不被移除。

Possibly related PRs

  • fix: 拖拽文件上传无法删除 #460: 该PR涉及在 Attachments 组件中添加 onRemove 回调,相关修改与文件卡片组件中移除图标的可见性和功能性有关,均涉及附件的移除过程。

Suggested reviewers

  • YumoImer
  • afc163

Poem

我是一只快乐的小兔子在代码中奔跳,
测试和逻辑齐舞,确保内容绝不跑偏。
onRemove守护文件,防止意外离场,
每行代码如同跳跃,带来希望的光芒。
代码森林中欢笑声,我为变更歌一曲! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e491cf and 3abaf05.

📒 Files selected for processing (1)
  • components/attachments/__tests__/index.test.tsx (1 hunks)
🔇 Additional comments (1)
components/attachments/__tests__/index.test.tsx (1)

56-78: 测试用例逻辑完善,验证了关键功能

这个新增的测试用例很好地验证了当 onRemove 返回 false 时文件不会被删除的行为。测试用例结构清晰,包含了准备、操作和断言三个主要步骤,确保了 onRemove 功能按预期工作。

测试验证了两个关键点:

  1. 当尝试删除文件时,DOM 中的文件元素数量保持不变
  2. onChange 回调没有被触发,这表明删除操作被正确阻止

这个测试用例与 PR 的目标(修复 Attachments 的 onRemove 功能)直接相关,并有效地验证了修复是否成功。

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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 generate docstrings to generate docstrings for this 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

github-actions bot commented Feb 25, 2025

Preview is ready

Copy link

Walkthrough

This pull request addresses an issue with the Attachments component where the onRemove function was not working as expected. The changes ensure that the onRemove function can now prevent the removal of a file if it returns false.

Changes

File Summary
components/attachments/tests/index.test.tsx Added a test case to verify that a file can be added and then prevented from being removed using the onRemove function.
components/attachments/index.tsx Modified the onItemRemove function to check the return value of onRemove and prevent file removal if it returns false.

Copy link
Contributor

@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: 1

🔭 Outside diff range comments (1)
components/attachments/index.tsx (1)

26-49: ⚠️ Potential issue

缺少 onRemove 属性的类型定义

在组件的属性解构中添加了 onRemove 属性(第 68 行),但在 AttachmentsProps 接口中没有对应的类型定义。需要在接口中添加此属性的类型定义,以确保类型安全。

建议添加以下定义:

export interface AttachmentsProps extends Omit<UploadProps, 'fileList'> {
  prefixCls?: string;

  rootClassName?: string;
  rootStyle?: React.CSSProperties;

  style?: React.CSSProperties;
  className?: string;

  classNames?: Partial<Record<SemanticType, string>>;
  styles?: Partial<Record<SemanticType, React.CSSProperties>>;

  children?: React.ReactElement;

  disabled?: boolean;

  // ============= placeholder =============
  placeholder?: PlaceholderType | ((type: 'inline' | 'drop') => PlaceholderType);
  getDropContainer?: null | (() => HTMLElement | null | undefined);

  // ============== File List ==============
  items?: Attachment[];
  overflow?: FileListProps['overflow'];
+ onRemove?: (item: Attachment) => Promise<boolean> | boolean;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7085686 and cdd6a86.

📒 Files selected for processing (2)
  • components/attachments/__tests__/index.test.tsx (1 hunks)
  • components/attachments/index.tsx (2 hunks)
🔇 Additional comments (1)
components/attachments/__tests__/index.test.tsx (1)

56-77: 测试用例实现正确

新增的测试用例正确验证了 onRemove 返回 false 时文件不会被删除的功能。测试思路清晰,断言准确。

Copy link

codecov bot commented Mar 9, 2025

Bundle Report

Bundle size has no change ✅

@afc163
Copy link
Member

afc163 commented Mar 9, 2025

用例挂了

Copy link

codecov bot commented Mar 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.81%. Comparing base (0b5ba19) to head (7af8748).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #555      +/-   ##
==========================================
+ Coverage   91.79%   91.81%   +0.01%     
==========================================
  Files          67       67              
  Lines        1463     1466       +3     
  Branches      386      388       +2     
==========================================
+ Hits         1343     1346       +3     
  Misses        120      120              

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@afc163 afc163 merged commit bb89f08 into ant-design:main Mar 10, 2025
11 checks passed
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.

Attachments onRemove 无效
2 participants