2017-03-17 11:47:05 +08:00
|
|
|
import React from 'react';
|
|
|
|
import RcUpload from 'rc-upload';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2017-03-17 11:47:05 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import Dragger from './Dragger';
|
|
|
|
import UploadList from './UploadList';
|
|
|
|
import { UploadProps, UploadLocale } from './interface';
|
|
|
|
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils';
|
|
|
|
|
2017-03-20 15:20:15 +08:00
|
|
|
export interface UploadContext {
|
|
|
|
antLocale?: {
|
|
|
|
Upload?: any,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
const defaultLocale: UploadLocale = {
|
|
|
|
uploading: '文件上传中',
|
|
|
|
removeFile: '删除文件',
|
|
|
|
uploadError: '上传错误',
|
|
|
|
previewFile: '预览文件',
|
|
|
|
};
|
|
|
|
|
|
|
|
export { UploadProps };
|
|
|
|
|
|
|
|
export default class Upload extends React.Component<UploadProps, any> {
|
|
|
|
static Dragger: typeof Dragger;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-upload',
|
|
|
|
type: 'select',
|
|
|
|
multiple: false,
|
|
|
|
action: '',
|
|
|
|
data: {},
|
|
|
|
accept: '',
|
|
|
|
beforeUpload: T,
|
|
|
|
showUploadList: true,
|
|
|
|
listType: 'text', // or pictrue
|
|
|
|
className: '',
|
|
|
|
disabled: false,
|
|
|
|
supportServerRender: true,
|
|
|
|
};
|
|
|
|
|
2017-03-20 15:20:15 +08:00
|
|
|
static contextTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
antLocale: PropTypes.object,
|
2017-03-20 15:20:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
context: UploadContext;
|
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
recentUploadStatus: boolean | PromiseLike<any>;
|
|
|
|
progressTimer: any;
|
|
|
|
refs: {
|
|
|
|
[key: string]: any;
|
|
|
|
upload: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
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',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.clearProgressTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
getLocale() {
|
|
|
|
let locale = {};
|
|
|
|
if (this.context.antLocale && this.context.antLocale.Upload) {
|
|
|
|
locale = this.context.antLocale.Upload;
|
|
|
|
}
|
2017-07-03 16:57:11 +08:00
|
|
|
return {
|
|
|
|
...defaultLocale,
|
|
|
|
...locale,
|
|
|
|
...this.props.locale,
|
|
|
|
};
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onStart = (file) => {
|
|
|
|
let targetItem;
|
|
|
|
let nextFileList = this.state.fileList.concat();
|
|
|
|
if (file.length > 0) {
|
|
|
|
targetItem = file.map(f => {
|
|
|
|
const fileObject = fileToObject(f);
|
|
|
|
fileObject.status = 'uploading';
|
|
|
|
return fileObject;
|
|
|
|
});
|
|
|
|
nextFileList = nextFileList.concat(targetItem);
|
|
|
|
} else {
|
|
|
|
targetItem = fileToObject(file);
|
|
|
|
targetItem.status = 'uploading';
|
|
|
|
nextFileList.push(targetItem);
|
|
|
|
}
|
|
|
|
this.onChange({
|
|
|
|
file: targetItem,
|
|
|
|
fileList: nextFileList,
|
|
|
|
});
|
|
|
|
// fix ie progress
|
|
|
|
if (!(window as any).FormData) {
|
|
|
|
this.autoUpdateProgress(0, targetItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
autoUpdateProgress(_, file) {
|
|
|
|
const getPercent = genPercentAdd();
|
|
|
|
let curPercent = 0;
|
|
|
|
this.clearProgressTimer();
|
|
|
|
this.progressTimer = setInterval(() => {
|
|
|
|
curPercent = getPercent(curPercent);
|
|
|
|
this.onProgress({
|
|
|
|
percent: curPercent,
|
|
|
|
}, file);
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess = (response, file) => {
|
|
|
|
this.clearProgressTimer();
|
|
|
|
try {
|
|
|
|
if (typeof response === 'string') {
|
|
|
|
response = JSON.parse(response);
|
|
|
|
}
|
|
|
|
} catch (e) { /* do nothing */
|
|
|
|
}
|
|
|
|
let fileList = this.state.fileList;
|
|
|
|
let targetItem = getFileItem(file, fileList);
|
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.status = 'done';
|
|
|
|
targetItem.response = response;
|
|
|
|
this.onChange({
|
|
|
|
file: { ...targetItem },
|
|
|
|
fileList,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onProgress = (e, file) => {
|
|
|
|
let fileList = this.state.fileList;
|
|
|
|
let targetItem = getFileItem(file, fileList);
|
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.percent = e.percent;
|
|
|
|
this.onChange({
|
|
|
|
event: e,
|
|
|
|
file: { ...targetItem },
|
|
|
|
fileList: this.state.fileList,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onError = (error, response, file) => {
|
|
|
|
this.clearProgressTimer();
|
|
|
|
let fileList = this.state.fileList;
|
|
|
|
let targetItem = getFileItem(file, fileList);
|
|
|
|
// removed
|
|
|
|
if (!targetItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
targetItem.error = error;
|
|
|
|
targetItem.response = response;
|
|
|
|
targetItem.status = 'error';
|
|
|
|
this.onChange({
|
|
|
|
file: { ...targetItem },
|
|
|
|
fileList,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRemove(file) {
|
|
|
|
const { onRemove } = this.props;
|
2017-05-02 16:31:39 +08:00
|
|
|
|
|
|
|
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(ret => {
|
|
|
|
// Prevent removing file
|
|
|
|
if (ret === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const removedFileList = removeFileItem(file, this.state.fileList);
|
|
|
|
if (removedFileList) {
|
|
|
|
this.onChange({
|
|
|
|
file,
|
|
|
|
fileList: removedFileList,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-03-17 11:47:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleManualRemove = (file) => {
|
|
|
|
this.refs.upload.abort(file);
|
|
|
|
file.status = 'removed'; // eslint-disable-line
|
|
|
|
this.handleRemove(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange = (info) => {
|
|
|
|
if (!('fileList' in this.props)) {
|
|
|
|
this.setState({ fileList: info.fileList });
|
|
|
|
}
|
|
|
|
|
|
|
|
const { onChange } = this.props;
|
|
|
|
if (onChange) {
|
|
|
|
onChange(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if ('fileList' in nextProps) {
|
|
|
|
this.setState({
|
|
|
|
fileList: nextProps.fileList || [],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onFileDrop = (e) => {
|
|
|
|
this.setState({
|
|
|
|
dragState: e.type,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-29 16:52:16 +08:00
|
|
|
beforeUpload = (file, fileList) => {
|
2017-10-09 19:41:17 +08:00
|
|
|
if (!this.props.beforeUpload) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const result = this.props.beforeUpload(file, fileList);
|
2017-10-12 14:48:35 +08:00
|
|
|
if (result === false) {
|
2017-09-29 16:52:16 +08:00
|
|
|
this.onChange({
|
|
|
|
file,
|
|
|
|
fileList,
|
|
|
|
});
|
|
|
|
return false;
|
2017-10-12 14:48:35 +08:00
|
|
|
} else 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;
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:47:05 +08:00
|
|
|
clearProgressTimer() {
|
|
|
|
clearInterval(this.progressTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
prefixCls = '', showUploadList, listType, onPreview,
|
|
|
|
type, disabled, children, className,
|
|
|
|
} = this.props;
|
|
|
|
|
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,
|
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;
|
|
|
|
|
|
|
|
const { showRemoveIcon, showPreviewIcon } = showUploadList as any;
|
|
|
|
const uploadList = showUploadList ? (
|
|
|
|
<UploadList
|
|
|
|
listType={listType}
|
|
|
|
items={this.state.fileList}
|
|
|
|
onPreview={onPreview}
|
|
|
|
onRemove={this.handleManualRemove}
|
|
|
|
showRemoveIcon={showRemoveIcon}
|
|
|
|
showPreviewIcon={showPreviewIcon}
|
|
|
|
locale={this.getLocale()}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
if (type === 'drag') {
|
|
|
|
const dragCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-drag`]: true,
|
|
|
|
[`${prefixCls}-drag-uploading`]: this.state.fileList.some(file => file.status === 'uploading'),
|
|
|
|
[`${prefixCls}-drag-hover`]: this.state.dragState === 'dragover',
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<span className={className}>
|
|
|
|
<div
|
|
|
|
className={dragCls}
|
|
|
|
onDrop={this.onFileDrop}
|
|
|
|
onDragOver={this.onFileDrop}
|
|
|
|
onDragLeave={this.onFileDrop}
|
|
|
|
>
|
|
|
|
<RcUpload {...rcUploadProps} ref="upload" className={`${prefixCls}-btn`}>
|
|
|
|
<div className={`${prefixCls}-drag-container`}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</RcUpload>
|
|
|
|
</div>
|
|
|
|
{uploadList}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadButtonCls = classNames(prefixCls, {
|
|
|
|
[`${prefixCls}-select`]: true,
|
|
|
|
[`${prefixCls}-select-${listType}`]: true,
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
});
|
|
|
|
|
|
|
|
const uploadButton = (
|
2017-03-23 21:15:49 +08:00
|
|
|
<div className={uploadButtonCls} style={{ display: children ? '' : 'none' }}>
|
2017-03-17 11:47:05 +08:00
|
|
|
<RcUpload {...rcUploadProps} ref="upload" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (listType === 'picture-card') {
|
|
|
|
return (
|
|
|
|
<span className={className}>
|
|
|
|
{uploadList}
|
|
|
|
{uploadButton}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<span className={className}>
|
|
|
|
{uploadButton}
|
|
|
|
{uploadList}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|