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
This commit is contained in:
二货爱吃白萝卜 2023-06-16 00:47:55 +08:00 committed by GitHub
parent 3c979eb84b
commit b037433d2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

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

View File

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