Skip to content

feat(uploader): support beforeupload props in taro #2567

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 1 commit into from
Sep 6, 2024

Conversation

Miles-hxy
Copy link
Collaborator

@Miles-hxy Miles-hxy commented Sep 4, 2024

  • 新特性提交
  • 站点、文档改进

Summary by CodeRabbit

  • 新功能

    • 引入了beforeUpload异步函数,允许在文件上传前进行自定义验证和过滤,仅处理PNG图像。
    • 更新了Uploader组件,支持beforeUpload属性,增强了文件上传的灵活性和控制能力。
  • 文档

    • 更新了文件上传功能的文档,详细说明了beforeUpload属性的使用及其返回值。

Copy link

coderabbitai bot commented Sep 4, 2024

Walkthrough

此次更改在上传组件中引入了一个新的异步函数 beforeUpload,用于在文件上传前对文件进行验证和处理。该函数允许开发者自定义上传逻辑,确保只有符合特定条件的文件被处理。相关的组件和文档也进行了相应更新,以支持这一新功能。

Changes

文件路径 更改摘要
src/packages/uploader/demos/taro/demo1.tsx 新增异步函数 beforeUpload,并将其作为 Uploader 组件的属性传递。
src/packages/uploader/doc.taro.md 新增 beforeUpload 属性,定义为接收文件数组并返回一个 Promise 的函数。
src/packages/uploader/uploader.taro.tsx UploaderProps 接口中新增可选的 beforeUpload 回调函数,修改文件上传流程以支持此功能。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Uploader
    participant beforeUpload
    participant FileHandler

    User->>Uploader: 选择文件
    Uploader->>beforeUpload: 调用 beforeUpload(files)
    beforeUpload-->>Uploader: 返回处理后的文件或布尔值
    alt 处理成功
        Uploader->>FileHandler: 处理文件
    else 处理失败
        Uploader-->>User: 显示错误信息
    end
Loading

Possibly related issues

  • [FR]: Uploader组件小程序端无beforeUpload props参数和此功能: 此更改为小程序端补充了 beforeUpload 属性,满足该问题的需求。

Poem

🐰 在上传的路上跳跃,
文件筛选不再烦恼,
只要是 PNG,便可上传,
轻松处理,真是妙!
兔子欢呼,舞动小脚,
新功能来临,快乐无比!


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 902f918 and 790720f.

Files selected for processing (3)
  • src/packages/uploader/demos/taro/demo1.tsx (1 hunks)
  • src/packages/uploader/doc.taro.md (1 hunks)
  • src/packages/uploader/uploader.taro.tsx (3 hunks)
Additional context used
Learnings (2)
src/packages/uploader/demos/taro/demo1.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.
src/packages/uploader/uploader.taro.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.
Additional comments not posted (4)
src/packages/uploader/doc.taro.md (1)

171-171: 新增 beforeUpload 属性没有问题!

beforeUpload 属性允许开发者在实际上传之前实现自定义逻辑,可用于验证或修改要上传的文件。函数签名定义正确,描述清晰地解释了该属性的目的和用法。

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

118-120: 新增 beforeUpload 属性以支持上传前的文件验证和处理。

beforeUpload 函数允许开发者在文件上传前对文件进行自定义验证和处理,提供了更灵活的上传控制。这是一个很好的改进!


477-487: onChangeMedia 函数中正确集成了 beforeUpload 功能。

当选择文件时,如果提供了 beforeUpload 函数,则会调用它对文件进行验证和处理。根据 beforeUpload 的返回值,选择性地调用 readFile 函数处理文件。这确保了只有通过验证的文件才会被上传。


494-504: onChangeImage 函数中正确集成了 beforeUpload 功能,与 onChangeMedia 的改动一致。

当选择图片文件时,如果提供了 beforeUpload 函数,则会调用它对文件进行验证和处理。根据 beforeUpload 的返回值,选择性地调用 readFile 函数处理文件。这确保了只有通过验证的图片文件才会被上传。


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 Sep 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.20%. Comparing base (902f918) to head (790720f).
Report is 3 commits behind head on next.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2567   +/-   ##
=======================================
  Coverage   83.20%   83.20%           
=======================================
  Files         217      217           
  Lines       17801    17801           
  Branches     2573     2573           
=======================================
  Hits        14812    14812           
  Misses       2984     2984           
  Partials        5        5           

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

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

Outside diff range, codebase verification and nitpick comments (2)
src/packages/uploader/demos/taro/demo1.tsx (1)

10-17: 考虑将允许的文件类型提取到一个常量中。

为了提高可读性和可维护性,建议将允许的文件类型提取到一个常量中。

可以应用以下更改:

+const ALLOWED_TYPES = ['image/png']
+
 const beforeUpload = async (files: File[]) => {
   console.log('beforeUpload')
-  const allowedTypes = ['image/png']
   const filteredFiles = Array.from(files).filter((file) =>
-    allowedTypes.includes(file.type)
+    ALLOWED_TYPES.includes(file.type)
   )
   return filteredFiles
 }
src/packages/uploader/uploader.taro.tsx (1)

Line range hint 1-1: 根据之前的学习,建议为涉及 fileList 状态变化的改动添加单元测试。

当前的改动引入了 beforeUpload 功能,可能会影响 fileList 状态。为涵盖 beforeUpload 函数的不同场景编写单元测试,有助于维护上传组件的稳定性和正确性。

return (
<Cell style={{ flexWrap: 'wrap' }}>
<Uploader
url={uploadUrl}
onStart={onStart}
beforeUpload={beforeUpload}
Copy link

Choose a reason for hiding this comment

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

考虑为 src/packages/uploader/uploader.taro.tsx 文件中的 fileList 状态变更生成单元测试。

根据之前的学习,建议为 src/packages/uploader/uploader.taro.tsx 文件中的 fileList 状态变更生成单元测试,以确保功能的正确性。

如果需要帮助生成单元测试代码或创建 GitHub issue 来跟踪此任务,请告诉我。

@Miles-hxy Miles-hxy changed the title feat(uploader): suppor beforeupload props in taro feat(uploader): support beforeupload props in taro Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR]: 希望新增 overLimit 的回调 + 希望增加beforeUpload校验函数
2 participants