ant-design/components/upload/Upload.tsx

307 lines
7.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import RcUpload from 'rc-upload';
import classNames from 'classnames';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import Dragger from './Dragger';
import UploadList from './UploadList';
2017-11-20 16:31:41 +08:00
import { UploadProps, UploadState, UploadFile, UploadLocale, UploadChangeParam } from './interface';
import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils';
export { UploadProps };
2017-11-20 16:31:41 +08:00
export default class Upload extends React.Component<UploadProps, UploadState> {
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,
};
recentUploadStatus: boolean | PromiseLike<any>;
progressTimer: any;
2017-09-17 15:48:44 +08:00
private upload: any;
2017-11-20 16:31:41 +08:00
constructor(props: UploadProps) {
super(props);
2017-11-10 09:47:30 +08:00
this.state = {
2017-11-10 09:47:30 +08:00
fileList: props.fileList || props.defaultFileList || [],
dragState: 'drop',
};
}
componentWillUnmount() {
this.clearProgressTimer();
}
2017-11-20 16:31:41 +08:00
onStart = (file: UploadFile) => {
let targetItem;
let nextFileList = this.state.fileList.concat();
2017-11-20 16:31:41 +08:00
targetItem = fileToObject(file);
2017-11-20 16:53:56 +08:00
targetItem.status = 'uploading';
2017-11-20 16:31:41 +08:00
nextFileList.push(targetItem);
this.onChange({
file: targetItem,
fileList: nextFileList,
});
// fix ie progress
if (!(window as any).FormData) {
this.autoUpdateProgress(0, targetItem);
}
}
2017-11-20 16:31:41 +08:00
autoUpdateProgress(_: any, file: UploadFile) {
const getPercent = genPercentAdd();
let curPercent = 0;
this.clearProgressTimer();
this.progressTimer = setInterval(() => {
curPercent = getPercent(curPercent);
this.onProgress({
percent: curPercent,
}, file);
}, 200);
}
2017-11-20 16:31:41 +08:00
onSuccess = (response: any, file: UploadFile) => {
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,
});
}
2017-11-20 16:31:41 +08:00
onProgress = (e: { percent: number }, file: UploadFile) => {
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,
});
}
2017-11-20 16:31:41 +08:00
onError = (error: Error, response: any, file: UploadFile) => {
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,
});
}
2017-11-20 16:31:41 +08:00
handleRemove(file: UploadFile) {
const { onRemove } = this.props;
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-11-20 16:31:41 +08:00
handleManualRemove = (file: UploadFile) => {
this.upload.abort(file);
file.status = 'removed'; // eslint-disable-line
this.handleRemove(file);
}
2017-11-20 16:31:41 +08:00
onChange = (info: UploadChangeParam) => {
if (!('fileList' in this.props)) {
this.setState({ fileList: info.fileList });
}
const { onChange } = this.props;
if (onChange) {
onChange(info);
}
}
2017-11-20 16:31:41 +08:00
componentWillReceiveProps(nextProps: UploadProps) {
if ('fileList' in nextProps) {
this.setState({
fileList: nextProps.fileList || [],
});
}
}
2017-11-20 16:31:41 +08:00
onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
this.setState({
dragState: e.type,
});
}
2017-11-20 16:31:41 +08:00
beforeUpload = (file: UploadFile, fileList: UploadFile[]) => {
if (!this.props.beforeUpload) {
return true;
}
const result = this.props.beforeUpload(file, fileList);
if (result === false) {
this.onChange({
file,
fileList,
});
return false;
} else if (result && (result as PromiseLike<any>).then) {
return result;
}
return true;
}
clearProgressTimer() {
clearInterval(this.progressTimer);
}
2017-11-20 16:31:41 +08:00
saveUpload = (node: typeof RcUpload) => {
this.upload = node;
}
2017-11-20 16:31:41 +08:00
renderUploadList = (locale: UploadLocale) => {
const { showUploadList, listType, onPreview } = this.props;
const { showRemoveIcon, showPreviewIcon } = showUploadList as any;
return (
<UploadList
listType={listType}
items={this.state.fileList}
onPreview={onPreview}
onRemove={this.handleManualRemove}
showRemoveIcon={showRemoveIcon}
showPreviewIcon={showPreviewIcon}
locale={{ ...locale, ...this.props.locale }}
/>
);
}
render() {
const {
prefixCls = '',
className,
showUploadList,
listType,
type,
disabled,
children,
} = this.props;
const rcUploadProps = {
onStart: this.onStart,
onError: this.onError,
onProgress: this.onProgress,
onSuccess: this.onSuccess,
...this.props,
beforeUpload: this.beforeUpload,
};
delete rcUploadProps.className;
const uploadList = showUploadList ? (
<LocaleReceiver
componentName="Upload"
defaultLocale={defaultLocale.Upload}
>
{this.renderUploadList}
</LocaleReceiver>
) : 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={this.saveUpload} 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 = (
<div className={uploadButtonCls} style={{ display: children ? '' : 'none' }}>
<RcUpload {...rcUploadProps} ref={this.saveUpload} />
</div>
);
if (listType === 'picture-card') {
return (
<span className={className}>
{uploadList}
{uploadButton}
</span>
);
}
return (
<span className={className}>
{uploadButton}
{uploadList}
</span>
);
}
}