2020-11-21 19:00:11 +08:00
|
|
|
import DeleteOutlined from '@ant-design/icons/DeleteOutlined';
|
|
|
|
import DownloadOutlined from '@ant-design/icons/DownloadOutlined';
|
2022-06-22 14:57:09 +08:00
|
|
|
import EyeOutlined from '@ant-design/icons/EyeOutlined';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import CSSMotion from 'rc-motion';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { ConfigContext } from '../../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Progress from '../../progress';
|
|
|
|
import Tooltip from '../../tooltip';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2020-11-21 19:00:11 +08:00
|
|
|
ItemRender,
|
|
|
|
UploadFile,
|
|
|
|
UploadListProgressProps,
|
|
|
|
UploadListType,
|
|
|
|
UploadLocale,
|
|
|
|
} from '../interface';
|
|
|
|
|
|
|
|
export interface ListItemProps {
|
|
|
|
prefixCls: string;
|
|
|
|
className?: string;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
locale: UploadLocale;
|
|
|
|
file: UploadFile;
|
|
|
|
items: UploadFile[];
|
|
|
|
listType?: UploadListType;
|
|
|
|
isImgUrl?: (file: UploadFile) => boolean;
|
|
|
|
showRemoveIcon?: boolean;
|
|
|
|
showDownloadIcon?: boolean;
|
|
|
|
showPreviewIcon?: boolean;
|
|
|
|
removeIcon?: React.ReactNode | ((file: UploadFile) => React.ReactNode);
|
|
|
|
downloadIcon?: React.ReactNode | ((file: UploadFile) => React.ReactNode);
|
2021-09-08 09:30:09 +08:00
|
|
|
previewIcon?: React.ReactNode | ((file: UploadFile) => React.ReactNode);
|
2020-11-21 19:00:11 +08:00
|
|
|
iconRender: (file: UploadFile) => React.ReactNode;
|
|
|
|
actionIconRender: (
|
|
|
|
customIcon: React.ReactNode,
|
|
|
|
callback: () => void,
|
|
|
|
prefixCls: string,
|
2023-01-16 09:55:52 +08:00
|
|
|
title?: string,
|
2023-12-15 09:57:13 +08:00
|
|
|
acceptUploadDisabled?: boolean,
|
2020-11-21 19:00:11 +08:00
|
|
|
) => React.ReactNode;
|
|
|
|
itemRender?: ItemRender;
|
|
|
|
onPreview: (file: UploadFile, e: React.SyntheticEvent<HTMLElement>) => void;
|
|
|
|
onClose: (file: UploadFile) => void;
|
|
|
|
onDownload: (file: UploadFile) => void;
|
|
|
|
progress?: UploadListProgressProps;
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:18:51 +08:00
|
|
|
const ListItem = React.forwardRef<HTMLDivElement, ListItemProps>(
|
2020-11-21 19:00:11 +08:00
|
|
|
(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
locale,
|
|
|
|
listType,
|
|
|
|
file,
|
|
|
|
items,
|
|
|
|
progress: progressProps,
|
|
|
|
iconRender,
|
|
|
|
actionIconRender,
|
|
|
|
itemRender,
|
|
|
|
isImgUrl,
|
|
|
|
showPreviewIcon,
|
|
|
|
showRemoveIcon,
|
|
|
|
showDownloadIcon,
|
2021-09-08 09:30:09 +08:00
|
|
|
previewIcon: customPreviewIcon,
|
2020-11-21 19:00:11 +08:00
|
|
|
removeIcon: customRemoveIcon,
|
|
|
|
downloadIcon: customDownloadIcon,
|
|
|
|
onPreview,
|
|
|
|
onDownload,
|
|
|
|
onClose,
|
2023-07-18 12:18:51 +08:00
|
|
|
},
|
|
|
|
ref,
|
2020-11-21 19:00:11 +08:00
|
|
|
) => {
|
2022-07-26 15:17:41 +08:00
|
|
|
// Status: which will ignore `removed` status
|
|
|
|
const { status } = file;
|
|
|
|
const [mergedStatus, setMergedStatus] = React.useState(status);
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (status !== 'removed') {
|
|
|
|
setMergedStatus(status);
|
|
|
|
}
|
|
|
|
}, [status]);
|
|
|
|
|
2020-11-21 19:00:11 +08:00
|
|
|
// Delay to show the progress bar
|
|
|
|
const [showProgress, setShowProgress] = React.useState(false);
|
|
|
|
React.useEffect(() => {
|
2023-06-19 14:36:24 +08:00
|
|
|
const timer = setTimeout(() => {
|
2020-11-21 19:00:11 +08:00
|
|
|
setShowProgress(true);
|
|
|
|
}, 300);
|
|
|
|
return () => {
|
2023-06-19 14:36:24 +08:00
|
|
|
clearTimeout(timer);
|
2020-11-21 19:00:11 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const iconNode = iconRender(file);
|
2022-04-25 18:46:53 +08:00
|
|
|
let icon = <div className={`${prefixCls}-icon`}>{iconNode}</div>;
|
2023-01-31 14:31:34 +08:00
|
|
|
if (listType === 'picture' || listType === 'picture-card' || listType === 'picture-circle') {
|
2022-07-26 15:17:41 +08:00
|
|
|
if (mergedStatus === 'uploading' || (!file.thumbUrl && !file.url)) {
|
2023-07-22 19:07:57 +08:00
|
|
|
const uploadingClassName = classNames(`${prefixCls}-list-item-thumbnail`, {
|
2022-07-26 15:17:41 +08:00
|
|
|
[`${prefixCls}-list-item-file`]: mergedStatus !== 'uploading',
|
2020-11-21 19:00:11 +08:00
|
|
|
});
|
|
|
|
icon = <div className={uploadingClassName}>{iconNode}</div>;
|
|
|
|
} else {
|
|
|
|
const thumbnail = isImgUrl?.(file) ? (
|
|
|
|
<img
|
|
|
|
src={file.thumbUrl || file.url}
|
|
|
|
alt={file.name}
|
|
|
|
className={`${prefixCls}-list-item-image`}
|
2022-04-12 12:03:46 +08:00
|
|
|
crossOrigin={file.crossOrigin}
|
2020-11-21 19:00:11 +08:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
iconNode
|
|
|
|
);
|
2023-07-18 12:18:51 +08:00
|
|
|
const aClassName = classNames(`${prefixCls}-list-item-thumbnail`, {
|
2020-11-21 19:00:11 +08:00
|
|
|
[`${prefixCls}-list-item-file`]: isImgUrl && !isImgUrl(file),
|
|
|
|
});
|
|
|
|
icon = (
|
|
|
|
<a
|
|
|
|
className={aClassName}
|
2022-11-19 13:47:33 +08:00
|
|
|
onClick={(e) => onPreview(file, e)}
|
2020-11-21 19:00:11 +08:00
|
|
|
href={file.url || file.thumbUrl}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{thumbnail}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:46:53 +08:00
|
|
|
const listItemClassName = classNames(
|
|
|
|
`${prefixCls}-list-item`,
|
2022-07-29 15:56:11 +08:00
|
|
|
`${prefixCls}-list-item-${mergedStatus}`,
|
2022-04-25 18:46:53 +08:00
|
|
|
);
|
2020-11-21 19:00:11 +08:00
|
|
|
const linkProps =
|
|
|
|
typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
|
|
|
|
|
|
|
|
const removeIcon = showRemoveIcon
|
|
|
|
? actionIconRender(
|
|
|
|
(typeof customRemoveIcon === 'function' ? customRemoveIcon(file) : customRemoveIcon) || (
|
|
|
|
<DeleteOutlined />
|
|
|
|
),
|
|
|
|
() => onClose(file),
|
|
|
|
prefixCls,
|
|
|
|
locale.removeFile,
|
2023-12-15 09:57:13 +08:00
|
|
|
// acceptUploadDisabled is true, only remove icon will follow Upload disabled prop
|
|
|
|
// https://github.com/ant-design/ant-design/issues/46171
|
|
|
|
true,
|
2020-11-21 19:00:11 +08:00
|
|
|
)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const downloadIcon =
|
2022-07-26 15:17:41 +08:00
|
|
|
showDownloadIcon && mergedStatus === 'done'
|
2020-11-21 19:00:11 +08:00
|
|
|
? actionIconRender(
|
|
|
|
(typeof customDownloadIcon === 'function'
|
|
|
|
? customDownloadIcon(file)
|
|
|
|
: customDownloadIcon) || <DownloadOutlined />,
|
|
|
|
() => onDownload(file),
|
|
|
|
prefixCls,
|
|
|
|
locale.downloadFile,
|
|
|
|
)
|
|
|
|
: null;
|
2023-01-31 14:31:34 +08:00
|
|
|
const downloadOrDelete = listType !== 'picture-card' && listType !== 'picture-circle' && (
|
2020-11-21 19:00:11 +08:00
|
|
|
<span
|
|
|
|
key="download-delete"
|
2022-04-25 18:46:53 +08:00
|
|
|
className={classNames(`${prefixCls}-list-item-actions`, {
|
2020-11-21 19:00:11 +08:00
|
|
|
picture: listType === 'picture',
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{downloadIcon}
|
|
|
|
{removeIcon}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
const listItemNameClass = classNames(`${prefixCls}-list-item-name`);
|
2022-04-25 18:46:53 +08:00
|
|
|
const fileName = file.url
|
2020-11-21 19:00:11 +08:00
|
|
|
? [
|
|
|
|
<a
|
|
|
|
key="view"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className={listItemNameClass}
|
|
|
|
title={file.name}
|
|
|
|
{...linkProps}
|
|
|
|
href={file.url}
|
2022-11-19 13:47:33 +08:00
|
|
|
onClick={(e) => onPreview(file, e)}
|
2020-11-21 19:00:11 +08:00
|
|
|
>
|
|
|
|
{file.name}
|
|
|
|
</a>,
|
|
|
|
downloadOrDelete,
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
<span
|
|
|
|
key="view"
|
|
|
|
className={listItemNameClass}
|
2022-11-19 13:47:33 +08:00
|
|
|
onClick={(e) => onPreview(file, e)}
|
2020-11-21 19:00:11 +08:00
|
|
|
title={file.name}
|
|
|
|
>
|
|
|
|
{file.name}
|
|
|
|
</span>,
|
|
|
|
downloadOrDelete,
|
|
|
|
];
|
2022-04-25 18:46:53 +08:00
|
|
|
|
2023-12-15 09:57:13 +08:00
|
|
|
const previewIcon = (showPreviewIcon && (file.url || file.thumbUrl)) ? (
|
2020-11-21 19:00:11 +08:00
|
|
|
<a
|
|
|
|
href={file.url || file.thumbUrl}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2022-11-19 13:47:33 +08:00
|
|
|
onClick={(e) => onPreview(file, e)}
|
2020-11-21 19:00:11 +08:00
|
|
|
title={locale.previewFile}
|
|
|
|
>
|
2021-09-08 09:30:09 +08:00
|
|
|
{typeof customPreviewIcon === 'function'
|
|
|
|
? customPreviewIcon(file)
|
|
|
|
: customPreviewIcon || <EyeOutlined />}
|
2020-11-21 19:00:11 +08:00
|
|
|
</a>
|
|
|
|
) : null;
|
|
|
|
|
2023-01-31 14:31:34 +08:00
|
|
|
const pictureCardActions = (listType === 'picture-card' || listType === 'picture-circle') &&
|
|
|
|
mergedStatus !== 'uploading' && (
|
|
|
|
<span className={`${prefixCls}-list-item-actions`}>
|
|
|
|
{previewIcon}
|
|
|
|
{mergedStatus === 'done' && downloadIcon}
|
|
|
|
{removeIcon}
|
|
|
|
</span>
|
|
|
|
);
|
2020-11-21 19:00:11 +08:00
|
|
|
|
2021-02-08 17:09:13 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-11-21 19:00:11 +08:00
|
|
|
|
|
|
|
const dom = (
|
2022-04-25 18:46:53 +08:00
|
|
|
<div className={listItemClassName}>
|
|
|
|
{icon}
|
|
|
|
{fileName}
|
|
|
|
{pictureCardActions}
|
2020-11-21 19:00:11 +08:00
|
|
|
{showProgress && (
|
2021-03-09 23:14:52 +08:00
|
|
|
<CSSMotion
|
|
|
|
motionName={`${rootPrefixCls}-fade`}
|
2022-07-26 15:17:41 +08:00
|
|
|
visible={mergedStatus === 'uploading'}
|
2021-03-09 23:14:52 +08:00
|
|
|
motionDeadline={2000}
|
|
|
|
>
|
2020-11-21 19:00:11 +08:00
|
|
|
{({ className: motionClassName }) => {
|
|
|
|
// show loading icon if upload progress listener is disabled
|
|
|
|
const loadingProgress =
|
|
|
|
'percent' in file ? (
|
2023-05-31 20:36:29 +08:00
|
|
|
<Progress
|
|
|
|
{...progressProps}
|
|
|
|
type="line"
|
|
|
|
percent={file.percent}
|
|
|
|
aria-label={file['aria-label']}
|
|
|
|
aria-labelledby={file['aria-labelledby']}
|
|
|
|
/>
|
2020-11-21 19:00:11 +08:00
|
|
|
) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classNames(`${prefixCls}-list-item-progress`, motionClassName)}>
|
|
|
|
{loadingProgress}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</CSSMotion>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
2022-04-25 18:46:53 +08:00
|
|
|
|
|
|
|
const message =
|
|
|
|
file.response && typeof file.response === 'string'
|
|
|
|
? file.response
|
|
|
|
: file.error?.statusText || file.error?.message || locale.uploadError;
|
2020-11-21 19:00:11 +08:00
|
|
|
const item =
|
2022-07-26 15:17:41 +08:00
|
|
|
mergedStatus === 'error' ? (
|
2022-11-19 13:47:33 +08:00
|
|
|
<Tooltip title={message} getPopupContainer={(node) => node.parentNode as HTMLElement}>
|
2020-11-21 19:00:11 +08:00
|
|
|
{dom}
|
|
|
|
</Tooltip>
|
|
|
|
) : (
|
|
|
|
dom
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-04-25 18:46:53 +08:00
|
|
|
<div
|
|
|
|
className={classNames(`${prefixCls}-list-item-container`, className)}
|
|
|
|
style={style}
|
|
|
|
ref={ref}
|
|
|
|
>
|
2021-05-06 14:12:26 +08:00
|
|
|
{itemRender
|
|
|
|
? itemRender(item, file, items, {
|
|
|
|
download: onDownload.bind(null, file),
|
|
|
|
preview: onPreview.bind(null, file),
|
|
|
|
remove: onClose.bind(null, file),
|
|
|
|
})
|
|
|
|
: item}
|
2020-11-21 19:00:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export default ListItem;
|