-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
base: main
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthrough此变更在 Changes
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
WalkthroughThis pull request addresses an issue with the Changes
|
file: item, | ||
fileList: newFileList, | ||
let currentFile: Attachment; | ||
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove).then((ret) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that the onRemove
function is properly handled as a promise to prevent any asynchronous issues. The current implementation correctly resolves the onRemove
function and checks its return value to decide whether to proceed with file removal.
There was a problem hiding this 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
📒 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
时文件不会被删除的功能。测试思路清晰,断言准确。
components/attachments/index.tsx
Outdated
const onItemRemove = (item: Attachment) => { | ||
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid); | ||
triggerChange({ | ||
file: item, | ||
fileList: newFileList, | ||
let currentFile: Attachment; | ||
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove).then((ret) => { | ||
// Prevent removing file | ||
if (ret === false) { | ||
return; | ||
} | ||
|
||
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid); | ||
|
||
if (newFileList) { | ||
currentFile = { ...item, status: 'removed' }; | ||
|
||
triggerChange({ | ||
file: currentFile, | ||
fileList: newFileList, | ||
}); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
优化文件移除处理函数的实现
当前实现存在几个问题需要改进:
currentFile
变量声明后没有初始化,在 Promise 链中才被赋值,可能导致问题if (newFileList)
判断条件是多余的,因为filter
操作始终会返回一个数组(可能为空)- Promise 链中没有错误处理,如果
onRemove
回调抛出错误,将会默默失败
建议修改为:
const onItemRemove = (item: Attachment) => {
- let currentFile: Attachment;
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove)
+ .then((ret) => {
// Prevent removing file
if (ret === false) {
return;
}
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid);
-
- if (newFileList) {
- currentFile = { ...item, status: 'removed' };
-
- triggerChange({
- file: currentFile,
- fileList: newFileList,
- });
- }
+ const currentFile = { ...item, status: 'removed' };
+
+ triggerChange({
+ file: currentFile,
+ fileList: newFileList,
+ });
})
+ .catch((error) => {
+ console.error('Error occurred during file removal:', error);
+ });
};
📝 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.
const onItemRemove = (item: Attachment) => { | |
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid); | |
triggerChange({ | |
file: item, | |
fileList: newFileList, | |
let currentFile: Attachment; | |
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove).then((ret) => { | |
// Prevent removing file | |
if (ret === false) { | |
return; | |
} | |
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid); | |
if (newFileList) { | |
currentFile = { ...item, status: 'removed' }; | |
triggerChange({ | |
file: currentFile, | |
fileList: newFileList, | |
}); | |
} | |
}); | |
const onItemRemove = (item: Attachment) => { | |
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove) | |
.then((ret) => { | |
// Prevent removing file | |
if (ret === false) { | |
return; | |
} | |
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid); | |
const currentFile = { ...item, status: 'removed' }; | |
triggerChange({ | |
file: currentFile, | |
fileList: newFileList, | |
}); | |
}) | |
.catch((error) => { | |
console.error('Error occurred during file removal:', error); | |
}); | |
}; |
[issue](#554)
Summary by CodeRabbit
新增功能
测试