fix(uploader): beforeUpload should trigger every time before uploading#2553
fix(uploader): beforeUpload should trigger every time before uploading#2553oasis-cloud merged 5 commits intojdf2e:nextfrom
Conversation
Walkthrough此次更改涉及 Changes
Poem
Recent review detailsConfiguration used: CodeRabbit UI Files selected for processing (2)
Files skipped from review as they are similar to previous changes (1)
Additional context usedGitHub Check: codecov/patch
Additional comments not posted (2)
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
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. |
| 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 | ||
| } |
There was a problem hiding this comment.
建议添加错误处理和改进日志记录。
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 []
}
}
src/packages/uploader/uploader.tsx
Outdated
| 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 = '' |
There was a problem hiding this comment.
修复 _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.
| 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
原来的beforeUpload在fileChange中进行触发。
Problem:在某些场景:如果用户选择的文件不符合要求,可能不会再次触发 fileChange 事件。
Solution: 在选择的文件没有符合要求的时,通过reset input保证每次的触发
补充: beforeUpload的使用方法、unit test更新
Summary by CodeRabbit
新特性
测试相关