ant-design/components/upload/index.jsx

108 lines
2.6 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-21 16:19:28 +08:00
const AntUpload = React.createClass({
2015-08-08 23:37:35 +08:00
getInitialState() {
return {
downloadList: []
};
},
handleStart(file) {
2015-08-21 16:19:28 +08:00
let nextDownloadList = this.state.downloadList;
nextDownloadList.push({
2015-08-22 14:45:03 +08:00
index: fileIndex++,
uid: file.uid || '',
filename: file.name,
status: 'downloading'
});
this.setState({
downloadList: nextDownloadList
});
2015-08-08 23:37:35 +08:00
},
handleSuccess(ret, file) {
Message.success(file.name + '上传完成');
2015-08-21 16:19:28 +08:00
let targetItem = getFileItem(file, this.state.downloadList);
targetItem.status = 'done';
this.setState({
downloadList: this.state.downloadList
});
2015-08-08 23:37:35 +08:00
},
handleProgress() {
},
handleError() {
Message.error('上传失败');
},
getDefaultProps() {
return {
type: 'select',
name: '',
2015-08-08 23:37:35 +08:00
multipart: false,
action: '',
data: {},
accept: '',
uploadTip: '',
start: function() {},
error: function() {},
success: function() {},
progress: function() {}
};
},
render() {
2015-08-21 16:19:28 +08:00
let type = this.props.type || 'select';
let props = assign({}, this.props);
2015-08-08 23:37:35 +08:00
props.onStart = (file) => {
this.handleStart(file);
props.start.call(this, file);
};
props.onSuccess = (ret, file) => {
this.handleSuccess(ret, file);
props.success.call(this, ret, file);
};
props.onProgress = (step) => {
this.handleProgress(step);
props.progress.call(this, step);
};
props.onError = (err, responce, file) => {
2015-08-08 23:37:35 +08:00
this.handleError(err);
props.error.call(this, err, responce, file);
2015-08-08 23:37:35 +08:00
};
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>
<UploadList items={this.state.downloadList} />
2015-08-08 23:37:35 +08:00
</div>
);
}
}
});
AntUpload.Dragger = React.createClass({
render() {
return <AntUpload {...this.props} type="drag" style={{height: this.props.height}} />;
}
});
export default AntUpload;