ant-design/components/upload/index.jsx

140 lines
3.4 KiB
React
Raw Normal View History

2015-08-08 23:37:35 +08:00
import React from 'react';
import Upload from 'rc-upload';
import assign from 'object-assign';
import Message from '../message';
import UploadList from './uploadList';
2015-08-21 16:19:28 +08:00
import getFileItem from './getFileItem';
2015-08-08 23:37:35 +08:00
const prefixCls = 'ant-upload';
2015-08-22 14:45:03 +08:00
let fileIndex = 0;
2015-08-08 23:37:35 +08:00
2015-08-27 15:46:23 +08:00
function noop() {
}
2015-08-21 16:19:28 +08:00
const AntUpload = React.createClass({
2015-08-08 23:37:35 +08:00
getInitialState() {
return {
2015-08-31 18:13:16 +08:00
fileList: []
2015-08-08 23:37:35 +08:00
};
},
2015-08-27 15:45:38 +08:00
onStart(file) {
2015-08-31 18:13:16 +08:00
let nextFileList = this.state.fileList;
nextFileList.push({
2015-08-22 14:45:03 +08:00
index: fileIndex++,
uid: file.uid || '',
filename: file.name,
2015-08-27 15:45:38 +08:00
file: file,
2015-08-31 18:13:16 +08:00
status: 'uploading'
});
2015-08-31 18:13:16 +08:00
if (nextFileList.length === this.props.limit + 1) {
nextFileList = nextFileList.slice(1);
2015-08-31 12:13:55 +08:00
}
this.setState({
2015-08-31 18:13:16 +08:00
fileList: nextFileList
});
2015-08-27 15:45:38 +08:00
this.props.onStart(file);
2015-08-08 23:37:35 +08:00
},
2015-08-27 17:35:10 +08:00
removeFile(file){
2015-08-31 18:13:16 +08:00
var fileList = this.state.fileList.concat();
let targetItem = getFileItem(file, fileList);
var index = fileList.indexOf(targetItem);
2015-08-27 17:35:10 +08:00
if (index !== -1) {
2015-08-31 18:13:16 +08:00
fileList.splice(index, 1);
2015-08-27 17:35:10 +08:00
}
this.setState({
2015-08-31 18:13:16 +08:00
fileList: fileList
2015-08-27 17:35:10 +08:00
});
},
2015-08-27 15:45:38 +08:00
onSuccess(ret, file) {
2015-08-27 16:53:53 +08:00
var res = this.props.onSuccess(ret, file);
if (res !== false) {
2015-08-31 18:13:16 +08:00
var fileList = this.state.fileList.concat();
2015-08-27 16:53:53 +08:00
Message.success(file.name + '上传完成');
2015-08-31 18:13:16 +08:00
let targetItem = getFileItem(file, fileList);
2015-08-27 16:53:53 +08:00
targetItem.status = 'done';
// 解析出文件上传后的远程地址
if (typeof this.props.urlResolver === 'function') {
targetItem.url = this.props.urlResolver(ret);
}
2015-08-27 16:53:53 +08:00
this.setState({
2015-08-31 18:13:16 +08:00
fileList: fileList
2015-08-27 16:53:53 +08:00
});
2015-08-27 17:35:10 +08:00
} else {
this.removeFile(file);
2015-08-27 16:53:53 +08:00
}
2015-08-08 23:37:35 +08:00
},
2015-08-27 15:45:38 +08:00
onProgress(e, file) {
this.props.onProgress(e, file);
2015-08-08 23:37:35 +08:00
},
2015-08-27 15:45:38 +08:00
onError(err, responce, file) {
2015-08-27 17:35:10 +08:00
Message.error(file.name + ' 上传失败');
this.removeFile(file);
2015-08-27 15:45:38 +08:00
this.props.onError(err, responce, file);
},
onRemove(file){
this.props.onRemove(file);
2015-08-08 23:37:35 +08:00
},
getDefaultProps() {
return {
type: 'select',
name: '',
2015-08-08 23:37:35 +08:00
multipart: false,
action: '',
data: {},
accept: '',
uploadTip: '',
2015-08-27 15:46:23 +08:00
onStart: noop,
onError: noop,
onSuccess: noop,
onProgress: noop,
onRemove: noop,
2015-08-31 12:13:55 +08:00
limit: Number.MAX_VALUE,
urlResolver: function(ret) {
try {
return JSON.parse(ret).url;
} catch(e) {}
}
2015-08-08 23:37:35 +08:00
};
},
render() {
2015-08-21 16:19:28 +08:00
let type = this.props.type || 'select';
2015-08-27 15:45:38 +08:00
let props = assign({}, this.props, {
onStart: this.onStart,
onError: this.onError,
onProgress: this.onProgress,
onSuccess: this.onSuccess,
});
if (type === 'drag') {
2015-08-08 23:37:35 +08:00
return (
<div className={prefixCls + ' ' + prefixCls + '-drag'}>
<Upload {...props}>
<div className={prefixCls + '-drag-container'}>
{this.props.children}
2015-08-08 23:37:35 +08:00
</div>
</Upload>
</div>
2015-08-08 23:37:35 +08:00
);
} else if (type === 'select') {
2015-08-08 23:37:35 +08:00
return (
<div>
<div className={prefixCls + ' ' + prefixCls + '-select'}>
<Upload {...props}>
{this.props.children}
</Upload>
</div>
2015-08-31 18:13:16 +08:00
<UploadList items={this.state.fileList}
2015-08-31 12:13:55 +08:00
onRemove={this.onRemove}
limit={props.limit} />
2015-08-08 23:37:35 +08:00
</div>
);
}
}
});
AntUpload.Dragger = React.createClass({
render() {
2015-08-27 15:45:38 +08:00
return <AntUpload {...this.props} type="drag" style={{height: this.props.height}}/>;
}
});
export default AntUpload;