You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
7.1 KiB

import React from 'react';
import RcUpload from 'rc-upload';
import UploadList from './uploadList';
import getFileItem from './getFileItem';
import classNames from 'classnames';
import assign from 'object-assign';
import { UploadProps } from './interface';
function T() {
return true;
}
// Fix IE file.status problem
// via coping a new Object
function fileToObject(file): any {
return {
lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate,
9 years ago
name: file.filename || file.name,
size: file.size,
type: file.type,
9 years ago
uid: file.uid,
response: file.response,
error: file.error,
percent: 0,
originFileObj: file,
status: null,
};
}
/**
* Progress percent: 0.1 -> 0.98
* - for ie
*/
function genPercentAdd() {
let k = 0.1;
const i = 0.01;
const end = 0.98;
return function (s) {
let start = s;
if (start >= end) {
return start;
}
start += k;
k = k - i;
if (k < 0.001) {
k = 0.001;
}
return start * 100;
};
}
export { UploadProps };
export function Dragger(props) {
8 years ago
return <Upload {...props} type="drag" style={{ height: props.height }}/>;
}
export default class Upload extends React.Component<UploadProps, any> {
static Dragger = 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;
refs: {
[key: string]: any;
upload: any;
};
constructor(props) {
super(props);
this.state = {
fileList: this.props.fileList || this.props.defaultFileList || [],
dragState: 'drop',
};
}
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);
}
9 years ago
this.onChange({
file: targetItem,
fileList: nextFileList,
9 years ago
});
// fix ie progress
if (!(window as any).FormData) {
this.autoUpdateProgress(0, targetItem);
}
}
8 years ago
autoUpdateProgress(_, file) {
const getPercent = genPercentAdd();
let curPercent = 0;
this.progressTimer = setInterval(() => {
curPercent = getPercent(curPercent);
this.onProgress({
percent: curPercent,
}, file);
}, 200);
}
removeFile(file) {
let fileList = this.state.fileList;
let targetItem = getFileItem(file, fileList);
let index = fileList.indexOf(targetItem);
if (index !== -1) {
fileList.splice(index, 1);
9 years ago
return fileList;
}
9 years ago
return null;
}
onSuccess = (response, file) => {
this.clearProgressTimer();
9 years ago
try {
if (typeof response === 'string') {
response = JSON.parse(response);
}
8 years ago
} catch (e) { /* do nothing */
}
let fileList = this.state.fileList;
9 years ago
let targetItem = getFileItem(file, fileList);
// removed
if (!targetItem) {
return;
}
targetItem.status = 'done';
targetItem.response = response;
this.onChange({
file: targetItem,
fileList,
});
}
onProgress = (e, file) => {
9 years ago
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();
9 years ago
let fileList = this.state.fileList;
let targetItem = getFileItem(file, fileList);
// removed
if (!targetItem) {
return;
}
9 years ago
targetItem.error = error;
targetItem.response = response;
targetItem.status = 'error';
this.handleRemove(targetItem);
}
9 years ago
handleRemove(file) {
const onRemove = this.props.onRemove;
if (onRemove) {
onRemove(file);
}
let fileList = this.removeFile(file);
9 years ago
if (fileList) {
this.onChange({
file,
fileList,
9 years ago
});
}
}
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.onChange;
if (onChange) {
onChange(info);
}
}
componentWillReceiveProps(nextProps) {
if ('fileList' in nextProps) {
this.setState({
9 years ago
fileList: nextProps.fileList || [],
});
}
}
onFileDrop = (e) => {
9 years ago
this.setState({
dragState: e.type,
9 years ago
});
}
clearProgressTimer() {
clearInterval(this.progressTimer);
}
render() {
const {
prefixCls = '', showUploadList, listType, onPreview,
type, disabled, children, className,
} = this.props;
const rcUploadProps = assign({}, this.props, {
onStart: this.onStart,
onError: this.onError,
onProgress: this.onProgress,
onSuccess: this.onSuccess,
});
delete rcUploadProps.className;
const uploadList = showUploadList ? (
<UploadList
listType={listType}
items={this.state.fileList}
onPreview={onPreview}
onRemove={this.handleManualRemove}
/>
) : 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>
9 years ago
{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="upload" />
</div>
);
if (listType === 'picture-card') {
return (
<span className={className}>
{uploadList}
{uploadButton}
9 years ago
</span>
);
}
return (
<span className={className}>
{uploadButton}
{uploadList}
</span>
);
}
}