2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-08-21 17:12:42 +08:00
|
|
|
import Animate from 'rc-animate';
|
2018-12-05 19:12:18 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { UploadListProps, UploadFile, UploadListType } from './interface';
|
2015-11-20 11:05:34 +08:00
|
|
|
import Icon from '../icon';
|
2017-02-15 20:58:40 +08:00
|
|
|
import Tooltip from '../tooltip';
|
2016-04-07 11:39:23 +08:00
|
|
|
import Progress from '../progress';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2015-10-02 16:52:34 +08:00
|
|
|
|
2018-11-04 12:52:09 +08:00
|
|
|
const imageTypes: string[] = ['image', 'webp', 'png', 'svg', 'gif', 'jpg', 'jpeg', 'bmp'];
|
2015-12-27 15:32:37 +08:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
|
2017-08-12 13:56:57 +08:00
|
|
|
const previewFile = (file: File, callback: Function) => {
|
2018-11-04 12:52:09 +08:00
|
|
|
if (file.type && !imageTypes.includes(file.type)) {
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2018-04-04 17:17:01 +08:00
|
|
|
const extname = (url: string) => {
|
|
|
|
if (!url) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const temp = url.split('/');
|
|
|
|
const filename = temp[temp.length - 1];
|
2018-06-15 13:21:01 +08:00
|
|
|
const filenameWithoutSuffix = filename.split(/#|\?/)[0];
|
2018-04-17 13:49:03 +08:00
|
|
|
return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
|
2018-04-04 17:17:01 +08:00
|
|
|
};
|
2018-08-09 20:30:02 +08:00
|
|
|
const isImageUrl = (file: UploadFile): boolean => {
|
|
|
|
if (imageTypes.includes(file.type)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const url: string = (file.thumbUrl || file.url) as string;
|
2018-04-17 13:49:03 +08:00
|
|
|
const extension = extname(url);
|
2018-06-15 13:21:01 +08:00
|
|
|
if (/^data:image\//.test(url) || /(webp|svg|png|gif|jpg|jpeg|bmp)$/i.test(extension)) {
|
2018-04-04 17:17:01 +08:00
|
|
|
return true;
|
2018-12-07 20:02:01 +08:00
|
|
|
} else if (/^data:/.test(url)) {
|
|
|
|
// other file types of base64
|
2018-04-04 17:17:01 +08:00
|
|
|
return false;
|
2018-12-07 20:02:01 +08:00
|
|
|
} else if (extension) {
|
|
|
|
// other file types which have extension
|
2018-04-04 17:17:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-03-11 22:24:09 +08:00
|
|
|
};
|
|
|
|
|
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 = {
|
2018-12-07 20:02:01 +08:00
|
|
|
listType: 'text' as UploadListType, // or picture
|
2016-03-29 17:33:37 +08:00
|
|
|
progressAttr: {
|
2017-03-20 16:44:29 +08:00
|
|
|
strokeWidth: 2,
|
2016-05-11 09:32:33 +08:00
|
|
|
showInfo: false,
|
|
|
|
},
|
2017-01-10 20:15:02 +08:00
|
|
|
showRemoveIcon: true,
|
|
|
|
showPreviewIcon: true,
|
2016-03-29 17:33:37 +08:00
|
|
|
};
|
|
|
|
|
2017-08-12 13:56:57 +08:00
|
|
|
handleClose = (file: UploadFile) => {
|
2017-09-25 18:15:36 +08:00
|
|
|
const { onRemove } = this.props;
|
2016-10-24 16:30:38 +08:00
|
|
|
if (onRemove) {
|
|
|
|
onRemove(file);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
|
2017-11-20 16:31:41 +08:00
|
|
|
handlePreview = (file: UploadFile, e: React.SyntheticEvent<HTMLElement>) => {
|
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);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
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 => {
|
2018-12-07 20:02:01 +08:00
|
|
|
if (
|
|
|
|
typeof document === 'undefined' ||
|
|
|
|
typeof window === 'undefined' ||
|
|
|
|
!(window as any).FileReader ||
|
|
|
|
!(window as any).File ||
|
|
|
|
!(file.originFileObj instanceof File) ||
|
|
|
|
file.thumbUrl !== undefined
|
|
|
|
) {
|
2015-12-27 15:32:37 +08:00
|
|
|
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 */
|
2017-11-20 16:31:41 +08:00
|
|
|
previewFile(file.originFileObj, (previewDataUrl: string) => {
|
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
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderUploadList = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
items = [],
|
|
|
|
listType,
|
|
|
|
showPreviewIcon,
|
|
|
|
showRemoveIcon,
|
|
|
|
locale,
|
2018-12-05 19:12:18 +08:00
|
|
|
} = this.props;
|
|
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
2016-09-22 11:54:36 +08:00
|
|
|
const list = items.map(file => {
|
2015-10-31 13:27:58 +08:00
|
|
|
let progress;
|
2017-03-16 13:30:09 +08:00
|
|
|
let icon = <Icon type={file.status === 'uploading' ? 'loading' : 'paper-clip'} />;
|
2015-12-12 15:25:54 +08:00
|
|
|
|
2016-09-22 11:54:36 +08:00
|
|
|
if (listType === 'picture' || listType === 'picture-card') {
|
2018-02-11 16:11:13 +08:00
|
|
|
if (listType === 'picture-card' && file.status === 'uploading') {
|
|
|
|
icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
|
|
|
|
} else if (!file.thumbUrl && !file.url) {
|
2018-12-07 20:02:01 +08:00
|
|
|
icon = (
|
|
|
|
<Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" theme="twoTone" />
|
|
|
|
);
|
2016-01-15 15:56:06 +08:00
|
|
|
} else {
|
2018-12-07 20:02:01 +08:00
|
|
|
const thumbnail = isImageUrl(file) ? (
|
|
|
|
<img src={file.thumbUrl || file.url} alt={file.name} />
|
|
|
|
) : (
|
|
|
|
<Icon type="file" className={`${prefixCls}-list-item-icon`} theme="twoTone" />
|
|
|
|
);
|
2016-05-24 17:57:31 +08:00
|
|
|
icon = (
|
|
|
|
<a
|
|
|
|
className={`${prefixCls}-list-item-thumbnail`}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
2016-11-22 14:43:20 +08:00
|
|
|
href={file.url || file.thumbUrl}
|
2016-11-23 17:53:10 +08:00
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2018-03-11 22:24:09 +08:00
|
|
|
{thumbnail}
|
2016-05-24 17:57:31 +08:00
|
|
|
</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') {
|
2017-03-10 19:18:05 +08:00
|
|
|
// show loading icon if upload progress listener is disabled
|
2018-12-07 20:02:01 +08:00
|
|
|
const loadingProgress =
|
|
|
|
'percent' in file ? (
|
|
|
|
<Progress type="line" {...this.props.progressAttr} percent={file.percent} />
|
|
|
|
) : null;
|
2017-03-10 19:18:05 +08:00
|
|
|
|
2016-01-07 14:21:29 +08:00
|
|
|
progress = (
|
2017-03-20 16:44:29 +08:00
|
|
|
<div className={`${prefixCls}-list-item-progress`} key="progress">
|
2017-03-10 19:18:05 +08:00
|
|
|
{loadingProgress}
|
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
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
const linkProps =
|
|
|
|
typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
|
2016-11-25 12:03:39 +08:00
|
|
|
const preview = file.url ? (
|
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className={`${prefixCls}-list-item-name`}
|
2017-03-08 12:11:31 +08:00
|
|
|
title={file.name}
|
2018-09-14 11:43:02 +08:00
|
|
|
{...linkProps}
|
|
|
|
href={file.url}
|
|
|
|
onClick={e => this.handlePreview(file, e)}
|
2016-11-25 12:03:39 +08:00
|
|
|
>
|
|
|
|
{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}
|
2016-11-25 12:03:39 +08:00
|
|
|
>
|
|
|
|
{file.name}
|
|
|
|
</span>
|
|
|
|
);
|
2018-04-04 14:46:59 +08:00
|
|
|
const style: React.CSSProperties = {
|
2016-11-25 12:03:39 +08:00
|
|
|
pointerEvents: 'none',
|
|
|
|
opacity: 0.5,
|
|
|
|
};
|
2017-01-10 20:15:02 +08:00
|
|
|
const previewIcon = showPreviewIcon ? (
|
|
|
|
<a
|
|
|
|
href={file.url || file.thumbUrl}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2018-12-07 20:02:01 +08:00
|
|
|
style={file.url || file.thumbUrl ? undefined : style}
|
2017-01-10 20:15:02 +08:00
|
|
|
onClick={e => this.handlePreview(file, e)}
|
2017-02-28 18:43:22 +08:00
|
|
|
title={locale.previewFile}
|
2017-01-10 20:15:02 +08:00
|
|
|
>
|
|
|
|
<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)} />
|
2017-01-10 20:15:02 +08:00
|
|
|
) : null;
|
2018-08-24 18:36:08 +08:00
|
|
|
const removeIconClose = showRemoveIcon ? (
|
|
|
|
<Icon type="close" title={locale.removeFile} onClick={() => this.handleClose(file)} />
|
2017-01-10 20:15:02 +08:00
|
|
|
) : null;
|
2018-12-07 20:02:01 +08:00
|
|
|
const actions =
|
|
|
|
listType === 'picture-card' && file.status !== 'uploading' ? (
|
|
|
|
<span className={`${prefixCls}-list-item-actions`}>
|
|
|
|
{previewIcon}
|
|
|
|
{removeIcon}
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
removeIconClose
|
|
|
|
);
|
2017-08-08 13:34:24 +08:00
|
|
|
let message;
|
2017-08-08 17:18:55 +08:00
|
|
|
if (file.response && typeof file.response === 'string') {
|
2017-08-08 13:34:24 +08:00
|
|
|
message = file.response;
|
|
|
|
} else {
|
|
|
|
message = (file.error && file.error.statusText) || locale.uploadError;
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
const iconAndPreview =
|
|
|
|
file.status === 'error' ? (
|
|
|
|
<Tooltip title={message}>
|
|
|
|
{icon}
|
|
|
|
{preview}
|
|
|
|
</Tooltip>
|
|
|
|
) : (
|
|
|
|
<span>
|
|
|
|
{icon}
|
|
|
|
{preview}
|
|
|
|
</span>
|
|
|
|
);
|
2017-03-08 18:02:15 +08:00
|
|
|
|
|
|
|
return (
|
2015-12-12 15:25:54 +08:00
|
|
|
<div className={infoUploadingClass} key={file.uid}>
|
2018-12-07 20:02:01 +08:00
|
|
|
<div className={`${prefixCls}-list-item-info`}>{iconAndPreview}</div>
|
2017-03-08 18:02:15 +08:00
|
|
|
{actions}
|
2017-03-20 16:44:29 +08:00
|
|
|
<Animate transitionName="fade" component="">
|
|
|
|
{progress}
|
|
|
|
</Animate>
|
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,
|
2016-09-22 11:54:36 +08:00
|
|
|
[`${prefixCls}-list-${listType}`]: true,
|
2015-12-12 15:25:54 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
const animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate';
|
2016-01-07 14:21:29 +08:00
|
|
|
return (
|
2016-09-22 11:54:36 +08:00
|
|
|
<Animate
|
2017-03-08 18:02:15 +08:00
|
|
|
transitionName={`${prefixCls}-${animationDirection}`}
|
2016-09-22 11:54:36 +08:00
|
|
|
component="div"
|
|
|
|
className={listClassNames}
|
|
|
|
>
|
|
|
|
{list}
|
|
|
|
</Animate>
|
2016-01-07 14:21:29 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderUploadList}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|