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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
18 additions and
4 deletions
-
components/upload/Upload.tsx
-
components/upload/__tests__/upload.test.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[]) => { |
|
|
|
|
|
@ -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'); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|