Browse Source

chore: pick upload patch (#43200)

pull/43207/head
二货爱吃白萝卜 1 year ago
committed by GitHub
parent
commit
02f876f03a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      components/upload/Upload.tsx

30
components/upload/Upload.tsx

@ -4,11 +4,12 @@ import RcUpload from 'rc-upload';
import useMergedState from 'rc-util/lib/hooks/useMergedState'; import useMergedState from 'rc-util/lib/hooks/useMergedState';
import * as React from 'react'; import * as React from 'react';
import { flushSync } from 'react-dom'; import { flushSync } from 'react-dom';
import warning from '../_util/warning';
import { ConfigContext } from '../config-provider'; import { ConfigContext } from '../config-provider';
import DisabledContext from '../config-provider/DisabledContext'; import DisabledContext from '../config-provider/DisabledContext';
import LocaleReceiver from '../locale-provider/LocaleReceiver'; import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale/default'; import defaultLocale from '../locale/default';
import warning from '../_util/warning'; import UploadList from './UploadList';
import type { import type {
RcFile, RcFile,
ShowUploadListInterface, ShowUploadListInterface,
@ -16,7 +17,6 @@ import type {
UploadFile, UploadFile,
UploadProps, UploadProps,
} from './interface'; } from './interface';
import UploadList from './UploadList';
import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils'; import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils';
export const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`; export const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`;
@ -60,7 +60,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
const [mergedFileList, setMergedFileList] = useMergedState(defaultFileList || [], { const [mergedFileList, setMergedFileList] = useMergedState(defaultFileList || [], {
value: fileList, value: fileList,
postState: list => list ?? [], postState: (list) => list ?? [],
}); });
const [dragState, setDragState] = React.useState<string>('drop'); const [dragState, setDragState] = React.useState<string>('drop');
@ -119,7 +119,9 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
changeInfo.event = event; changeInfo.event = event;
} }
onChange?.(changeInfo); flushSync(() => {
onChange?.(changeInfo);
});
}; };
const mergedBeforeUpload = async (file: RcFile, fileListArgs: RcFile[]) => { const mergedBeforeUpload = async (file: RcFile, fileListArgs: RcFile[]) => {
@ -155,21 +157,23 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
return parsedFile as RcFile; return parsedFile as RcFile;
}; };
const onBatchStart: RcUploadProps['onBatchStart'] = batchFileInfoList => { const onBatchStart: RcUploadProps['onBatchStart'] = (batchFileInfoList) => {
// Skip file which marked as `LIST_IGNORE`, these file will not add to file list // Skip file which marked as `LIST_IGNORE`, these file will not add to file list
const filteredFileInfoList = batchFileInfoList.filter(info => !(info.file as any)[LIST_IGNORE]); const filteredFileInfoList = batchFileInfoList.filter(
(info) => !(info.file as any)[LIST_IGNORE],
);
// Nothing to do since no file need upload // Nothing to do since no file need upload
if (!filteredFileInfoList.length) { if (!filteredFileInfoList.length) {
return; return;
} }
const objectFileList = filteredFileInfoList.map(info => file2Obj(info.file as RcFile)); const objectFileList = filteredFileInfoList.map((info) => file2Obj(info.file as RcFile));
// Concat new files with prev files // Concat new files with prev files
let newFileList = [...mergedFileList]; let newFileList = [...mergedFileList];
objectFileList.forEach(fileObj => { objectFileList.forEach((fileObj) => {
// Replace file if exist // Replace file if exist
newFileList = updateFileList(fileObj, newFileList); newFileList = updateFileList(fileObj, newFileList);
}); });
@ -265,7 +269,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
const handleRemove = (file: UploadFile) => { const handleRemove = (file: UploadFile) => {
let currentFile: UploadFile; let currentFile: UploadFile;
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => { Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then((ret) => {
// Prevent removing file // Prevent removing file
if (ret === false) { if (ret === false) {
return; return;
@ -275,7 +279,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
if (removedFileList) { if (removedFileList) {
currentFile = { ...file, status: 'removed' }; currentFile = { ...file, status: 'removed' };
mergedFileList?.forEach(item => { mergedFileList?.forEach((item) => {
const matchKey = currentFile.uid !== undefined ? 'uid' : 'name'; const matchKey = currentFile.uid !== undefined ? 'uid' : 'name';
if (item[matchKey] === currentFile[matchKey] && !Object.isFrozen(item)) { if (item[matchKey] === currentFile[matchKey] && !Object.isFrozen(item)) {
item.status = 'removed'; item.status = 'removed';
@ -340,8 +344,8 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
const renderUploadList = (button?: React.ReactNode, buttonVisible?: boolean) => const renderUploadList = (button?: React.ReactNode, buttonVisible?: boolean) =>
showUploadList ? ( showUploadList ? (
<LocaleReceiver componentName="Upload" defaultLocale={defaultLocale.Upload}> <LocaleReceiver componentName='Upload' defaultLocale={defaultLocale.Upload}>
{contextLocale => { {(contextLocale) => {
const { const {
showRemoveIcon, showRemoveIcon,
showPreviewIcon, showPreviewIcon,
@ -387,7 +391,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
prefixCls, prefixCls,
{ {
[`${prefixCls}-drag`]: true, [`${prefixCls}-drag`]: true,
[`${prefixCls}-drag-uploading`]: mergedFileList.some(file => file.status === 'uploading'), [`${prefixCls}-drag-uploading`]: mergedFileList.some((file) => file.status === 'uploading'),
[`${prefixCls}-drag-hover`]: dragState === 'dragover', [`${prefixCls}-drag-hover`]: dragState === 'dragover',
[`${prefixCls}-disabled`]: mergedDisabled, [`${prefixCls}-disabled`]: mergedDisabled,
[`${prefixCls}-rtl`]: direction === 'rtl', [`${prefixCls}-rtl`]: direction === 'rtl',

Loading…
Cancel
Save