ListItem.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use client";
  2. import * as React from 'react';
  3. import DeleteOutlined from "@ant-design/icons/es/icons/DeleteOutlined";
  4. import DownloadOutlined from "@ant-design/icons/es/icons/DownloadOutlined";
  5. import EyeOutlined from "@ant-design/icons/es/icons/EyeOutlined";
  6. import classNames from 'classnames';
  7. import CSSMotion from 'rc-motion';
  8. import { ConfigContext } from '../../config-provider';
  9. import Progress from '../../progress';
  10. import Tooltip from '../../tooltip';
  11. const ListItem = /*#__PURE__*/React.forwardRef(({
  12. prefixCls,
  13. className,
  14. style,
  15. locale,
  16. listType,
  17. file,
  18. items,
  19. progress: progressProps,
  20. iconRender,
  21. actionIconRender,
  22. itemRender,
  23. isImgUrl,
  24. showPreviewIcon,
  25. showRemoveIcon,
  26. showDownloadIcon,
  27. previewIcon: customPreviewIcon,
  28. removeIcon: customRemoveIcon,
  29. downloadIcon: customDownloadIcon,
  30. extra: customExtra,
  31. onPreview,
  32. onDownload,
  33. onClose
  34. }, ref) => {
  35. var _a, _b;
  36. // Status: which will ignore `removed` status
  37. const {
  38. status
  39. } = file;
  40. const [mergedStatus, setMergedStatus] = React.useState(status);
  41. React.useEffect(() => {
  42. if (status !== 'removed') {
  43. setMergedStatus(status);
  44. }
  45. }, [status]);
  46. // Delay to show the progress bar
  47. const [showProgress, setShowProgress] = React.useState(false);
  48. React.useEffect(() => {
  49. const timer = setTimeout(() => {
  50. setShowProgress(true);
  51. }, 300);
  52. return () => {
  53. clearTimeout(timer);
  54. };
  55. }, []);
  56. const iconNode = iconRender(file);
  57. let icon = /*#__PURE__*/React.createElement("div", {
  58. className: `${prefixCls}-icon`
  59. }, iconNode);
  60. if (listType === 'picture' || listType === 'picture-card' || listType === 'picture-circle') {
  61. if (mergedStatus === 'uploading' || !file.thumbUrl && !file.url) {
  62. const uploadingClassName = classNames(`${prefixCls}-list-item-thumbnail`, {
  63. [`${prefixCls}-list-item-file`]: mergedStatus !== 'uploading'
  64. });
  65. icon = /*#__PURE__*/React.createElement("div", {
  66. className: uploadingClassName
  67. }, iconNode);
  68. } else {
  69. const thumbnail = (isImgUrl === null || isImgUrl === void 0 ? void 0 : isImgUrl(file)) ? (/*#__PURE__*/React.createElement("img", {
  70. src: file.thumbUrl || file.url,
  71. alt: file.name,
  72. className: `${prefixCls}-list-item-image`,
  73. crossOrigin: file.crossOrigin
  74. })) : iconNode;
  75. const aClassName = classNames(`${prefixCls}-list-item-thumbnail`, {
  76. [`${prefixCls}-list-item-file`]: isImgUrl && !isImgUrl(file)
  77. });
  78. icon = /*#__PURE__*/React.createElement("a", {
  79. className: aClassName,
  80. onClick: e => onPreview(file, e),
  81. href: file.url || file.thumbUrl,
  82. target: "_blank",
  83. rel: "noopener noreferrer"
  84. }, thumbnail);
  85. }
  86. }
  87. const listItemClassName = classNames(`${prefixCls}-list-item`, `${prefixCls}-list-item-${mergedStatus}`);
  88. const linkProps = typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
  89. const removeIcon = (typeof showRemoveIcon === 'function' ? showRemoveIcon(file) : showRemoveIcon) ? actionIconRender((typeof customRemoveIcon === 'function' ? customRemoveIcon(file) : customRemoveIcon) || (/*#__PURE__*/React.createElement(DeleteOutlined, null)), () => onClose(file), prefixCls, locale.removeFile,
  90. // acceptUploadDisabled is true, only remove icon will follow Upload disabled prop
  91. // https://github.com/ant-design/ant-design/issues/46171
  92. true) : null;
  93. const downloadIcon = (typeof showDownloadIcon === 'function' ? showDownloadIcon(file) : showDownloadIcon) && mergedStatus === 'done' ? actionIconRender((typeof customDownloadIcon === 'function' ? customDownloadIcon(file) : customDownloadIcon) || /*#__PURE__*/React.createElement(DownloadOutlined, null), () => onDownload(file), prefixCls, locale.downloadFile) : null;
  94. const downloadOrDelete = listType !== 'picture-card' && listType !== 'picture-circle' && (/*#__PURE__*/React.createElement("span", {
  95. key: "download-delete",
  96. className: classNames(`${prefixCls}-list-item-actions`, {
  97. picture: listType === 'picture'
  98. })
  99. }, downloadIcon, removeIcon));
  100. const extraContent = typeof customExtra === 'function' ? customExtra(file) : customExtra;
  101. const extra = extraContent && (/*#__PURE__*/React.createElement("span", {
  102. className: `${prefixCls}-list-item-extra`
  103. }, extraContent));
  104. const listItemNameClass = classNames(`${prefixCls}-list-item-name`);
  105. const fileName = file.url ? (/*#__PURE__*/React.createElement("a", Object.assign({
  106. key: "view",
  107. target: "_blank",
  108. rel: "noopener noreferrer",
  109. className: listItemNameClass,
  110. title: file.name
  111. }, linkProps, {
  112. href: file.url,
  113. onClick: e => onPreview(file, e)
  114. }), file.name, extra)) : (/*#__PURE__*/React.createElement("span", {
  115. key: "view",
  116. className: listItemNameClass,
  117. onClick: e => onPreview(file, e),
  118. title: file.name
  119. }, file.name, extra));
  120. const previewIcon = (typeof showPreviewIcon === 'function' ? showPreviewIcon(file) : showPreviewIcon) && (file.url || file.thumbUrl) ? (/*#__PURE__*/React.createElement("a", {
  121. href: file.url || file.thumbUrl,
  122. target: "_blank",
  123. rel: "noopener noreferrer",
  124. onClick: e => onPreview(file, e),
  125. title: locale.previewFile
  126. }, typeof customPreviewIcon === 'function' ? customPreviewIcon(file) : customPreviewIcon || /*#__PURE__*/React.createElement(EyeOutlined, null))) : null;
  127. const pictureCardActions = (listType === 'picture-card' || listType === 'picture-circle') && mergedStatus !== 'uploading' && (/*#__PURE__*/React.createElement("span", {
  128. className: `${prefixCls}-list-item-actions`
  129. }, previewIcon, mergedStatus === 'done' && downloadIcon, removeIcon));
  130. const {
  131. getPrefixCls
  132. } = React.useContext(ConfigContext);
  133. const rootPrefixCls = getPrefixCls();
  134. const dom = /*#__PURE__*/React.createElement("div", {
  135. className: listItemClassName
  136. }, icon, fileName, downloadOrDelete, pictureCardActions, showProgress && (/*#__PURE__*/React.createElement(CSSMotion, {
  137. motionName: `${rootPrefixCls}-fade`,
  138. visible: mergedStatus === 'uploading',
  139. motionDeadline: 2000
  140. }, ({
  141. className: motionClassName
  142. }) => {
  143. // show loading icon if upload progress listener is disabled
  144. const loadingProgress = 'percent' in file ? (/*#__PURE__*/React.createElement(Progress, Object.assign({
  145. type: "line",
  146. percent: file.percent,
  147. "aria-label": file['aria-label'],
  148. "aria-labelledby": file['aria-labelledby']
  149. }, progressProps))) : null;
  150. return /*#__PURE__*/React.createElement("div", {
  151. className: classNames(`${prefixCls}-list-item-progress`, motionClassName)
  152. }, loadingProgress);
  153. })));
  154. const message = file.response && typeof file.response === 'string' ? file.response : ((_a = file.error) === null || _a === void 0 ? void 0 : _a.statusText) || ((_b = file.error) === null || _b === void 0 ? void 0 : _b.message) || locale.uploadError;
  155. const item = mergedStatus === 'error' ? (/*#__PURE__*/React.createElement(Tooltip, {
  156. title: message,
  157. getPopupContainer: node => node.parentNode
  158. }, dom)) : dom;
  159. return /*#__PURE__*/React.createElement("div", {
  160. className: classNames(`${prefixCls}-list-item-container`, className),
  161. style: style,
  162. ref: ref
  163. }, itemRender ? itemRender(item, file, items, {
  164. download: onDownload.bind(null, file),
  165. preview: onPreview.bind(null, file),
  166. remove: onClose.bind(null, file)
  167. }) : item);
  168. });
  169. export default ListItem;