Skip to content

fix(uploader): beforeUpload failed to work #3219

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

Merged
merged 2 commits into from
May 17, 2025

Conversation

Alex-huxiyang
Copy link
Collaborator

@Alex-huxiyang Alex-huxiyang commented May 7, 2025

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

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

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

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • Bug Fixes
    • 修复了上传组件在处理 beforeUpload 钩子返回空数组时的行为,避免了后续不必要的处理。
    • 优化了文件过滤逻辑,确保 beforeUpload 钩子修改后的文件列表能被正确处理。
    • 改进了异步处理方式,提升了上传流程的稳定性和一致性。

Copy link

coderabbitai bot commented May 7, 2025

"""

Walkthrough

本次变更主要重构了上传组件中处理 beforeUpload 钩子的异步流程,分别在 Taro 端和 Web 端优化了文件筛选与上传前处理的逻辑。同时,H5 示例中移除了初始文件的 status: 'uploading' 属性,简化了初始状态。

Changes

文件/分组 变更摘要
src/packages/uploader/demos/h5/demo1.tsx 移除了初始文件对象中的 status: 'uploading' 属性。
src/packages/uploader/uploader.taro.tsx onChangeImagebeforeUpload 处理由 async/await 改为 .then() Promise 链式调用,优化了异步流程和文件筛选。
src/packages/uploader/uploader.tsx 优化了 fileChange 函数,遇到 beforeUpload 返回空数组时立即返回,防止后续处理,并调整了文件筛选逻辑。
src/packages/uploader/tests/uploader.spec.tsx 新增测试用例,验证 beforeUpload 钩子对文件过滤和上传流程的影响,包括过滤文件和空数组返回时不上传的场景。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Uploader
    participant beforeUpload
    participant FileReader

    User->>Uploader: 选择文件
    Uploader->>beforeUpload: (可选) 传递筛选后的文件
    alt beforeUpload 存在
        beforeUpload-->>Uploader: 返回处理后的文件数组
        Uploader->>Uploader: 判断文件数组是否为空
        alt 文件数组为空
            Uploader->>User: 清空 input,无后续处理
        else 文件数组非空
            Uploader->>FileReader: 读取文件
        end
    else beforeUpload 不存在
        Uploader->>FileReader: 直接读取文件
    end
Loading

Possibly related PRs

  • jdf2e/nutui-react#2553:同样修改了 fileChange 函数和 beforeUpload 钩子的处理流程,确保未选中文件时正确重置 input,代码层面高度相关。
  • jdf2e/nutui-react#2567:该 PR 为 Taro 上传组件新增了 beforeUpload 支持,本次重构正是在其基础上进一步优化异步处理和文件筛选逻辑。

Suggested reviewers

  • xiaoyatong
  • oasis-cloud

Poem

兔子敲键盘,上传逻辑变新颜,
Promise 链上舞翩跹,
beforeUpload 细筛选,
空文件即刻返,
代码如风更轻便,
上传体验更自然!
🐇✨
"""

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 c8f55cb and e543631.

📒 Files selected for processing (1)
  • src/packages/uploader/__tests__/uploader.spec.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
src/packages/uploader/__tests__/uploader.spec.tsx (1)
Learnt from: Alex-huxiyang
PR: jdf2e/nutui-react#2416
File: src/packages/uploader/uploader.taro.tsx:230-230
Timestamp: 2024-07-05T02:58:57.992Z
Learning: When changes are made to the `fileList` state in the `src/packages/uploader/uploader.taro.tsx` file, ensure to generate unit tests to cover these changes.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build
  • GitHub Check: test
🔇 Additional comments (2)
src/packages/uploader/__tests__/uploader.spec.tsx (2)

456-493: 测试覆盖了beforeUpload函数的文件过滤功能

这个测试用例很好地验证了beforeUpload钩子函数的核心功能:根据文件类型进行筛选。测试涵盖了三种情况:单个PNG文件上传成功、单个JPG文件被过滤掉、多文件上传时只有PNG文件通过。

代码实现清晰、断言完整,能够确保组件在修复后正确处理文件过滤逻辑。


495-515: 验证了beforeUpload返回空数组时的正确行为

这个测试用例检验了一个重要的边界情况:当beforeUpload函数返回空数组时,应该不会触发文件上传。测试正确模拟了这种场景,并验证了预期行为。

这项测试对于确保上传组件在各种情况下的鲁棒性非常重要,特别是考虑到PR的主要目标是修复beforeUpload功能。

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@github-actions github-actions bot added 3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) labels May 7, 2025
@Alex-huxiyang Alex-huxiyang linked an issue May 7, 2025 that may be closed by this pull request
Copy link

codecov bot commented May 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.72%. Comparing base (56f239d) to head (e543631).
Report is 7 commits behind head on feat_v3.x.

Additional details and impacted files
@@              Coverage Diff              @@
##           feat_v3.x    #3219      +/-   ##
=============================================
+ Coverage      86.70%   86.72%   +0.01%     
=============================================
  Files            291      291              
  Lines          19062    19067       +5     
  Branches        2891     2894       +3     
=============================================
+ Hits           16528    16535       +7     
+ Misses          2529     2527       -2     
  Partials           5        5              

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 56f239d and c8f55cb.

📒 Files selected for processing (3)
  • src/packages/uploader/demos/h5/demo1.tsx (0 hunks)
  • src/packages/uploader/uploader.taro.tsx (1 hunks)
  • src/packages/uploader/uploader.tsx (1 hunks)
💤 Files with no reviewable changes (1)
  • src/packages/uploader/demos/h5/demo1.tsx
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/packages/uploader/uploader.tsx

[warning] 166-169: src/packages/uploader/uploader.tsx#L166-L169
Added lines #L166 - L169 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (2)
src/packages/uploader/uploader.tsx (1)

173-173: 修复了文件过滤顺序问题

这个修改非常关键 - 您现在对 _files 进行过滤(即 beforeUpload 处理后的结果),而不是原始文件数组。这确保了 beforeUpload 钩子函数对文件的任何修改都会被正确地应用到后续的过滤逻辑中。

这个修改很好地解决了 beforeUpload 功能无法正常工作的问题。

src/packages/uploader/uploader.taro.tsx (1)

319-328: 优化了 Taro 端的 beforeUpload 处理逻辑

您改进了 onChangeImage 函数中的 beforeUpload 处理流程,使用 Promise 链式调用替代了之前的方式。这种实现方式更加清晰,并且与 Web 端的实现保持一致。

关键改进包括:

  1. 直接将筛选后的 _files 数组传递给 beforeUpload
  2. 正确处理 beforeUpload 返回空数组的情况
  3. 简化了条件分支逻辑

这个改动很好地解决了 beforeUpload 钩子在 Taro 环境中不能正常工作的问题。

@pull-request-size pull-request-size bot added size/M and removed size/S labels May 8, 2025
@oasis-cloud oasis-cloud merged commit 8588c4e into jdf2e:feat_v3.x May 17, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uploader 自定义上传前的处理无效
2 participants