mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
79f8fece4b
* chore: Update Upload version * useMergedStatus * fix file list parser * fix list replacement * bump rc-upload * add Upload.LIST_IGNORE * support LIST_IGNORE * fix uploadList batch test * test: fix batch case test case changed * test: Finish test case * fix: Not change uid if exist * use proxy * fix proxy * fix ts * part test case check * more test back * back of test all * update test case * more unmount * adjust test cae * add unmount
415 lines
11 KiB
TypeScript
415 lines
11 KiB
TypeScript
import * as React from 'react';
|
|
import RcUpload, { UploadProps as RcUploadProps } from 'rc-upload';
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
|
import classNames from 'classnames';
|
|
import Dragger from './Dragger';
|
|
import UploadList from './UploadList';
|
|
import {
|
|
RcFile,
|
|
ShowUploadListInterface,
|
|
UploadProps,
|
|
UploadFile,
|
|
UploadLocale,
|
|
UploadChangeParam,
|
|
UploadType,
|
|
UploadListType,
|
|
} from './interface';
|
|
import { T, wrapFile, getFileItem, removeFileItem } from './utils';
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
|
import defaultLocale from '../locale/default';
|
|
import { ConfigContext } from '../config-provider';
|
|
import devWarning from '../_util/devWarning';
|
|
|
|
const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`;
|
|
|
|
export { UploadProps };
|
|
|
|
const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (props, ref) => {
|
|
const {
|
|
fileList,
|
|
defaultFileList,
|
|
onRemove,
|
|
showUploadList,
|
|
listType,
|
|
onPreview,
|
|
onDownload,
|
|
onChange,
|
|
previewFile,
|
|
disabled,
|
|
locale: propLocale,
|
|
iconRender,
|
|
isImageUrl,
|
|
progress,
|
|
prefixCls: customizePrefixCls,
|
|
className,
|
|
type,
|
|
children,
|
|
style,
|
|
itemRender,
|
|
maxCount,
|
|
} = props;
|
|
|
|
const [mergedFileList, setMergedFileList] = useMergedState(defaultFileList || [], {
|
|
value: fileList,
|
|
});
|
|
|
|
const [dragState, setDragState] = React.useState<string>('drop');
|
|
|
|
const upload = React.useRef<any>();
|
|
|
|
React.useEffect(() => {
|
|
devWarning(
|
|
'fileList' in props || !('value' in props),
|
|
'Upload',
|
|
'`value` is not a valid prop, do you mean `fileList`?',
|
|
);
|
|
|
|
devWarning(
|
|
!('transformFile' in props),
|
|
'Upload',
|
|
'`transformFile` is deprecated. Please use `beforeUpload` directly.',
|
|
);
|
|
}, []);
|
|
|
|
// Control mode will auto fill file uid if not provided
|
|
React.useEffect(() => {
|
|
const timestamp = Date.now();
|
|
|
|
(fileList || []).forEach((file, index) => {
|
|
if (!file.uid) {
|
|
file.uid = `__AUTO__${timestamp}_${index}__`;
|
|
}
|
|
});
|
|
}, [fileList]);
|
|
|
|
const onInternalChange = (
|
|
file: UploadFile,
|
|
changedFileList: UploadFile[],
|
|
event?: { percent: number },
|
|
) => {
|
|
let cloneList = [...changedFileList];
|
|
|
|
// Cut to match count
|
|
if (maxCount === 1) {
|
|
cloneList = cloneList.slice(-1);
|
|
} else if (maxCount) {
|
|
cloneList = cloneList.slice(0, maxCount);
|
|
}
|
|
|
|
setMergedFileList(cloneList);
|
|
|
|
const changeInfo: UploadChangeParam = {
|
|
file,
|
|
fileList: cloneList,
|
|
};
|
|
|
|
if (event) {
|
|
changeInfo.event = event;
|
|
}
|
|
|
|
onChange?.(changeInfo);
|
|
};
|
|
|
|
const onBatchStart: RcUploadProps['onBatchStart'] = batchFileInfoList => {
|
|
// 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]);
|
|
|
|
// Nothing to do since no file need upload
|
|
if (!filteredFileInfoList.length) {
|
|
return;
|
|
}
|
|
|
|
const objectFileList = filteredFileInfoList.map(info => wrapFile(info.file));
|
|
|
|
// Concat new files with prev files
|
|
const newFileList = [...mergedFileList];
|
|
objectFileList.forEach(fileObj => {
|
|
if (newFileList.every(existFile => existFile.uid !== fileObj.uid)) {
|
|
newFileList.push(fileObj);
|
|
}
|
|
});
|
|
|
|
onInternalChange(filteredFileInfoList[0]?.file, newFileList);
|
|
};
|
|
|
|
const onStart = (file: RcFile) => {
|
|
const targetItem = wrapFile(file);
|
|
targetItem.status = 'uploading';
|
|
|
|
const nextFileList = [...mergedFileList];
|
|
|
|
const fileIndex = nextFileList.findIndex(({ uid }: UploadFile) => uid === targetItem.uid);
|
|
if (fileIndex === -1) {
|
|
nextFileList.push(targetItem);
|
|
} else {
|
|
nextFileList[fileIndex] = targetItem;
|
|
}
|
|
|
|
onInternalChange(targetItem, nextFileList);
|
|
};
|
|
|
|
const onSuccess = (response: any, file: UploadFile, xhr: any) => {
|
|
try {
|
|
if (typeof response === 'string') {
|
|
response = JSON.parse(response);
|
|
}
|
|
} catch (e) {
|
|
/* do nothing */
|
|
}
|
|
const targetItem = getFileItem(file, mergedFileList);
|
|
// removed
|
|
if (!targetItem) {
|
|
return;
|
|
}
|
|
targetItem.status = 'done';
|
|
targetItem.response = response;
|
|
targetItem.xhr = xhr;
|
|
onInternalChange(wrapFile(targetItem), [...mergedFileList]);
|
|
};
|
|
|
|
const onProgress = (e: { percent: number }, file: UploadFile) => {
|
|
const targetItem = getFileItem(file, mergedFileList);
|
|
// removed
|
|
if (!targetItem) {
|
|
return;
|
|
}
|
|
targetItem.percent = e.percent;
|
|
onInternalChange(wrapFile(targetItem), [...mergedFileList], e);
|
|
};
|
|
|
|
const onError = (error: Error, response: any, file: UploadFile) => {
|
|
const targetItem = getFileItem(file, mergedFileList);
|
|
// removed
|
|
if (!targetItem) {
|
|
return;
|
|
}
|
|
targetItem.error = error;
|
|
targetItem.response = response;
|
|
targetItem.status = 'error';
|
|
onInternalChange(wrapFile(targetItem), [...mergedFileList]);
|
|
};
|
|
|
|
const handleRemove = (file: UploadFile) => {
|
|
let currentFile: UploadFile;
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => {
|
|
// Prevent removing file
|
|
if (ret === false) {
|
|
return;
|
|
}
|
|
|
|
const removedFileList = removeFileItem(file, mergedFileList);
|
|
|
|
if (removedFileList) {
|
|
currentFile = wrapFile(file);
|
|
currentFile.status = 'removed';
|
|
mergedFileList?.forEach(item => {
|
|
const matchKey = currentFile.uid !== undefined ? 'uid' : 'name';
|
|
if (item[matchKey] === currentFile[matchKey]) {
|
|
item.status = 'removed';
|
|
}
|
|
});
|
|
upload.current?.abort(currentFile);
|
|
|
|
onInternalChange(currentFile, removedFileList);
|
|
}
|
|
});
|
|
};
|
|
|
|
const onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
|
setDragState(e.type);
|
|
};
|
|
|
|
const mergedBeforeUpload = async (file: RcFile, fileListArgs: RcFile[]) => {
|
|
const { beforeUpload, transformFile } = props;
|
|
|
|
let parsedFile: File | Blob | string = file;
|
|
if (beforeUpload) {
|
|
const result = await beforeUpload(file, fileListArgs);
|
|
|
|
if (result === false) {
|
|
return false;
|
|
}
|
|
|
|
// Hack for LIST_IGNORE, we add additional info to remove from the list
|
|
delete (file as any)[LIST_IGNORE];
|
|
if ((result as any) === LIST_IGNORE) {
|
|
Object.defineProperty(file, LIST_IGNORE, {
|
|
value: true,
|
|
configurable: true,
|
|
});
|
|
return false;
|
|
}
|
|
|
|
if (typeof result === 'object' && result) {
|
|
parsedFile = result as File;
|
|
}
|
|
}
|
|
|
|
if (transformFile) {
|
|
parsedFile = await transformFile(parsedFile as any);
|
|
}
|
|
|
|
return parsedFile as RcFile;
|
|
};
|
|
|
|
// Test needs
|
|
React.useImperativeHandle(ref, () => ({
|
|
onStart,
|
|
onSuccess,
|
|
onProgress,
|
|
onError,
|
|
fileList: mergedFileList,
|
|
upload: upload.current,
|
|
}));
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
|
|
|
const rcUploadProps = {
|
|
onBatchStart,
|
|
onStart,
|
|
onError,
|
|
onProgress,
|
|
onSuccess,
|
|
...props,
|
|
prefixCls,
|
|
beforeUpload: mergedBeforeUpload,
|
|
onChange: undefined,
|
|
};
|
|
|
|
delete rcUploadProps.className;
|
|
delete rcUploadProps.style;
|
|
|
|
// Remove id to avoid open by label when trigger is hidden
|
|
// !children: https://github.com/ant-design/ant-design/issues/14298
|
|
// disabled: https://github.com/ant-design/ant-design/issues/16478
|
|
// https://github.com/ant-design/ant-design/issues/24197
|
|
if (!children || disabled) {
|
|
delete rcUploadProps.id;
|
|
}
|
|
|
|
const renderUploadList = (button?: React.ReactNode) =>
|
|
showUploadList ? (
|
|
<LocaleReceiver componentName="Upload" defaultLocale={defaultLocale.Upload}>
|
|
{(locale: UploadLocale) => {
|
|
const { showRemoveIcon, showPreviewIcon, showDownloadIcon, removeIcon, downloadIcon } =
|
|
typeof showUploadList === 'boolean' ? ({} as ShowUploadListInterface) : showUploadList;
|
|
return (
|
|
<UploadList
|
|
listType={listType}
|
|
items={mergedFileList}
|
|
previewFile={previewFile}
|
|
onPreview={onPreview}
|
|
onDownload={onDownload}
|
|
onRemove={handleRemove}
|
|
showRemoveIcon={!disabled && showRemoveIcon}
|
|
showPreviewIcon={showPreviewIcon}
|
|
showDownloadIcon={showDownloadIcon}
|
|
removeIcon={removeIcon}
|
|
downloadIcon={downloadIcon}
|
|
iconRender={iconRender}
|
|
locale={{ ...locale, ...propLocale }}
|
|
isImageUrl={isImageUrl}
|
|
progress={progress}
|
|
appendAction={button}
|
|
itemRender={itemRender}
|
|
/>
|
|
);
|
|
}}
|
|
</LocaleReceiver>
|
|
) : (
|
|
button
|
|
);
|
|
|
|
if (type === 'drag') {
|
|
const dragCls = classNames(
|
|
prefixCls,
|
|
{
|
|
[`${prefixCls}-drag`]: true,
|
|
[`${prefixCls}-drag-uploading`]: mergedFileList.some(file => file.status === 'uploading'),
|
|
[`${prefixCls}-drag-hover`]: dragState === 'dragover',
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
},
|
|
className,
|
|
);
|
|
return (
|
|
<span>
|
|
<div
|
|
className={dragCls}
|
|
onDrop={onFileDrop}
|
|
onDragOver={onFileDrop}
|
|
onDragLeave={onFileDrop}
|
|
style={style}
|
|
>
|
|
<RcUpload {...rcUploadProps} ref={upload} className={`${prefixCls}-btn`}>
|
|
<div className={`${prefixCls}-drag-container`}>{children}</div>
|
|
</RcUpload>
|
|
</div>
|
|
{renderUploadList()}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
const uploadButtonCls = classNames(prefixCls, {
|
|
[`${prefixCls}-select`]: true,
|
|
[`${prefixCls}-select-${listType}`]: true,
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
});
|
|
|
|
const uploadButton = (
|
|
<div className={uploadButtonCls} style={children ? undefined : { display: 'none' }}>
|
|
<RcUpload {...rcUploadProps} ref={upload} />
|
|
</div>
|
|
);
|
|
|
|
if (listType === 'picture-card') {
|
|
return (
|
|
<span className={classNames(`${prefixCls}-picture-card-wrapper`, className)}>
|
|
{renderUploadList(uploadButton)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<span className={className}>
|
|
{uploadButton}
|
|
{renderUploadList()}
|
|
</span>
|
|
);
|
|
};
|
|
|
|
interface CompoundedComponent
|
|
extends React.ForwardRefExoticComponent<
|
|
React.PropsWithChildren<UploadProps> & React.RefAttributes<any>
|
|
> {
|
|
Dragger: typeof Dragger;
|
|
LIST_IGNORE: {};
|
|
}
|
|
|
|
const Upload = React.forwardRef<unknown, UploadProps>(InternalUpload) as CompoundedComponent;
|
|
|
|
Upload.Dragger = Dragger;
|
|
|
|
Upload.LIST_IGNORE = LIST_IGNORE;
|
|
|
|
Upload.displayName = 'Upload';
|
|
|
|
Upload.defaultProps = {
|
|
type: 'select' as UploadType,
|
|
multiple: false,
|
|
action: '',
|
|
data: {},
|
|
accept: '',
|
|
beforeUpload: T,
|
|
showUploadList: true,
|
|
listType: 'text' as UploadListType, // or picture
|
|
className: '',
|
|
disabled: false,
|
|
supportServerRender: true,
|
|
};
|
|
|
|
export default Upload;
|