mirror of
https://github.com/ant-design/ant-design.git
synced 2025-07-31 20:36:35 +08:00

* feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
239 lines
6.9 KiB
TypeScript
239 lines
6.9 KiB
TypeScript
import * as React from 'react';
|
|
import Animate from 'rc-animate';
|
|
import classNames from 'classnames';
|
|
import {
|
|
Loading,
|
|
PaperClip,
|
|
PictureTwoTone,
|
|
FileTwoTone,
|
|
Eye,
|
|
Delete,
|
|
Close,
|
|
} from '@ant-design/icons';
|
|
|
|
import { UploadListProps, UploadFile, UploadListType } from './interface';
|
|
import { previewImage, isImageUrl } from './utils';
|
|
import Tooltip from '../tooltip';
|
|
import Progress from '../progress';
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
export default class UploadList extends React.Component<UploadListProps, any> {
|
|
static defaultProps = {
|
|
listType: 'text' as UploadListType, // or picture
|
|
progressAttr: {
|
|
strokeWidth: 2,
|
|
showInfo: false,
|
|
},
|
|
showRemoveIcon: true,
|
|
showPreviewIcon: true,
|
|
previewFile: previewImage,
|
|
};
|
|
|
|
componentDidUpdate() {
|
|
const { listType, items, previewFile } = this.props;
|
|
if (listType !== 'picture' && listType !== 'picture-card') {
|
|
return;
|
|
}
|
|
(items || []).forEach(file => {
|
|
if (
|
|
typeof document === 'undefined' ||
|
|
typeof window === 'undefined' ||
|
|
!(window as any).FileReader ||
|
|
!(window as any).File ||
|
|
!(file.originFileObj instanceof File || file.originFileObj instanceof Blob) ||
|
|
file.thumbUrl !== undefined
|
|
) {
|
|
return;
|
|
}
|
|
file.thumbUrl = '';
|
|
if (previewFile) {
|
|
previewFile(file.originFileObj as File).then((previewDataUrl: string) => {
|
|
// Need append '' to avoid dead loop
|
|
file.thumbUrl = previewDataUrl || '';
|
|
this.forceUpdate();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
handlePreview = (file: UploadFile, e: React.SyntheticEvent<HTMLElement>) => {
|
|
const { onPreview } = this.props;
|
|
if (!onPreview) {
|
|
return;
|
|
}
|
|
e.preventDefault();
|
|
return onPreview(file);
|
|
};
|
|
|
|
handleClose = (file: UploadFile) => {
|
|
const { onRemove } = this.props;
|
|
if (onRemove) {
|
|
onRemove(file);
|
|
}
|
|
};
|
|
|
|
renderUploadList = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
const {
|
|
prefixCls: customizePrefixCls,
|
|
items = [],
|
|
listType,
|
|
showPreviewIcon,
|
|
showRemoveIcon,
|
|
locale,
|
|
progressAttr,
|
|
} = this.props;
|
|
const prefixCls = getPrefixCls('upload', customizePrefixCls);
|
|
const list = items.map(file => {
|
|
let progress;
|
|
let icon = file.status === 'uploading' ? <Loading /> : <PaperClip />;
|
|
|
|
if (listType === 'picture' || listType === 'picture-card') {
|
|
if (listType === 'picture-card' && file.status === 'uploading') {
|
|
icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
|
|
} else if (!file.thumbUrl && !file.url) {
|
|
icon = <PictureTwoTone className={`${prefixCls}-list-item-thumbnail`} />;
|
|
} else {
|
|
const thumbnail = isImageUrl(file) ? (
|
|
<img
|
|
src={file.thumbUrl || file.url}
|
|
alt={file.name}
|
|
className={`${prefixCls}-list-item-image`}
|
|
/>
|
|
) : (
|
|
<FileTwoTone className={`${prefixCls}-list-item-icon`} />
|
|
);
|
|
icon = (
|
|
<a
|
|
className={`${prefixCls}-list-item-thumbnail`}
|
|
onClick={e => this.handlePreview(file, e)}
|
|
href={file.url || file.thumbUrl}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
{thumbnail}
|
|
</a>
|
|
);
|
|
}
|
|
}
|
|
|
|
if (file.status === 'uploading') {
|
|
// show loading icon if upload progress listener is disabled
|
|
const loadingProgress =
|
|
'percent' in file ? (
|
|
<Progress type="line" {...progressAttr} percent={file.percent} />
|
|
) : null;
|
|
|
|
progress = (
|
|
<div className={`${prefixCls}-list-item-progress`} key="progress">
|
|
{loadingProgress}
|
|
</div>
|
|
);
|
|
}
|
|
const infoUploadingClass = classNames({
|
|
[`${prefixCls}-list-item`]: true,
|
|
[`${prefixCls}-list-item-${file.status}`]: true,
|
|
});
|
|
const linkProps =
|
|
typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
|
|
const preview = file.url ? (
|
|
<a
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className={`${prefixCls}-list-item-name`}
|
|
title={file.name}
|
|
{...linkProps}
|
|
href={file.url}
|
|
onClick={e => this.handlePreview(file, e)}
|
|
>
|
|
{file.name}
|
|
</a>
|
|
) : (
|
|
<span
|
|
className={`${prefixCls}-list-item-name`}
|
|
onClick={e => this.handlePreview(file, e)}
|
|
title={file.name}
|
|
>
|
|
{file.name}
|
|
</span>
|
|
);
|
|
const style: React.CSSProperties = {
|
|
pointerEvents: 'none',
|
|
opacity: 0.5,
|
|
};
|
|
const previewIcon = showPreviewIcon ? (
|
|
<a
|
|
href={file.url || file.thumbUrl}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
style={file.url || file.thumbUrl ? undefined : style}
|
|
onClick={e => this.handlePreview(file, e)}
|
|
title={locale.previewFile}
|
|
>
|
|
<Eye />
|
|
</a>
|
|
) : null;
|
|
const removeIcon = showRemoveIcon ? (
|
|
<Delete title={locale.removeFile} onClick={() => this.handleClose(file)} />
|
|
) : null;
|
|
const removeIconClose = showRemoveIcon ? (
|
|
<Close 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>
|
|
) : (
|
|
removeIconClose
|
|
);
|
|
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 (
|
|
<div className={infoUploadingClass} key={file.uid}>
|
|
<div className={`${prefixCls}-list-item-info`}>{iconAndPreview}</div>
|
|
{actions}
|
|
<Animate transitionName="fade" component="">
|
|
{progress}
|
|
</Animate>
|
|
</div>
|
|
);
|
|
});
|
|
const listClassNames = classNames({
|
|
[`${prefixCls}-list`]: true,
|
|
[`${prefixCls}-list-${listType}`]: true,
|
|
});
|
|
const animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate';
|
|
return (
|
|
<Animate
|
|
transitionName={`${prefixCls}-${animationDirection}`}
|
|
component="div"
|
|
className={listClassNames}
|
|
>
|
|
{list}
|
|
</Animate>
|
|
);
|
|
};
|
|
|
|
render() {
|
|
return <ConfigConsumer>{this.renderUploadList}</ConfigConsumer>;
|
|
}
|
|
}
|