Skip to content

Comments

fix(uploader): beforeUpload should trigger every time before uploading#2553

Merged
oasis-cloud merged 5 commits intojdf2e:nextfrom
Miles-hxy:hxy/beforeupload
Aug 27, 2024
Merged

fix(uploader): beforeUpload should trigger every time before uploading#2553
oasis-cloud merged 5 commits intojdf2e:nextfrom
Miles-hxy:hxy/beforeupload

Conversation

@Miles-hxy
Copy link
Collaborator

@Miles-hxy Miles-hxy commented Aug 22, 2024

原来的beforeUpload在fileChange中进行触发。
Problem:在某些场景:如果用户选择的文件不符合要求,可能不会再次触发 fileChange 事件。
Solution: 在选择的文件没有符合要求的时,通过reset input保证每次的触发
补充: beforeUpload的使用方法、unit test更新

  • 日常 bug 修复

Summary by CodeRabbit

  • 新特性

    • 在文件上传组件中增加了对文件输入元素的直接引用,优化了文件上传过程的控制流。
    • 文件输入在没有选中有效文件时会被自动清空,提升了用户体验。
    • 新增异步函数以过滤文件类型,仅允许上传PNG图片,增强了上传过程的验证步骤。
  • 测试相关

    • 更新了测试用例中的查询选择器,增强了对目标元素的准确性。

@coderabbitai
Copy link

coderabbitai bot commented Aug 22, 2024

Walkthrough

此次更改涉及 uploader 组件的测试和实现。测试文件中,选择器被修改为更具体的目标,以确保测试与正确的子元素交互。在实现文件中,增加了对文件输入元素的引用,改进了文件选择过程中的用户体验,确保在无有效文件时重置输入框。此外,演示文件中新增了异步函数以筛选上传文件类型。

Changes

文件 变更概述
src/packages/uploader/__tests__/uploader.spec.tsx 修改测试中的选择器,使其更具体,以确保正确选择 DOM 元素。
src/packages/uploader/demos/h5/demo1.tsx 增加异步函数 beforeUpload,筛选上传文件类型,仅允许 PNG 图片。
src/packages/uploader/uploader.tsx 添加 fileInputRef 引用,更新 fileChange 函数以重置文件输入,改进文件上传过程的控制流。

Poem

在草地上欢快跳,
文件上传新变化,
选择器更精确,
输入框清新如花。
小兔子见了笑哈哈,
上传之路更顺畅呀! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f6f6c40 and 5d60825.

Files selected for processing (2)
  • src/packages/uploader/demos/h5/demo1.tsx (1 hunks)
  • src/packages/uploader/uploader.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/packages/uploader/demos/h5/demo1.tsx
Additional context used
GitHub Check: codecov/patch
src/packages/uploader/uploader.tsx

[warning] 353-353: src/packages/uploader/uploader.tsx#L353
Added line #L353 was not covered by tests


[warning] 362-362: src/packages/uploader/uploader.tsx#L362
Added line #L362 was not covered by tests

Additional comments not posted (2)
src/packages/uploader/uploader.tsx (2)

353-353: 提醒:为禁用检查添加测试覆盖。

当前代码在上传器被禁用时立即返回,但缺少测试覆盖。建议为此逻辑添加测试用例。

Tools
GitHub Check: codecov/patch

[warning] 353-353: src/packages/uploader/uploader.tsx#L353
Added line #L353 was not covered by tests


362-362: 提醒:为输入重置逻辑添加测试覆盖。

当没有有效文件被选中时,代码会重置输入值。建议为此逻辑添加测试用例以确保其正确性。

Tools
GitHub Check: codecov/patch

[warning] 362-362: src/packages/uploader/uploader.tsx#L362
Added line #L362 was not covered by tests


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

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@Miles-hxy Miles-hxy changed the title fix(upload): beforeUpload should trigger every time before uploadinig fix(uploader): beforeUpload should trigger every time before uploadinig Aug 22, 2024
@codecov
Copy link

codecov bot commented Aug 22, 2024

Codecov Report

Attention: Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Project coverage is 83.20%. Comparing base (c30e44a) to head (5d60825).
Report is 2 commits behind head on next.

Files Patch % Lines
src/packages/uploader/uploader.tsx 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           next    #2553       +/-   ##
=========================================
+ Coverage      0   83.20%   +83.20%     
=========================================
  Files         0      217      +217     
  Lines         0    17799    +17799     
  Branches      0     2572     +2572     
=========================================
+ Hits          0    14810    +14810     
- Misses        0     2984     +2984     
- Partials      0        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

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

Comment on lines 10 to 17
const beforeUpload = async (files: File[]) => {
console.log('bbb')
const allowedTypes = ['image/png']
const filteredFiles = Array.from(files).filter((file) =>
allowedTypes.includes(file.type)
)
return filteredFiles
}
Copy link

Choose a reason for hiding this comment

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

建议添加错误处理和改进日志记录。

beforeUpload 函数目前没有错误处理机制。如果文件类型不符合预期,可能会导致未处理的异常。此外,日志信息'bbb'不够描述性,建议提供更有意义的日志信息以便于调试。

可以考虑如下改进:

const beforeUpload = async (files: File[]) => {
  try {
    console.log('开始过滤上传文件...')
    const allowedTypes = ['image/png']
    const filteredFiles = Array.from(files).filter((file) =>
      allowedTypes.includes(file.type)
    )
    return filteredFiles
  } catch (error) {
    console.error('文件过滤过程中出错:', error)
    return []
  }
}

beforeUpload(new Array<File>().slice.call(files)).then(
(f: Array<File> | boolean) => {
const _files: File[] = filterFiles(new Array<File>().slice.call(f))
if (!_files) $el.value = ''
Copy link

Choose a reason for hiding this comment

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

修复 _files 的条件检查。

_files 是一个数组,因此条件检查应该确认数组是否为空。当前的检查可能会导致逻辑错误。

应用以下 diff 来修复代码:

          if (!_files.length) $el.value = ''
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!_files) $el.value = ''
if (!_files.length) $el.value = ''
Tools
GitHub Check: codecov/patch

[warning] 362-362: src/packages/uploader/uploader.tsx#L362
Added line #L362 was not covered by tests

@Miles-hxy Miles-hxy changed the title fix(uploader): beforeUpload should trigger every time before uploadinig fix(uploader): beforeUpload should trigger every time before uploading Aug 22, 2024
@jdf2e jdf2e deleted a comment from coderabbitai bot Aug 22, 2024
@jdf2e jdf2e deleted a comment from coderabbitai bot Aug 22, 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.

2 participants