ant-design/components/upload/UploadList.tsx

196 lines
6.1 KiB
TypeScript
Raw Normal View History

2016-09-21 11:54:53 +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';
import Tooltip from '../tooltip';
2016-04-07 11:39:23 +08:00
import Progress from '../progress';
2015-12-12 15:25:54 +08:00
import classNames from 'classnames';
import { UploadListProps, UploadFile } from './interface';
2015-10-02 16:52:34 +08:00
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
const previewFile = (file: File, callback: Function) => {
const reader = new FileReader();
2016-05-24 17:57:31 +08:00
reader.onloadend = () => callback(reader.result);
reader.readAsDataURL(file);
};
export default class UploadList extends React.Component<UploadListProps, any> {
static defaultProps = {
listType: 'text', // or picture
progressAttr: {
2017-03-20 16:44:29 +08:00
strokeWidth: 2,
2016-05-11 09:32:33 +08:00
showInfo: false,
},
prefixCls: 'ant-upload',
showRemoveIcon: true,
showPreviewIcon: true,
};
handleClose = (file: UploadFile) => {
const { onRemove } = this.props;
2016-10-24 16:30:38 +08:00
if (onRemove) {
onRemove(file);
}
}
handlePreview = (file: UploadFile, e) => {
2016-11-22 14:21:42 +08:00
const { onPreview } = this.props;
2016-10-24 16:30:38 +08:00
if (!onPreview) {
return;
}
2016-11-22 14:21:42 +08:00
e.preventDefault();
2016-10-24 16:30:38 +08:00
return 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;
}
2016-10-24 16:30:38 +08:00
(this.props.items || []).forEach(file => {
2015-12-27 16:00:40 +08:00
if (typeof document === 'undefined' ||
typeof window === 'undefined' ||
!(window as any).FileReader || !(window as any).File ||
!(file.originFileObj instanceof File) ||
file.thumbUrl !== undefined) {
return;
}
2016-01-07 18:04:05 +08:00
/*eslint-disable */
file.thumbUrl = '';
2016-01-07 18:04:05 +08:00
/*eslint-enable */
previewFile(file.originFileObj, (previewDataUrl) => {
2016-01-07 18:04:05 +08:00
/*eslint-disable */
file.thumbUrl = previewDataUrl;
2016-01-07 18:04:05 +08:00
/*eslint-enable */
this.forceUpdate();
});
});
}
render() {
2017-02-28 18:43:22 +08:00
const { prefixCls, items = [], listType, showPreviewIcon, showRemoveIcon, locale } = this.props;
const list = items.map(file => {
2015-10-31 13:27:58 +08:00
let progress;
let icon = <Icon type={file.status === 'uploading' ? 'loading' : 'paper-clip'} />;
2015-12-12 15:25:54 +08:00
if (listType === 'picture' || listType === 'picture-card') {
2016-01-15 15:56:06 +08:00
if (file.status === 'uploading' || (!file.thumbUrl && !file.url)) {
if (listType === 'picture-card') {
2017-02-28 18:43:22 +08:00
icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
2016-01-15 15:56:06 +08:00
} else {
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 || file.thumbUrl}
target="_blank"
rel="noopener noreferrer"
>
<img src={file.thumbUrl || file.url} alt={file.name} />
2016-05-24 17:57:31 +08:00
</a>
);
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') {
// show loading icon if upload progress listener is disabled
const loadingProgress = ('percent' in file) ? (
<Progress type="line" {...this.props.progressAttr} percent={file.percent} />
) : null;
progress = (
2017-03-20 16:44:29 +08:00
<div className={`${prefixCls}-list-item-progress`} key="progress">
{loadingProgress}
</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
});
const preview = file.url ? (
<a
href={file.url}
target="_blank"
rel="noopener noreferrer"
className={`${prefixCls}-list-item-name`}
onClick={e => this.handlePreview(file, e)}
2017-03-08 12:11:31 +08:00
title={file.name}
>
{file.name}
</a>
) : (
<span
className={`${prefixCls}-list-item-name`}
onClick={e => this.handlePreview(file, e)}
2017-03-08 12:11:31 +08:00
title={file.name}
>
{file.name}
</span>
);
const style = (file.url || file.thumbUrl) ? undefined : {
pointerEvents: 'none',
opacity: 0.5,
};
const previewIcon = showPreviewIcon ? (
<a
href={file.url || file.thumbUrl}
target="_blank"
rel="noopener noreferrer"
style={style}
onClick={e => this.handlePreview(file, e)}
2017-02-28 18:43:22 +08:00
title={locale.previewFile}
>
<Icon type="eye-o" />
</a>
) : null;
const removeIcon = showRemoveIcon ? (
2017-02-28 18:43:22 +08:00
<Icon type="delete" title={locale.removeFile} onClick={() => this.handleClose(file)} />
) : null;
const removeIconCross = showRemoveIcon ? (
2017-02-28 18:43:22 +08:00
<Icon type="cross" title={locale.removeFile} onClick={() => this.handleClose(file)} />
) : null;
const actions = (listType === 'picture-card' && file.status !== 'uploading')
? <span className={`${prefixCls}-list-item-actions`}>{previewIcon}{removeIcon}</span>
: removeIconCross;
let message;
if (file.response && typeof file.response === 'string') {
message = file.response;
} else {
message = (file.error && file.error.statusText) || locale.uploadError;
}
const iconAndPreview = (file.status === 'error')
? <Tooltip title={message}>{icon}{preview}</Tooltip>
: <span>{icon}{preview}</span>;
return (
2015-12-12 15:25:54 +08:00
<div className={infoUploadingClass} key={file.uid}>
<div className={`${prefixCls}-list-item-info`}>
{iconAndPreview}
2015-10-31 13:27:58 +08:00
</div>
{actions}
2017-03-20 16:44:29 +08:00
<Animate transitionName="fade" component="">
{progress}
</Animate>
</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-${listType}`]: true,
2015-12-12 15:25:54 +08:00
});
const animationDirection =
listType === 'picture-card' ? 'animate-inline' : 'animate';
return (
<Animate
transitionName={`${prefixCls}-${animationDirection}`}
component="div"
className={listClassNames}
>
{list}
</Animate>
);
}
}