2015-08-08 23:37:35 +08:00
|
|
|
|
import React from 'react';
|
|
|
|
|
import Upload from 'rc-upload';
|
|
|
|
|
import assign from 'object-assign';
|
2015-08-09 18:00:20 +08:00
|
|
|
|
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-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-09-01 00:52:34 +08:00
|
|
|
|
fileList: this.props.fileList || this.props.defaultFileList || []
|
2015-08-08 23:37:35 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
2015-08-27 15:45:38 +08:00
|
|
|
|
onStart(file) {
|
2015-09-09 14:58:17 +08:00
|
|
|
|
let nextFileList = this.state.fileList.concat();
|
|
|
|
|
if (file.length > 0) {
|
|
|
|
|
file.forEach(function(f) {
|
|
|
|
|
f.status = 'uploading';
|
|
|
|
|
});
|
|
|
|
|
nextFileList = nextFileList.concat(file);
|
|
|
|
|
} else {
|
|
|
|
|
file.status = 'uploading';
|
|
|
|
|
nextFileList.push(file);
|
|
|
|
|
}
|
2015-09-01 11:26:33 +08:00
|
|
|
|
this.onChange({
|
2015-08-31 21:26:34 +08:00
|
|
|
|
file: file,
|
2015-09-09 14:58:17 +08:00
|
|
|
|
fileList: nextFileList
|
2015-08-31 21:26:34 +08:00
|
|
|
|
});
|
2015-08-08 23:37:35 +08:00
|
|
|
|
},
|
2015-09-01 00:52:34 +08:00
|
|
|
|
removeFile(file) {
|
|
|
|
|
let fileList = this.state.fileList.concat();
|
2015-08-31 18:13:16 +08:00
|
|
|
|
let targetItem = getFileItem(file, fileList);
|
2015-09-01 00:52:34 +08:00
|
|
|
|
let 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-09-01 11:26:33 +08:00
|
|
|
|
return fileList;
|
2015-08-27 17:35:10 +08:00
|
|
|
|
}
|
2015-09-01 11:26:33 +08:00
|
|
|
|
return null;
|
2015-08-27 17:35:10 +08:00
|
|
|
|
},
|
2015-09-01 00:52:34 +08:00
|
|
|
|
onSuccess(response, file) {
|
|
|
|
|
let fileList = this.state.fileList.concat();
|
2015-08-31 21:26:34 +08:00
|
|
|
|
let targetItem = getFileItem(file, fileList);
|
2015-09-01 11:26:33 +08:00
|
|
|
|
// 之前已经删除
|
|
|
|
|
if (targetItem) {
|
|
|
|
|
targetItem.status = 'done';
|
|
|
|
|
targetItem.response = response;
|
|
|
|
|
this.onChange({
|
|
|
|
|
file: targetItem,
|
|
|
|
|
fileList: this.state.fileList
|
|
|
|
|
});
|
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) {
|
2015-09-01 11:26:33 +08:00
|
|
|
|
let fileList = this.state.fileList;
|
|
|
|
|
let targetItem = getFileItem(file, fileList);
|
|
|
|
|
if (targetItem) {
|
|
|
|
|
this.onChange({
|
|
|
|
|
event: e,
|
|
|
|
|
file: file,
|
|
|
|
|
fileList: this.state.fileList
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-08-08 23:37:35 +08:00
|
|
|
|
},
|
2015-09-01 00:52:34 +08:00
|
|
|
|
onError(error, response, file) {
|
|
|
|
|
file.error = error;
|
2015-08-31 21:26:34 +08:00
|
|
|
|
file.response = response;
|
2015-09-14 00:28:39 +08:00
|
|
|
|
file.status = 'error';
|
2015-09-01 00:52:34 +08:00
|
|
|
|
this.handleRemove(file);
|
2015-08-27 15:45:38 +08:00
|
|
|
|
},
|
2015-09-01 00:52:34 +08:00
|
|
|
|
handleRemove(file) {
|
|
|
|
|
let fileList = this.removeFile(file);
|
2015-09-01 11:26:33 +08:00
|
|
|
|
if (fileList) {
|
|
|
|
|
this.onChange({
|
|
|
|
|
file: file,
|
|
|
|
|
fileList: fileList
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-08-08 23:37:35 +08:00
|
|
|
|
},
|
2015-09-14 00:28:39 +08:00
|
|
|
|
handleManualRemove(file) {
|
|
|
|
|
file.status = 'removed';
|
|
|
|
|
this.handleRemove(file);
|
|
|
|
|
},
|
2015-09-01 00:52:34 +08:00
|
|
|
|
onChange(info) {
|
|
|
|
|
// 1. 有设置外部属性时不改变 fileList
|
|
|
|
|
// 2. 上传中状态(info.event)不改变 fileList
|
|
|
|
|
if (!('fileList' in this.props) && !info.event) {
|
2015-09-09 14:58:17 +08:00
|
|
|
|
this.setState({
|
|
|
|
|
fileList: info.fileList
|
|
|
|
|
});
|
2015-09-01 00:52:34 +08:00
|
|
|
|
}
|
2015-09-09 14:58:17 +08:00
|
|
|
|
this.props.onChange(info);
|
2015-09-01 00:52:34 +08:00
|
|
|
|
},
|
2015-08-08 23:37:35 +08:00
|
|
|
|
getDefaultProps() {
|
|
|
|
|
return {
|
2015-08-09 18:00:20 +08:00
|
|
|
|
type: 'select',
|
|
|
|
|
name: '',
|
2015-08-08 23:37:35 +08:00
|
|
|
|
multipart: false,
|
|
|
|
|
action: '',
|
|
|
|
|
data: {},
|
|
|
|
|
accept: '',
|
2015-08-31 21:26:34 +08:00
|
|
|
|
onChange: noop,
|
2015-08-08 23:37:35 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
2015-09-01 00:52:34 +08:00
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if ('fileList' in nextProps) {
|
|
|
|
|
this.setState({
|
|
|
|
|
fileList: nextProps.fileList
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
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,
|
|
|
|
|
});
|
2015-08-09 18:00:20 +08:00
|
|
|
|
if (type === 'drag') {
|
2015-08-08 23:37:35 +08:00
|
|
|
|
return (
|
2015-08-21 14:46:12 +08:00
|
|
|
|
<div className={prefixCls + ' ' + prefixCls + '-drag'}>
|
|
|
|
|
<Upload {...props}>
|
2015-08-09 18:00:20 +08:00
|
|
|
|
<div className={prefixCls + '-drag-container'}>
|
|
|
|
|
{this.props.children}
|
2015-08-08 23:37:35 +08:00
|
|
|
|
</div>
|
2015-08-21 14:46:12 +08:00
|
|
|
|
</Upload>
|
|
|
|
|
</div>
|
2015-08-08 23:37:35 +08:00
|
|
|
|
);
|
2015-08-09 18:00:20 +08:00
|
|
|
|
} else if (type === 'select') {
|
2015-08-08 23:37:35 +08:00
|
|
|
|
return (
|
2015-08-21 14:46:12 +08:00
|
|
|
|
<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-09-14 00:28:39 +08:00
|
|
|
|
onRemove={this.handleManualRemove} />
|
2015-08-08 23:37:35 +08:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-08-09 18:00:20 +08:00
|
|
|
|
|
|
|
|
|
AntUpload.Dragger = React.createClass({
|
|
|
|
|
render() {
|
2015-08-27 15:45:38 +08:00
|
|
|
|
return <AntUpload {...this.props} type="drag" style={{height: this.props.height}}/>;
|
2015-08-09 18:00:20 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default AntUpload;
|