forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListItem.tsx
305 lines (294 loc) · 9.64 KB
/
ListItem.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import {
computed,
defineComponent,
onBeforeUnmount,
onMounted,
shallowRef,
watch,
Transition,
} from 'vue';
import type { ExtractPropTypes, CSSProperties } from 'vue';
import EyeOutlined from '@ant-design/icons-vue/EyeOutlined';
import DeleteOutlined from '@ant-design/icons-vue/DeleteOutlined';
import DownloadOutlined from '@ant-design/icons-vue/DownloadOutlined';
import Tooltip from '../../tooltip';
import Progress from '../../progress';
import type {
ItemRender,
UploadFile,
UploadListProgressProps,
UploadListType,
UploadLocale,
} from '../interface';
import type { VueNode } from '../../_util/type';
import useConfigInject from '../../config-provider/hooks/useConfigInject';
import { getTransitionProps } from '../../_util/transition';
import { booleanType, stringType, functionType, arrayType, objectType } from '../../_util/type';
export const listItemProps = () => {
return {
prefixCls: String,
locale: objectType<UploadLocale>(undefined as UploadLocale),
file: objectType<UploadFile>(),
items: arrayType<UploadFile[]>(),
listType: stringType<UploadListType>(),
isImgUrl: functionType<(file: UploadFile) => boolean>(),
showRemoveIcon: booleanType(),
showDownloadIcon: booleanType(),
showPreviewIcon: booleanType(),
removeIcon: functionType<(opt: { file: UploadFile }) => VueNode>(),
downloadIcon: functionType<(opt: { file: UploadFile }) => VueNode>(),
previewIcon: functionType<(opt: { file: UploadFile }) => VueNode>(),
iconRender: functionType<(opt: { file: UploadFile }) => VueNode>(),
actionIconRender:
functionType<
(opt: {
customIcon: VueNode;
callback: () => void;
prefixCls: string;
title?: string | undefined;
}) => VueNode
>(),
itemRender: functionType<ItemRender>(),
onPreview: functionType<(file: UploadFile, e: Event) => void>(),
onClose: functionType<(file: UploadFile) => void>(),
onDownload: functionType<(file: UploadFile) => void>(),
progress: objectType<UploadListProgressProps>(),
};
};
export type ListItemProps = Partial<ExtractPropTypes<ReturnType<typeof listItemProps>>>;
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'ListItem',
inheritAttrs: false,
props: listItemProps(),
setup(props, { slots, attrs }) {
const showProgress = shallowRef(false);
const progressRafRef = shallowRef();
onMounted(() => {
progressRafRef.value = setTimeout(() => {
showProgress.value = true;
}, 300);
});
onBeforeUnmount(() => {
clearTimeout(progressRafRef.value);
});
const mergedStatus = shallowRef(props.file?.status);
watch(
() => props.file?.status,
status => {
if (status !== 'removed') {
mergedStatus.value = status;
}
},
);
const { rootPrefixCls } = useConfigInject('upload', props);
const transitionProps = computed(() => getTransitionProps(`${rootPrefixCls.value}-fade`));
return () => {
const {
prefixCls,
locale,
listType,
file,
items,
progress: progressProps,
iconRender = slots.iconRender,
actionIconRender = slots.actionIconRender,
itemRender = slots.itemRender,
isImgUrl,
showPreviewIcon,
showRemoveIcon,
showDownloadIcon,
previewIcon: customPreviewIcon = slots.previewIcon,
removeIcon: customRemoveIcon = slots.removeIcon,
downloadIcon: customDownloadIcon = slots.downloadIcon,
onPreview,
onDownload,
onClose,
} = props;
const { class: className, style } = attrs;
// This is used for legacy span make scrollHeight the wrong value.
// We will force these to be `display: block` with non `picture-card`
const iconNode = iconRender({ file });
let icon = <div class={`${prefixCls}-text-icon`}>{iconNode}</div>;
if (listType === 'picture' || listType === 'picture-card') {
if (mergedStatus.value === 'uploading' || (!file.thumbUrl && !file.url)) {
const uploadingClassName = {
[`${prefixCls}-list-item-thumbnail`]: true,
[`${prefixCls}-list-item-file`]: mergedStatus.value !== 'uploading',
};
icon = <div class={uploadingClassName}>{iconNode}</div>;
} else {
const thumbnail = isImgUrl?.(file) ? (
<img
src={file.thumbUrl || file.url}
alt={file.name}
class={`${prefixCls}-list-item-image`}
crossorigin={file.crossOrigin}
/>
) : (
iconNode
);
const aClassName = {
[`${prefixCls}-list-item-thumbnail`]: true,
[`${prefixCls}-list-item-file`]: isImgUrl && !isImgUrl(file),
};
icon = (
<a
class={aClassName}
onClick={e => onPreview(file, e)}
href={file.url || file.thumbUrl}
target="_blank"
rel="noopener noreferrer"
>
{thumbnail}
</a>
);
}
}
const infoUploadingClass = {
[`${prefixCls}-list-item`]: true,
[`${prefixCls}-list-item-${mergedStatus.value}`]: true,
};
const linkProps =
typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
const removeIcon = showRemoveIcon
? actionIconRender({
customIcon: customRemoveIcon ? customRemoveIcon({ file }) : <DeleteOutlined />,
callback: () => onClose(file),
prefixCls,
title: locale.removeFile,
})
: null;
const downloadIcon =
showDownloadIcon && mergedStatus.value === 'done'
? actionIconRender({
customIcon: customDownloadIcon ? customDownloadIcon({ file }) : <DownloadOutlined />,
callback: () => onDownload(file),
prefixCls,
title: locale.downloadFile,
})
: null;
const downloadOrDelete = listType !== 'picture-card' && (
<span
key="download-delete"
class={[
`${prefixCls}-list-item-actions`,
{
picture: listType === 'picture',
},
]}
>
{downloadIcon}
{removeIcon}
</span>
);
const listItemNameClass = `${prefixCls}-list-item-name`;
const fileName = file.url
? [
<a
key="view"
target="_blank"
rel="noopener noreferrer"
class={listItemNameClass}
title={file.name}
{...linkProps}
href={file.url}
onClick={e => onPreview(file, e)}
>
{file.name}
</a>,
downloadOrDelete,
]
: [
<span
key="view"
class={listItemNameClass}
onClick={e => onPreview(file, e)}
title={file.name}
>
{file.name}
</span>,
downloadOrDelete,
];
const previewStyle: 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 : previewStyle}
onClick={e => onPreview(file, e)}
title={locale.previewFile}
>
{customPreviewIcon ? customPreviewIcon({ file }) : <EyeOutlined />}
</a>
) : null;
const pictureCardActions = listType === 'picture-card' &&
mergedStatus.value !== 'uploading' && (
<span class={`${prefixCls}-list-item-actions`}>
{previewIcon}
{mergedStatus.value === 'done' && downloadIcon}
{removeIcon}
</span>
);
const dom = (
<div class={infoUploadingClass}>
{icon}
{fileName}
{pictureCardActions}
{showProgress.value && (
<Transition {...transitionProps.value}>
<div
v-show={mergedStatus.value === 'uploading'}
class={`${prefixCls}-list-item-progress`}
>
{'percent' in file ? (
<Progress
{...(progressProps as UploadListProgressProps)}
type="line"
percent={file.percent}
/>
) : null}
</div>
</Transition>
)}
</div>
);
const listContainerNameClass = {
[`${prefixCls}-list-item-container`]: true,
[`${className}`]: !!className,
};
const message =
file.response && typeof file.response === 'string'
? file.response
: file.error?.statusText || file.error?.message || locale.uploadError;
const item =
mergedStatus.value === 'error' ? (
<Tooltip title={message} getPopupContainer={node => node.parentNode as HTMLElement}>
{dom}
</Tooltip>
) : (
dom
);
return (
<div class={listContainerNameClass} style={style as CSSProperties}>
{itemRender
? itemRender({
originNode: item,
file,
fileList: items,
actions: {
download: onDownload.bind(null, file),
preview: onPreview.bind(null, file),
remove: onClose.bind(null, file),
},
})
: item}
</div>
);
};
},
});