Skip to content

Commit bb89f08

Browse files
edison-tianhejin.gaoafc163
authoredMar 10, 2025··
fix(Attachments): fix Attachments onRemove no work (#555)
* fix(Attachments): fix Attachments onRemove no work * fix(Attachments): fix Attachments onRemove no work * Update index.test.tsx 修复单测 --------- Co-authored-by: jin.gao <jin.gao@mobilemd.cn> Co-authored-by: afc163 <afc163@gmail.com>
1 parent 0b5ba19 commit bb89f08

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed
 

‎components/attachments/__tests__/index.test.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ describe('attachments', () => {
5353
expect(onChange.mock.calls[0][0].fileList).toHaveLength(0);
5454
});
5555

56+
it('add and remove file but can stop remove', async () => {
57+
const onChange = jest.fn();
58+
const { container } = render(
59+
renderAttachments({
60+
onChange,
61+
onRemove: () => false,
62+
}),
63+
);
64+
65+
// Add file
66+
fireEvent.change(container.querySelector('input')!, {
67+
target: { files: [{ file: 'foo.png' }] },
68+
});
69+
await waitFakeTimer();
70+
expect(onChange.mock.calls[0][0].fileList).toHaveLength(1);
71+
onChange.mockClear();
72+
73+
// Remove file
74+
fireEvent.click(container.querySelector('.ant-attachment-list-card-remove')!);
75+
await waitFakeTimer();
76+
expect(container.querySelectorAll('.ant-attachment-list-card')).toHaveLength(1);
77+
expect(onChange).not.toHaveBeenCalled();
78+
});
79+
5680
it('overflow: scrollX', () => {
5781
const { container } = render(
5882
renderAttachments({

‎components/attachments/index.tsx

+14-7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function Attachments(props: AttachmentsProps, ref: React.Ref<AttachmentsRef>) {
6565
getDropContainer,
6666
placeholder,
6767
onChange,
68+
onRemove,
6869
overflow,
6970
disabled,
7071
classNames = {},
@@ -124,14 +125,20 @@ function Attachments(props: AttachmentsProps, ref: React.Ref<AttachmentsRef>) {
124125
onChange: triggerChange,
125126
};
126127

127-
const onItemRemove = (item: Attachment) => {
128-
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid);
129-
triggerChange({
130-
file: item,
131-
fileList: newFileList,
132-
});
133-
};
128+
const onItemRemove = (item: Attachment) =>
129+
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove).then((ret) => {
130+
// Prevent removing file
131+
if (ret === false) {
132+
return;
133+
}
134134

135+
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid);
136+
137+
triggerChange({
138+
file: { ...item, status: 'removed' },
139+
fileList: newFileList,
140+
});
141+
});
135142
// ============================ Render ============================
136143
let renderChildren: React.ReactElement;
137144

0 commit comments

Comments
 (0)
Please sign in to comment.