diff --git a/components/upload/Upload.tsx b/components/upload/Upload.tsx index 5bb802a004..1424c3f5ac 100644 --- a/components/upload/Upload.tsx +++ b/components/upload/Upload.tsx @@ -107,10 +107,13 @@ const InternalUpload: React.ForwardRefRenderFunction = ( ) => { 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 = ( 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[]) => { diff --git a/components/upload/__tests__/upload.test.tsx b/components/upload/__tests__/upload.test.tsx index 94dcdfa8b7..a733c80971 100644 --- a/components/upload/__tests__/upload.test.tsx +++ b/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'); + }); }); });