Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions components/attachments/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ describe('attachments', () => {
expect(onChange.mock.calls[0][0].fileList).toHaveLength(0);
});

it('add and remove file but can stop remove', async () => {
const onChange = jest.fn();
const { container } = render(
renderAttachments({
onChange,
onRemove: () => false,
}),
);

// Add file
fireEvent.change(container.querySelector('input')!, {
target: { files: [{ file: 'foo.png' }] },
});
await waitFakeTimer();
expect(onChange.mock.calls[0][0].fileList).toHaveLength(1);
onChange.mockClear();

// Remove file
fireEvent.click(container.querySelector('.ant-attachment-list-card-remove')!);
await waitFakeTimer();
expect(onChange.mock.calls[0][0].fileList).toHaveLength(1);
});

it('overflow: scrollX', () => {
const { container } = render(
renderAttachments({
Expand Down
21 changes: 14 additions & 7 deletions components/attachments/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function Attachments(props: AttachmentsProps, ref: React.Ref<AttachmentsRef>) {
getDropContainer,
placeholder,
onChange,
onRemove,
overflow,
disabled,
classNames = {},
Expand Down Expand Up @@ -124,14 +125,20 @@ function Attachments(props: AttachmentsProps, ref: React.Ref<AttachmentsRef>) {
onChange: triggerChange,
};

const onItemRemove = (item: Attachment) => {
const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid);
triggerChange({
file: item,
fileList: newFileList,
});
};
const onItemRemove = (item: Attachment) =>
Promise.resolve(typeof onRemove === 'function' ? onRemove(item) : onRemove).then((ret) => {

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.

// Prevent removing file
if (ret === false) {
return;
}

const newFileList = fileList.filter((fileItem) => fileItem.uid !== item.uid);

triggerChange({
file: { ...item, status: 'removed' },
fileList: newFileList,
});
});
// ============================ Render ============================
let renderChildren: React.ReactElement;

Expand Down