2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-03-17 11:47:05 +08:00
|
|
|
import RcUpload from 'rc-upload';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import Dragger from './Dragger';
|
|
|
|
import UploadList from './UploadList';
|
2018-04-16 10:58:17 +08:00
|
|
|
import {
|
2018-04-19 11:54:28 +08:00
|
|
|
RcFile,
|
2020-08-26 18:43:34 +08:00
|
|
|
ShowUploadListInterface,
|
2018-04-16 10:58:17 +08:00
|
|
|
UploadProps,
|
|
|
|
UploadFile,
|
|
|
|
UploadLocale,
|
|
|
|
UploadChangeParam,
|
|
|
|
UploadType,
|
|
|
|
UploadListType,
|
|
|
|
} from './interface';
|
2019-12-31 11:54:39 +08:00
|
|
|
import { T, fileToObject, getFileItem, removeFileItem } from './utils';
|
2018-12-05 19:12:18 +08:00
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2019-07-24 10:34:55 +08:00
|
|
|
import defaultLocale from '../locale/default';
|
2020-08-18 15:44:31 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2020-08-19 09:50:04 +08:00
|
|
|
import useForceUpdate from '../_util/hooks/useForceUpdate';
|
2020-09-07 16:41:28 +08:00
|
|
|
import useFreshState from './useFreshState';
|
2017-03-17 11:47:05 +08:00
|
|
|
|
|
|
|
export { UploadProps };
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (props, ref) => {
|
|
|
|
const {
|
|
|
|
fileList: fileListProp,
|
|
|
|
defaultFileList,
|
|
|
|
onRemove,
|
|
|
|
showUploadList,
|
|
|
|
listType,
|
|
|
|
onPreview,
|
|
|
|
onDownload,
|
2020-12-15 17:09:52 +08:00
|
|
|
onChange,
|
2020-08-18 15:44:31 +08:00
|
|
|
previewFile,
|
|
|
|
disabled,
|
|
|
|
locale: propLocale,
|
|
|
|
iconRender,
|
|
|
|
isImageUrl,
|
|
|
|
progress,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
type,
|
|
|
|
children,
|
|
|
|
style,
|
2020-09-17 16:14:42 +08:00
|
|
|
itemRender,
|
2020-12-15 17:09:52 +08:00
|
|
|
maxCount,
|
2020-08-18 15:44:31 +08:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const [dragState, setDragState] = React.useState<string>('drop');
|
2020-08-29 21:28:44 +08:00
|
|
|
const forceUpdate = useForceUpdate();
|
|
|
|
|
2020-09-07 16:41:28 +08:00
|
|
|
// Refresh always use fresh data
|
|
|
|
const [getFileList, setFileList] = useFreshState<UploadFile<any>[]>(
|
|
|
|
fileListProp || defaultFileList || [],
|
|
|
|
fileListProp,
|
|
|
|
);
|
2020-08-18 15:44:31 +08:00
|
|
|
|
|
|
|
const upload = React.useRef<any>();
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-08-27 18:29:20 +08:00
|
|
|
'fileList' in props || !('value' in props),
|
|
|
|
'Upload',
|
2020-04-14 16:48:23 +08:00
|
|
|
'`value` is not a valid prop, do you mean `fileList`?',
|
2019-08-27 18:29:20 +08:00
|
|
|
);
|
2020-12-15 15:56:35 +08:00
|
|
|
|
|
|
|
devWarning(
|
2020-12-21 15:29:19 +08:00
|
|
|
!('transformFile' in props),
|
2020-12-15 15:56:35 +08:00
|
|
|
'Upload',
|
|
|
|
'`transformFile` is deprecated. Please use `beforeUpload` directly.',
|
|
|
|
);
|
2020-08-18 15:44:31 +08:00
|
|
|
}, []);
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2021-01-13 14:31:49 +08:00
|
|
|
// Control mode will auto fill file uid if not provided
|
|
|
|
React.useEffect(() => {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
|
|
|
|
(fileListProp || []).forEach((file, index) => {
|
|
|
|
file.uid = file.uid ?? `__AUTO__${timestamp}_${index}__`;
|
|
|
|
});
|
|
|
|
}, [fileListProp]);
|
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
const onInternalChange = (info: UploadChangeParam) => {
|
|
|
|
let cloneList = [...info.fileList];
|
2020-08-18 15:44:31 +08:00
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
// Cut to match count
|
|
|
|
if (maxCount === 1) {
|
|
|
|
cloneList = cloneList.slice(-1);
|
|
|
|
} else if (maxCount) {
|
|
|
|
cloneList = cloneList.slice(0, maxCount);
|
2020-08-18 15:44:31 +08:00
|
|
|
}
|
2020-12-15 17:09:52 +08:00
|
|
|
|
|
|
|
setFileList(cloneList);
|
|
|
|
|
|
|
|
onChange?.({
|
|
|
|
...info,
|
|
|
|
fileList: cloneList,
|
|
|
|
});
|
2019-08-05 18:38:10 +08:00
|
|
|
};
|
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const onStart = (file: RcFile) => {
|
2018-09-19 22:37:35 +08:00
|
|
|
const targetItem = fileToObject(file);
|
2017-11-20 16:53:56 +08:00
|
|
|
targetItem.status = 'uploading';
|
2018-09-19 22:37:35 +08:00
|
|
|
|
2020-09-07 16:41:28 +08:00
|
|
|
const nextFileList = getFileList().concat();
|
2018-09-19 22:37:35 +08:00
|
|
|
|
2020-03-28 19:03:11 +08:00
|
|
|
const fileIndex = nextFileList.findIndex(({ uid }: UploadFile) => uid === targetItem.uid);
|
2018-09-19 22:37:35 +08:00
|
|
|
if (fileIndex === -1) {
|
|
|
|
nextFileList.push(targetItem);
|
|
|
|
} else {
|
|
|
|
nextFileList[fileIndex] = targetItem;
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
onInternalChange({
|
2017-03-17 11:47:05 +08:00
|
|
|
file: targetItem,
|
|
|
|
fileList: nextFileList,
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const onSuccess = (response: any, file: UploadFile, xhr: any) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
try {
|
|
|
|
if (typeof response === 'string') {
|
|
|
|
response = JSON.parse(response);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
} catch (e) {
|
|
|
|
/* do nothing */
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
2020-09-07 16:41:28 +08:00
|
|
|
const targetItem = getFileItem(file, getFileList());
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.status = 'done';
|
|
|
|
targetItem.response = response;
|
2019-11-04 10:44:43 +08:00
|
|
|
targetItem.xhr = xhr;
|
2020-12-15 17:09:52 +08:00
|
|
|
onInternalChange({
|
2017-03-17 11:47:05 +08:00
|
|
|
file: { ...targetItem },
|
2020-09-07 16:41:28 +08:00
|
|
|
fileList: getFileList().concat(),
|
2017-03-17 11:47:05 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const onProgress = (e: { percent: number }, file: UploadFile) => {
|
2020-09-07 16:41:28 +08:00
|
|
|
const targetItem = getFileItem(file, getFileList());
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.percent = e.percent;
|
2020-12-15 17:09:52 +08:00
|
|
|
onInternalChange({
|
2017-03-17 11:47:05 +08:00
|
|
|
event: e,
|
|
|
|
file: { ...targetItem },
|
2020-09-07 16:41:28 +08:00
|
|
|
fileList: getFileList().concat(),
|
2017-03-17 11:47:05 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const onError = (error: Error, response: any, file: UploadFile) => {
|
2020-09-07 16:41:28 +08:00
|
|
|
const targetItem = getFileItem(file, getFileList());
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.error = error;
|
|
|
|
targetItem.response = response;
|
|
|
|
targetItem.status = 'error';
|
2020-12-15 17:09:52 +08:00
|
|
|
onInternalChange({
|
2017-03-17 11:47:05 +08:00
|
|
|
file: { ...targetItem },
|
2020-09-07 16:41:28 +08:00
|
|
|
fileList: getFileList().concat(),
|
2017-03-17 11:47:05 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const handleRemove = (file: UploadFile) => {
|
2021-01-01 21:40:42 +08:00
|
|
|
let currentFile: UploadFile;
|
2019-09-23 10:16:09 +08:00
|
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => {
|
|
|
|
// Prevent removing file
|
|
|
|
if (ret === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-01 21:40:42 +08:00
|
|
|
const fileList = getFileList();
|
|
|
|
const removedFileList = removeFileItem(file, fileList);
|
2019-09-23 10:16:09 +08:00
|
|
|
|
|
|
|
if (removedFileList) {
|
2021-01-01 21:40:42 +08:00
|
|
|
currentFile = { ...file, status: 'removed' };
|
2021-01-16 18:06:26 +08:00
|
|
|
fileList?.forEach(item => {
|
2021-01-01 21:40:42 +08:00
|
|
|
const matchKey = currentFile.uid !== undefined ? 'uid' : 'name';
|
|
|
|
if (item[matchKey] === currentFile[matchKey]) {
|
|
|
|
item.status = 'removed';
|
|
|
|
}
|
|
|
|
});
|
2021-01-16 18:06:26 +08:00
|
|
|
upload.current?.abort(currentFile);
|
2019-09-23 10:16:09 +08:00
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
onInternalChange({
|
2021-01-01 21:40:42 +08:00
|
|
|
file: currentFile,
|
2019-09-23 10:16:09 +08:00
|
|
|
fileList: removedFileList,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
setDragState(e.type);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const beforeUpload = (file: RcFile, fileListArgs: RcFile[]) => {
|
|
|
|
const { beforeUpload: beforeUploadProp } = props;
|
|
|
|
if (!beforeUploadProp) {
|
2017-10-09 19:41:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-18 15:44:31 +08:00
|
|
|
const result = beforeUploadProp(file, fileListArgs);
|
2017-10-12 14:48:35 +08:00
|
|
|
if (result === false) {
|
2020-03-28 19:03:11 +08:00
|
|
|
// Get unique file list
|
|
|
|
const uniqueList: UploadFile<any>[] = [];
|
2020-09-07 16:41:28 +08:00
|
|
|
getFileList()
|
|
|
|
.concat(fileListArgs.map(fileToObject))
|
|
|
|
.forEach(f => {
|
|
|
|
if (uniqueList.every(uf => uf.uid !== f.uid)) {
|
|
|
|
uniqueList.push(f);
|
|
|
|
}
|
|
|
|
});
|
2020-03-28 19:03:11 +08:00
|
|
|
|
2020-12-15 17:09:52 +08:00
|
|
|
onInternalChange({
|
2018-04-28 11:06:32 +08:00
|
|
|
file,
|
2020-03-28 19:03:11 +08:00
|
|
|
fileList: uniqueList,
|
2018-02-11 16:11:13 +08:00
|
|
|
});
|
2017-09-29 16:52:16 +08:00
|
|
|
return false;
|
2019-01-11 17:20:48 +08:00
|
|
|
}
|
|
|
|
if (result && (result as PromiseLike<any>).then) {
|
2017-10-09 19:41:17 +08:00
|
|
|
return result;
|
2017-09-29 16:52:16 +08:00
|
|
|
}
|
|
|
|
return true;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2020-08-18 15:44:31 +08:00
|
|
|
// Test needs
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
onStart,
|
|
|
|
onSuccess,
|
|
|
|
onProgress,
|
|
|
|
onError,
|
2020-09-07 16:41:28 +08:00
|
|
|
fileList: getFileList(),
|
2020-08-18 15:44:31 +08:00
|
|
|
upload: upload.current,
|
|
|
|
forceUpdate,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-05-16 14:25:49 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const rcUploadProps = {
|
|
|
|
onStart,
|
|
|
|
onError,
|
|
|
|
onProgress,
|
|
|
|
onSuccess,
|
|
|
|
...props,
|
|
|
|
prefixCls,
|
|
|
|
beforeUpload,
|
2020-12-17 15:09:18 +08:00
|
|
|
onChange: undefined,
|
2020-08-18 15:44:31 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
delete rcUploadProps.className;
|
|
|
|
delete rcUploadProps.style;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2019-07-22 19:18:47 +08:00
|
|
|
|
2020-08-24 16:33:24 +08:00
|
|
|
const renderUploadList = (button?: React.ReactNode) =>
|
|
|
|
showUploadList ? (
|
|
|
|
<LocaleReceiver componentName="Upload" defaultLocale={defaultLocale.Upload}>
|
|
|
|
{(locale: UploadLocale) => {
|
|
|
|
const { showRemoveIcon, showPreviewIcon, showDownloadIcon, removeIcon, downloadIcon } =
|
2020-08-26 18:43:34 +08:00
|
|
|
typeof showUploadList === 'boolean' ? ({} as ShowUploadListInterface) : showUploadList;
|
2020-08-24 16:33:24 +08:00
|
|
|
return (
|
|
|
|
<UploadList
|
|
|
|
listType={listType}
|
2020-09-07 16:41:28 +08:00
|
|
|
items={getFileList(true)}
|
2020-08-24 16:33:24 +08:00
|
|
|
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}
|
2020-09-17 16:14:42 +08:00
|
|
|
itemRender={itemRender}
|
2020-08-24 16:33:24 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</LocaleReceiver>
|
2020-08-26 18:43:34 +08:00
|
|
|
) : (
|
|
|
|
button
|
|
|
|
);
|
2020-08-18 15:44:31 +08:00
|
|
|
|
|
|
|
if (type === 'drag') {
|
|
|
|
const dragCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-drag`]: true,
|
2020-09-07 16:41:28 +08:00
|
|
|
[`${prefixCls}-drag-uploading`]: getFileList().some(file => file.status === 'uploading'),
|
2020-08-18 15:44:31 +08:00
|
|
|
[`${prefixCls}-drag-hover`]: dragState === 'dragover',
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2017-03-17 11:47:05 +08:00
|
|
|
return (
|
2020-08-18 15:44:31 +08:00
|
|
|
<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>
|
2020-08-24 16:33:24 +08:00
|
|
|
{renderUploadList()}
|
2017-03-17 11:47:05 +08:00
|
|
|
</span>
|
|
|
|
);
|
2020-08-18 15:44:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const uploadButtonCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-select`]: true,
|
|
|
|
[`${prefixCls}-select-${listType}`]: true,
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const uploadButton = (
|
|
|
|
<div className={uploadButtonCls} style={children ? undefined : { display: 'none' }}>
|
|
|
|
<RcUpload {...rcUploadProps} ref={upload} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (listType === 'picture-card') {
|
|
|
|
return (
|
2020-09-06 13:07:39 +08:00
|
|
|
<span className={classNames(`${prefixCls}-picture-card-wrapper`, className)}>
|
2020-08-24 16:33:24 +08:00
|
|
|
{renderUploadList(uploadButton)}
|
2020-08-18 15:44:31 +08:00
|
|
|
</span>
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2020-08-18 15:44:31 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<span className={className}>
|
|
|
|
{uploadButton}
|
2020-08-24 16:33:24 +08:00
|
|
|
{renderUploadList()}
|
2020-08-18 15:44:31 +08:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface CompoundedComponent
|
2020-08-23 22:46:55 +08:00
|
|
|
extends React.ForwardRefExoticComponent<
|
|
|
|
React.PropsWithChildren<UploadProps> & React.RefAttributes<any>
|
|
|
|
> {
|
2020-08-18 15:44:31 +08:00
|
|
|
Dragger: typeof Dragger;
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
2018-08-10 17:04:40 +08:00
|
|
|
|
2020-08-18 15:44:31 +08:00
|
|
|
const Upload = React.forwardRef<unknown, UploadProps>(InternalUpload) as CompoundedComponent;
|
|
|
|
|
|
|
|
Upload.Dragger = Dragger;
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
export default Upload;
|