2015-08-09 18:00:20 +08:00
|
|
|
import React from 'react';
|
2015-08-21 17:12:42 +08:00
|
|
|
import Animate from 'rc-animate';
|
2015-11-20 11:05:34 +08:00
|
|
|
import Icon from '../icon';
|
2015-10-02 16:52:34 +08:00
|
|
|
const prefixCls = 'ant-upload';
|
2015-11-04 12:00:49 +08:00
|
|
|
import { Line } from '../progress';
|
2015-12-12 15:25:54 +08:00
|
|
|
import classNames from 'classnames';
|
2015-10-02 16:52:34 +08:00
|
|
|
|
2015-12-27 15:32:37 +08:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
|
|
|
|
const previewFile = function(file, callback) {
|
2015-12-27 15:35:13 +08:00
|
|
|
if (typeof document === 'undefined' && typeof window === 'undefined') {
|
2015-12-27 15:32:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!window.FileReader) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onloadend = function() {
|
|
|
|
callback(reader.result);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
};
|
|
|
|
|
2015-08-09 18:00:20 +08:00
|
|
|
export default React.createClass({
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
2015-12-12 15:25:54 +08:00
|
|
|
listType: 'text', // or picture
|
2015-10-31 13:27:58 +08:00
|
|
|
items: [],
|
|
|
|
progressAttr: {
|
|
|
|
strokeWidth: 3,
|
|
|
|
showInfo: false
|
|
|
|
}
|
2015-08-09 18:00:20 +08:00
|
|
|
};
|
|
|
|
},
|
2015-08-09 18:23:18 +08:00
|
|
|
handleClose(file) {
|
2015-09-01 00:52:34 +08:00
|
|
|
this.props.onRemove(file);
|
2015-08-09 18:00:20 +08:00
|
|
|
},
|
2015-12-27 15:32:37 +08:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
this.props.items.forEach(file => {
|
|
|
|
if (!file.originFileObj ||
|
|
|
|
!(file.originFileObj instanceof File) ||
|
|
|
|
file.thumbUrl !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file.thumbUrl = '';
|
|
|
|
previewFile(file.originFileObj, (previewDataUrl) => {
|
|
|
|
file.thumbUrl = previewDataUrl;
|
|
|
|
this.forceUpdate();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-08-09 18:00:20 +08:00
|
|
|
render() {
|
2015-12-27 15:32:37 +08:00
|
|
|
let list = this.props.items.map(file => {
|
2015-10-31 13:27:58 +08:00
|
|
|
let progress;
|
2015-12-12 15:25:54 +08:00
|
|
|
let icon = <Icon type="paper-clip" />;
|
|
|
|
|
|
|
|
if (this.props.listType === 'picture') {
|
2015-12-27 15:43:49 +08:00
|
|
|
icon = (file.status === 'uploading' || (!file.thumbUrl && !file.url))
|
2015-12-12 15:25:54 +08:00
|
|
|
? <Icon className={prefixCls + '-list-item-thumbnail'} type="picture" />
|
|
|
|
: <a className={prefixCls + '-list-item-thumbnail'}
|
|
|
|
href={file.url}
|
2015-12-27 15:43:49 +08:00
|
|
|
target="_blank"><img src={file.thumbUrl || file.url} alt={file.name} /></a>;
|
2015-12-12 15:25:54 +08:00
|
|
|
}
|
2015-10-31 13:27:58 +08:00
|
|
|
if (file.status === 'uploading') {
|
|
|
|
progress = <div className={prefixCls + '-list-item-progress'}>
|
|
|
|
<Line {...this.props.progressAttr} percent={file.percent} />
|
|
|
|
</div>;
|
|
|
|
}
|
2015-12-12 15:25:54 +08:00
|
|
|
const infoUploadingClass = classNames({
|
|
|
|
[`${prefixCls}-list-item`]: true,
|
2015-12-14 11:56:00 +08:00
|
|
|
[`${prefixCls}-list-item-${file.status}`]: true,
|
2015-12-12 15:25:54 +08:00
|
|
|
});
|
2015-08-09 18:00:20 +08:00
|
|
|
return (
|
2015-12-12 15:25:54 +08:00
|
|
|
<div className={infoUploadingClass} key={file.uid}>
|
2015-10-31 13:27:58 +08:00
|
|
|
<div className={prefixCls + '-list-item-info'}>
|
2015-12-12 15:25:54 +08:00
|
|
|
{icon}
|
|
|
|
<span className={prefixCls + '-list-item-name'}>{file.name}</span>
|
2015-12-04 15:06:36 +08:00
|
|
|
<Icon type="cross" onClick={this.handleClose.bind(this, file)} />
|
2015-10-31 13:27:58 +08:00
|
|
|
</div>
|
|
|
|
{ progress }
|
2015-08-09 18:00:20 +08:00
|
|
|
</div>
|
|
|
|
);
|
2015-08-31 18:13:16 +08:00
|
|
|
});
|
2015-12-12 15:25:54 +08:00
|
|
|
const listClassNames = classNames({
|
|
|
|
[`${prefixCls}-list`]: true,
|
|
|
|
[`${prefixCls}-list-${this.props.listType}`]: true,
|
|
|
|
});
|
|
|
|
return <div className={listClassNames}>
|
2015-08-21 17:37:54 +08:00
|
|
|
<Animate transitionName={prefixCls + '-margin-top'}>
|
2015-08-31 18:13:16 +08:00
|
|
|
{list}
|
2015-08-21 17:12:42 +08:00
|
|
|
</Animate>
|
2015-09-01 00:52:34 +08:00
|
|
|
</div>;
|
2015-08-09 18:00:20 +08:00
|
|
|
}
|
|
|
|
});
|