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';
|
2018-03-08 13:36:12 +08:00
|
|
|
import uniqBy from 'lodash/uniqBy';
|
2019-01-28 19:13:45 +08:00
|
|
|
import findIndex from 'lodash/findIndex';
|
2017-03-17 11:47:05 +08:00
|
|
|
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,
|
2018-04-16 10:58:17 +08:00
|
|
|
UploadProps,
|
|
|
|
UploadState,
|
|
|
|
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';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2019-08-27 18:29:20 +08:00
|
|
|
import warning from '../_util/warning';
|
2017-03-17 11:47:05 +08:00
|
|
|
|
|
|
|
export { UploadProps };
|
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
class Upload extends React.Component<UploadProps, UploadState> {
|
2017-03-17 11:47:05 +08:00
|
|
|
static Dragger: typeof Dragger;
|
|
|
|
|
|
|
|
static defaultProps = {
|
2018-04-16 10:58:17 +08:00
|
|
|
type: 'select' as UploadType,
|
2017-03-17 11:47:05 +08:00
|
|
|
multiple: false,
|
|
|
|
action: '',
|
|
|
|
data: {},
|
|
|
|
accept: '',
|
|
|
|
beforeUpload: T,
|
|
|
|
showUploadList: true,
|
2019-03-18 10:47:17 +08:00
|
|
|
listType: 'text' as UploadListType, // or picture
|
2017-03-17 11:47:05 +08:00
|
|
|
className: '',
|
|
|
|
disabled: false,
|
|
|
|
supportServerRender: true,
|
|
|
|
};
|
|
|
|
|
2018-08-10 17:04:40 +08:00
|
|
|
static getDerivedStateFromProps(nextProps: UploadProps) {
|
|
|
|
if ('fileList' in nextProps) {
|
|
|
|
return {
|
|
|
|
fileList: nextProps.fileList || [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
recentUploadStatus: boolean | PromiseLike<any>;
|
2019-01-11 17:20:48 +08:00
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
progressTimer: any;
|
2017-09-08 09:46:22 +08:00
|
|
|
|
2019-02-27 18:37:44 +08:00
|
|
|
upload: any;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2017-11-20 16:31:41 +08:00
|
|
|
constructor(props: UploadProps) {
|
2017-03-17 11:47:05 +08:00
|
|
|
super(props);
|
2017-11-10 09:47:30 +08:00
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
this.state = {
|
2017-11-10 09:47:30 +08:00
|
|
|
fileList: props.fileList || props.defaultFileList || [],
|
2017-03-17 11:47:05 +08:00
|
|
|
dragState: 'drop',
|
|
|
|
};
|
2019-08-27 18:29:20 +08:00
|
|
|
|
|
|
|
warning(
|
|
|
|
'fileList' in props || !('value' in props),
|
|
|
|
'Upload',
|
|
|
|
'`value` is not validate prop, do you mean `fileList`?',
|
|
|
|
);
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.clearProgressTimer();
|
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
saveUpload = (node: typeof RcUpload) => {
|
|
|
|
this.upload = node;
|
|
|
|
};
|
|
|
|
|
2018-04-19 11:54:28 +08:00
|
|
|
onStart = (file: RcFile) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { fileList } = this.state;
|
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
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
const nextFileList = fileList.concat();
|
2018-09-19 22:37:35 +08:00
|
|
|
|
2019-01-28 19:13:45 +08:00
|
|
|
const fileIndex = findIndex(nextFileList, ({ uid }: UploadFile) => uid === targetItem.uid);
|
2018-09-19 22:37:35 +08:00
|
|
|
if (fileIndex === -1) {
|
|
|
|
nextFileList.push(targetItem);
|
|
|
|
} else {
|
|
|
|
nextFileList[fileIndex] = targetItem;
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
this.onChange({
|
|
|
|
file: targetItem,
|
|
|
|
fileList: nextFileList,
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2019-11-04 10:44:43 +08:00
|
|
|
onSuccess = (response: any, file: UploadFile, xhr: any) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
this.clearProgressTimer();
|
|
|
|
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
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
const { fileList } = this.state;
|
2018-11-10 21:40:21 +08:00
|
|
|
const targetItem = getFileItem(file, fileList);
|
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;
|
2017-03-17 11:47:05 +08:00
|
|
|
this.onChange({
|
|
|
|
file: { ...targetItem },
|
|
|
|
fileList,
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2017-11-20 16:31:41 +08:00
|
|
|
onProgress = (e: { percent: number }, file: UploadFile) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { fileList } = this.state;
|
2018-11-10 21:40:21 +08:00
|
|
|
const targetItem = getFileItem(file, fileList);
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.percent = e.percent;
|
|
|
|
this.onChange({
|
|
|
|
event: e,
|
|
|
|
file: { ...targetItem },
|
2019-08-05 18:38:10 +08:00
|
|
|
fileList,
|
2017-03-17 11:47:05 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2017-11-20 16:31:41 +08:00
|
|
|
onError = (error: Error, response: any, file: UploadFile) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
this.clearProgressTimer();
|
2019-08-05 18:38:10 +08:00
|
|
|
const { fileList } = this.state;
|
2018-11-10 21:40:21 +08:00
|
|
|
const targetItem = getFileItem(file, fileList);
|
2017-03-17 11:47:05 +08:00
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.error = error;
|
|
|
|
targetItem.response = response;
|
|
|
|
targetItem.status = 'error';
|
|
|
|
this.onChange({
|
|
|
|
file: { ...targetItem },
|
|
|
|
fileList,
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2019-09-23 10:16:09 +08:00
|
|
|
handleRemove = (file: UploadFile) => {
|
|
|
|
const { onRemove } = this.props;
|
|
|
|
const { fileList } = this.state;
|
|
|
|
|
|
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => {
|
|
|
|
// Prevent removing file
|
|
|
|
if (ret === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const removedFileList = removeFileItem(file, fileList);
|
|
|
|
|
|
|
|
if (removedFileList) {
|
|
|
|
file.status = 'removed'; // eslint-disable-line
|
|
|
|
|
|
|
|
if (this.upload) {
|
|
|
|
this.upload.abort(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onChange({
|
|
|
|
file,
|
|
|
|
fileList: removedFileList,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2018-02-11 16:11:13 +08:00
|
|
|
onChange = (info: UploadChangeParam) => {
|
|
|
|
if (!('fileList' in this.props)) {
|
2017-03-17 11:47:05 +08:00
|
|
|
this.setState({ fileList: info.fileList });
|
|
|
|
}
|
|
|
|
|
|
|
|
const { onChange } = this.props;
|
|
|
|
if (onChange) {
|
|
|
|
onChange(info);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2017-11-20 16:31:41 +08:00
|
|
|
onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
this.setState({
|
|
|
|
dragState: e.type,
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2018-04-19 11:54:28 +08:00
|
|
|
beforeUpload = (file: RcFile, fileList: RcFile[]) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { beforeUpload } = this.props;
|
|
|
|
const { fileList: stateFileList } = this.state;
|
|
|
|
if (!beforeUpload) {
|
2017-10-09 19:41:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
const result = beforeUpload(file, fileList);
|
2017-10-12 14:48:35 +08:00
|
|
|
if (result === false) {
|
2017-09-29 16:52:16 +08:00
|
|
|
this.onChange({
|
2018-04-28 11:06:32 +08:00
|
|
|
file,
|
2018-06-29 13:57:38 +08:00
|
|
|
fileList: uniqBy(
|
2019-08-05 18:38:10 +08:00
|
|
|
stateFileList.concat(fileList.map(fileToObject)),
|
2018-06-30 19:05:09 +08:00
|
|
|
(item: UploadFile) => item.uid,
|
2018-06-29 13:57:38 +08:00
|
|
|
),
|
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
|
|
|
};
|
2017-09-29 16:52:16 +08:00
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
clearProgressTimer() {
|
|
|
|
clearInterval(this.progressTimer);
|
|
|
|
}
|
|
|
|
|
2017-11-20 16:31:41 +08:00
|
|
|
renderUploadList = (locale: UploadLocale) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const {
|
|
|
|
showUploadList,
|
|
|
|
listType,
|
|
|
|
onPreview,
|
2019-09-11 21:46:56 +08:00
|
|
|
onDownload,
|
2019-08-05 18:38:10 +08:00
|
|
|
previewFile,
|
|
|
|
disabled,
|
|
|
|
locale: propLocale,
|
|
|
|
} = this.props;
|
2019-09-11 21:46:56 +08:00
|
|
|
const { showRemoveIcon, showPreviewIcon, showDownloadIcon } = showUploadList as any;
|
2019-08-05 18:38:10 +08:00
|
|
|
const { fileList } = this.state;
|
2017-10-20 14:54:38 +08:00
|
|
|
return (
|
|
|
|
<UploadList
|
|
|
|
listType={listType}
|
2019-08-05 18:38:10 +08:00
|
|
|
items={fileList}
|
2019-05-06 12:04:39 +08:00
|
|
|
previewFile={previewFile}
|
2017-10-20 14:54:38 +08:00
|
|
|
onPreview={onPreview}
|
2019-09-11 21:46:56 +08:00
|
|
|
onDownload={onDownload}
|
2019-09-23 10:16:09 +08:00
|
|
|
onRemove={this.handleRemove}
|
2019-05-25 16:34:38 +08:00
|
|
|
showRemoveIcon={!disabled && showRemoveIcon}
|
2017-10-20 14:54:38 +08:00
|
|
|
showPreviewIcon={showPreviewIcon}
|
2019-09-11 21:46:56 +08:00
|
|
|
showDownloadIcon={showDownloadIcon}
|
2019-08-05 18:38:10 +08:00
|
|
|
locale={{ ...locale, ...propLocale }}
|
2017-10-20 14:54:38 +08:00
|
|
|
/>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-10-20 14:54:38 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
renderUpload = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2017-03-17 11:47:05 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2017-10-20 14:54:38 +08:00
|
|
|
className,
|
|
|
|
showUploadList,
|
|
|
|
listType,
|
|
|
|
type,
|
|
|
|
disabled,
|
|
|
|
children,
|
2019-11-28 18:00:59 +08:00
|
|
|
style,
|
2017-03-17 11:47:05 +08:00
|
|
|
} = this.props;
|
2019-08-05 18:38:10 +08:00
|
|
|
const { fileList, dragState } = this.state;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
|
|
|
|
2017-07-03 16:57:11 +08:00
|
|
|
const rcUploadProps = {
|
2017-03-17 11:47:05 +08:00
|
|
|
onStart: this.onStart,
|
|
|
|
onError: this.onError,
|
|
|
|
onProgress: this.onProgress,
|
|
|
|
onSuccess: this.onSuccess,
|
2017-07-03 16:57:11 +08:00
|
|
|
...this.props,
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls,
|
2017-09-29 16:52:16 +08:00
|
|
|
beforeUpload: this.beforeUpload,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
|
|
|
|
delete rcUploadProps.className;
|
2019-11-28 18:00:59 +08:00
|
|
|
delete rcUploadProps.style;
|
2017-03-17 11:47:05 +08:00
|
|
|
|
|
|
|
const uploadList = showUploadList ? (
|
2018-12-07 20:02:01 +08:00
|
|
|
<LocaleReceiver componentName="Upload" defaultLocale={defaultLocale.Upload}>
|
2017-10-20 14:54:38 +08:00
|
|
|
{this.renderUploadList}
|
|
|
|
</LocaleReceiver>
|
2017-03-17 11:47:05 +08:00
|
|
|
) : null;
|
|
|
|
|
|
|
|
if (type === 'drag') {
|
2019-11-28 18:00:59 +08:00
|
|
|
const dragCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-drag`]: true,
|
|
|
|
[`${prefixCls}-drag-uploading`]: fileList.some(file => file.status === 'uploading'),
|
|
|
|
[`${prefixCls}-drag-hover`]: dragState === 'dragover',
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2017-03-17 11:47:05 +08:00
|
|
|
return (
|
2019-11-28 18:00:59 +08:00
|
|
|
<span>
|
2017-03-17 11:47:05 +08:00
|
|
|
<div
|
|
|
|
className={dragCls}
|
|
|
|
onDrop={this.onFileDrop}
|
|
|
|
onDragOver={this.onFileDrop}
|
|
|
|
onDragLeave={this.onFileDrop}
|
2019-11-28 18:00:59 +08:00
|
|
|
style={style}
|
2017-03-17 11:47:05 +08:00
|
|
|
>
|
2017-09-08 09:46:22 +08:00
|
|
|
<RcUpload {...rcUploadProps} ref={this.saveUpload} className={`${prefixCls}-btn`}>
|
2018-12-07 20:02:01 +08:00
|
|
|
<div className={`${prefixCls}-drag-container`}>{children}</div>
|
2017-03-17 11:47:05 +08:00
|
|
|
</RcUpload>
|
|
|
|
</div>
|
|
|
|
{uploadList}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadButtonCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-select`]: true,
|
|
|
|
[`${prefixCls}-select-${listType}`]: true,
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2017-03-17 11:47:05 +08:00
|
|
|
});
|
|
|
|
|
2019-02-11 20:29:43 +08:00
|
|
|
// Remove id to avoid open by label when trigger is hidden
|
|
|
|
// https://github.com/ant-design/ant-design/issues/14298
|
2019-05-08 20:26:19 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/16478
|
|
|
|
if (!children || disabled) {
|
2019-02-11 20:29:43 +08:00
|
|
|
delete rcUploadProps.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadButton = (
|
|
|
|
<div className={uploadButtonCls} style={children ? undefined : { display: 'none' }}>
|
2017-09-08 09:46:22 +08:00
|
|
|
<RcUpload {...rcUploadProps} ref={this.saveUpload} />
|
2017-03-17 11:47:05 +08:00
|
|
|
</div>
|
2019-02-11 20:29:43 +08:00
|
|
|
);
|
2017-03-17 11:47:05 +08:00
|
|
|
|
|
|
|
if (listType === 'picture-card') {
|
|
|
|
return (
|
2019-07-22 19:18:47 +08:00
|
|
|
<span className={classNames(className, `${prefixCls}-picture-card-wrapper`)}>
|
2017-03-17 11:47:05 +08:00
|
|
|
{uploadList}
|
|
|
|
{uploadButton}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2019-07-22 19:18:47 +08:00
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
return (
|
|
|
|
<span className={className}>
|
|
|
|
{uploadButton}
|
|
|
|
{uploadList}
|
|
|
|
</span>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderUpload}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
2018-08-10 17:04:40 +08:00
|
|
|
|
|
|
|
export default Upload;
|