Browse Source

fix: upload event should not trigger the file out of maxCount (#43034)

* fix: upload event should not trigger the file out of maxCount

* chore: opt match logic

* chore: refactor use some
pull/43054/head
二货爱吃白萝卜 1 year ago
committed by GitHub
parent
commit
b037433d2b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      components/upload/Upload.tsx
  2. 7
      components/upload/__tests__/upload.test.tsx

15
components/upload/Upload.tsx

@ -107,10 +107,13 @@ const InternalUpload: React.ForwardRefRenderFunction<UploadRef, UploadProps> = (
) => {
let cloneList = [...changedFileList];
let exceedMaxCount = false;
// Cut to match count
if (maxCount === 1) {
cloneList = cloneList.slice(-1);
} else if (maxCount) {
exceedMaxCount = true;
cloneList = cloneList.slice(0, maxCount);
}
@ -129,9 +132,15 @@ const InternalUpload: React.ForwardRefRenderFunction<UploadRef, UploadProps> = (
changeInfo.event = event;
}
flushSync(() => {
onChange?.(changeInfo);
});
if (
!exceedMaxCount ||
// We should ignore event if current file is exceed `maxCount`
cloneList.some((f) => f.uid === file.uid)
) {
flushSync(() => {
onChange?.(changeInfo);
});
}
};
const mergedBeforeUpload = async (file: RcFile, fileListArgs: RcFile[]) => {

7
components/upload/__tests__/upload.test.tsx

@ -480,7 +480,7 @@ describe('Upload', () => {
// Delay return true for remove
await waitFakeTimer();
await act(async () => {
await removePromise(true);
removePromise(true);
});
expect(onChange).toHaveBeenCalled();
@ -771,6 +771,11 @@ describe('Upload', () => {
name: 'foo.png',
}),
]);
// Only trigger for file in `maxCount`
onChange.mock.calls.forEach((args) => {
expect(args[0].file.name).toBe('foo.png');
});
});
});

Loading…
Cancel
Save