From b037433d2bc6fb9826b8c7286db614e19eb5fdb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Fri, 16 Jun 2023 00:47:55 +0800 Subject: [PATCH] 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 --- components/upload/Upload.tsx | 15 ++++++++++++--- components/upload/__tests__/upload.test.tsx | 7 ++++++- 2 files changed, 18 insertions(+), 4 deletions(-) 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'); + }); }); });