2016-07-07 20:25:03 +08:00
|
|
|
import * as 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';
|
2016-04-07 11:39:23 +08:00
|
|
|
import Progress from '../progress';
|
2015-12-12 15:25:54 +08:00
|
|
|
import classNames from 'classnames';
|
2016-08-01 16:35:01 +08:00
|
|
|
import { UploadListProps } from './interface';
|
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
|
2016-05-24 17:57:31 +08:00
|
|
|
const previewFile = (file, callback) => {
|
2015-12-27 15:32:37 +08:00
|
|
|
const reader = new FileReader();
|
2016-05-24 17:57:31 +08:00
|
|
|
reader.onloadend = () => callback(reader.result);
|
2015-12-27 15:32:37 +08:00
|
|
|
reader.readAsDataURL(file);
|
|
|
|
};
|
|
|
|
|
2016-08-01 16:35:01 +08:00
|
|
|
export default class UploadList extends React.Component<UploadListProps, any> {
|
2016-03-29 17:33:37 +08:00
|
|
|
static defaultProps = {
|
|
|
|
listType: 'text', // or picture
|
|
|
|
items: [],
|
|
|
|
progressAttr: {
|
|
|
|
strokeWidth: 3,
|
2016-05-11 09:32:33 +08:00
|
|
|
showInfo: false,
|
|
|
|
},
|
2016-03-29 17:33:37 +08:00
|
|
|
};
|
|
|
|
|
2016-04-05 12:30:14 +08:00
|
|
|
handleClose = (file) => {
|
2015-09-01 00:52:34 +08:00
|
|
|
this.props.onRemove(file);
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
2016-04-05 12:30:14 +08:00
|
|
|
handlePreview = (file, e) => {
|
2016-07-09 17:22:48 +08:00
|
|
|
if (this.props.onPreview) {
|
|
|
|
e.preventDefault();
|
|
|
|
return this.props.onPreview(file);
|
|
|
|
}
|
2016-04-05 12:30:14 +08:00
|
|
|
}
|
|
|
|
|
2015-12-27 16:00:40 +08:00
|
|
|
componentDidUpdate() {
|
2016-01-15 15:56:06 +08:00
|
|
|
if (this.props.listType !== 'picture' && this.props.listType !== 'picture-card') {
|
2015-12-28 13:07:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-12-27 15:32:37 +08:00
|
|
|
this.props.items.forEach(file => {
|
2015-12-27 16:00:40 +08:00
|
|
|
if (typeof document === 'undefined' ||
|
|
|
|
typeof window === 'undefined' ||
|
2016-08-01 16:35:01 +08:00
|
|
|
!(window as any).FileReader || !(window as any).File ||
|
2016-01-26 19:55:19 +08:00
|
|
|
!(file.originFileObj instanceof File) ||
|
2015-12-27 15:32:37 +08:00
|
|
|
file.thumbUrl !== undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-07 18:04:05 +08:00
|
|
|
/*eslint-disable */
|
2015-12-27 15:32:37 +08:00
|
|
|
file.thumbUrl = '';
|
2016-01-07 18:04:05 +08:00
|
|
|
/*eslint-enable */
|
2015-12-27 15:32:37 +08:00
|
|
|
previewFile(file.originFileObj, (previewDataUrl) => {
|
2016-01-07 18:04:05 +08:00
|
|
|
/*eslint-disable */
|
2015-12-27 15:32:37 +08:00
|
|
|
file.thumbUrl = previewDataUrl;
|
2016-01-07 18:04:05 +08:00
|
|
|
/*eslint-enable */
|
2015-12-27 15:32:37 +08:00
|
|
|
this.forceUpdate();
|
|
|
|
});
|
|
|
|
});
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
|
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" />;
|
|
|
|
|
2016-01-15 15:56:06 +08:00
|
|
|
if (this.props.listType === 'picture' || this.props.listType === 'picture-card') {
|
|
|
|
if (file.status === 'uploading' || (!file.thumbUrl && !file.url)) {
|
2016-01-15 15:57:41 +08:00
|
|
|
if (this.props.listType === 'picture-card') {
|
2016-02-17 18:04:42 +08:00
|
|
|
icon = <div className={`${prefixCls}-list-item-uploading-text`}>文件上传中</div>;
|
2016-01-15 15:56:06 +08:00
|
|
|
} else {
|
2016-02-17 18:04:42 +08:00
|
|
|
icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
|
2016-01-15 15:56:06 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-24 17:57:31 +08:00
|
|
|
icon = (
|
|
|
|
<a
|
|
|
|
className={`${prefixCls}-list-item-thumbnail`}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
|
|
|
href={file.url}
|
2016-08-23 21:00:35 +08:00
|
|
|
target="_blank" rel="noopener noreferrer"
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-05-24 17:57:31 +08:00
|
|
|
<img src={file.thumbUrl || file.url} alt={file.name} />
|
|
|
|
</a>
|
2016-01-07 17:46:46 +08:00
|
|
|
);
|
2016-01-15 15:56:06 +08:00
|
|
|
}
|
2015-12-12 15:25:54 +08:00
|
|
|
}
|
2016-01-15 15:56:06 +08:00
|
|
|
|
2015-10-31 13:27:58 +08:00
|
|
|
if (file.status === 'uploading') {
|
2016-01-07 14:21:29 +08:00
|
|
|
progress = (
|
2016-02-17 18:04:42 +08:00
|
|
|
<div className={`${prefixCls}-list-item-progress`}>
|
2016-04-07 11:39:23 +08:00
|
|
|
<Progress type="line" {...this.props.progressAttr} percent={file.percent} />
|
2016-01-07 14:21:29 +08:00
|
|
|
</div>
|
|
|
|
);
|
2015-10-31 13:27:58 +08:00
|
|
|
}
|
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}>
|
2016-02-17 18:04:42 +08:00
|
|
|
<div className={`${prefixCls}-list-item-info`}>
|
2015-12-12 15:25:54 +08:00
|
|
|
{icon}
|
2016-05-24 17:57:31 +08:00
|
|
|
{
|
|
|
|
file.url
|
|
|
|
? (
|
|
|
|
<a
|
2016-06-24 13:35:11 +08:00
|
|
|
href={file.url}
|
2016-08-23 21:00:35 +08:00
|
|
|
target="_blank" rel="noopener noreferrer"
|
2016-06-06 13:54:10 +08:00
|
|
|
className={`${prefixCls}-list-item-name`}
|
2016-06-24 13:35:11 +08:00
|
|
|
onClick={e => this.handlePreview(file, e)}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-05-24 17:57:31 +08:00
|
|
|
{file.name}
|
|
|
|
</a>
|
2016-06-24 13:35:11 +08:00
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
className={`${prefixCls}-list-item-name`}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
|
|
|
>
|
|
|
|
{file.name}
|
|
|
|
</span>
|
|
|
|
)
|
2016-05-24 17:57:31 +08:00
|
|
|
}
|
2016-01-15 15:56:06 +08:00
|
|
|
{
|
|
|
|
this.props.listType === 'picture-card' && file.status !== 'uploading'
|
2016-01-16 16:39:52 +08:00
|
|
|
? (
|
|
|
|
<span>
|
2016-05-24 17:57:31 +08:00
|
|
|
<a
|
|
|
|
href={file.url}
|
2016-08-23 21:00:35 +08:00
|
|
|
target="_blank" rel="noopener noreferrer"
|
2016-06-06 13:54:10 +08:00
|
|
|
style={{ pointerEvents: file.url ? '' : 'none' }}
|
2016-06-24 13:35:11 +08:00
|
|
|
onClick={e => this.handlePreview(file, e)}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-05-24 17:57:31 +08:00
|
|
|
<Icon type="eye-o" />
|
|
|
|
</a>
|
2016-03-30 17:06:19 +08:00
|
|
|
<Icon type="delete" onClick={() => this.handleClose(file)} />
|
2016-01-16 16:39:52 +08:00
|
|
|
</span>
|
2016-03-30 17:06:19 +08:00
|
|
|
) : <Icon type="cross" onClick={() => this.handleClose(file)} />
|
2016-01-15 15:56:06 +08:00
|
|
|
}
|
2015-10-31 13:27:58 +08:00
|
|
|
</div>
|
2016-04-29 12:13:27 +08:00
|
|
|
{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,
|
|
|
|
});
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
|
|
|
<div className={listClassNames}>
|
2016-09-14 10:21:26 +08:00
|
|
|
<Animate transitionName={`${prefixCls}-margin-top`} component="">
|
2016-01-07 14:21:29 +08:00
|
|
|
{list}
|
|
|
|
</Animate>
|
|
|
|
</div>
|
|
|
|
);
|
2015-08-09 18:00:20 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|