Upload.tsx
9.13 KB
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
import { useRef, useState, type CSSProperties } from 'react';
import { Icon } from './Icon';
import styles from './Upload.module.css';
import { cx } from './utils';
export type UploadFileStatus = 'uploading' | 'done' | 'error';
export interface UploadFileItem {
id: string;
name: string;
size: number;
status: UploadFileStatus;
progress?: number;
errorMessage?: string;
previewable?: boolean;
}
export interface UploadDraggerProps {
/** UploadDragger 只负责 UI 状态和操作意图,不执行真实校验、分片续传或接口上传。 */
files: UploadFileItem[];
title?: string;
helperText?: string;
accept?: string;
multiple?: boolean;
disabled?: boolean;
className?: string;
style?: CSSProperties;
onSelectFiles?: (files: File[]) => void;
onPreview?: (id: string) => void;
onDownload?: (id: string) => void;
onRemove?: (id: string) => void;
onRetry?: (id: string) => void;
onCancel?: (id: string) => void;
}
function formatSize(size: number) {
return `${(size / 1024 / 1024).toFixed(2)} MB`;
}
function clampProgress(progress = 0) {
return Math.min(100, Math.max(0, Math.round(progress)));
}
function IconButton({ label, name, onClick }: { label: string; name: Parameters<typeof Icon>[0]['name']; onClick?: () => void }) {
if (!onClick) return null;
return (
<button className={styles.iconButton} type="button" aria-label={label} onClick={onClick}>
<Icon name={name} size={16} />
</button>
);
}
function FileActions({
file,
onCancel,
onDownload,
onPreview,
onRemove,
onRetry,
}: {
file: UploadFileItem;
onCancel?: (id: string) => void;
onDownload?: (id: string) => void;
onPreview?: (id: string) => void;
onRemove?: (id: string) => void;
onRetry?: (id: string) => void;
}) {
if (file.status === 'uploading') {
return <IconButton label={`取消 ${file.name}`} name="close-circle" onClick={onCancel ? () => onCancel(file.id) : undefined} />;
}
if (file.status === 'error') {
return (
<>
<IconButton label={`重试 ${file.name}`} name="refresh" onClick={onRetry ? () => onRetry(file.id) : undefined} />
<IconButton label={`删除 ${file.name}`} name="trash" onClick={onRemove ? () => onRemove(file.id) : undefined} />
</>
);
}
return (
<>
{file.previewable ? <IconButton label={`预览 ${file.name}`} name="eye" onClick={onPreview ? () => onPreview(file.id) : undefined} /> : null}
<IconButton label={`下载 ${file.name}`} name="download" onClick={onDownload ? () => onDownload(file.id) : undefined} />
<IconButton label={`删除 ${file.name}`} name="trash" onClick={onRemove ? () => onRemove(file.id) : undefined} />
</>
);
}
export function UploadDragger({
accept,
className,
disabled = false,
files,
helperText = '支持分片断点续传,单文件上限 1GB',
multiple = true,
onCancel,
onDownload,
onPreview,
onRemove,
onRetry,
onSelectFiles,
style,
title = '点击或拖拽文件到此上传',
}: UploadDraggerProps) {
const inputRef = useRef<HTMLInputElement>(null);
const dragDepthRef = useRef(0);
const [dragActive, setDragActive] = useState(false);
const emitFiles = (selectedFiles: FileList | File[]) => {
const nextFiles = Array.from(selectedFiles);
if (!disabled && nextFiles.length > 0) onSelectFiles?.(nextFiles);
};
const resetDragState = () => {
dragDepthRef.current = 0;
setDragActive(false);
};
return (
<div className={cx(styles.root, className)} style={style}>
<button
className={cx(styles.dragger, dragActive && styles.draggerActive)}
data-testid="upload-dragger"
type="button"
disabled={disabled}
onClick={() => inputRef.current?.click()}
onDragEnter={(event) => {
event.preventDefault();
dragDepthRef.current += 1;
if (!disabled) setDragActive(true);
}}
onDragLeave={(event) => {
event.preventDefault();
dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
if (dragDepthRef.current === 0) setDragActive(false);
}}
onDragOver={(event) => {
event.preventDefault();
if (!disabled) event.dataTransfer.dropEffect = 'copy';
}}
onDrop={(event) => {
event.preventDefault();
resetDragState();
emitFiles(event.dataTransfer.files);
}}
>
<span className={styles.uploadIcon} aria-hidden="true">
<Icon name="upload" size={32} />
</span>
<span className={styles.draggerTitle}>{title}</span>
<span className={styles.draggerHelper}>{helperText}</span>
</button>
<input
ref={inputRef}
aria-label="选择上传文件"
className={styles.input}
type="file"
accept={accept}
multiple={multiple}
disabled={disabled}
onChange={(event) => {
if (event.target.files) emitFiles(event.target.files);
event.currentTarget.value = '';
}}
/>
{files.length > 0 ? (
<ul className={styles.fileList} aria-label="上传文件列表">
{files.map((file) => {
const progress = clampProgress(file.progress);
const statusText = file.status === 'done' ? '已完成' : file.status === 'error' ? '上传失败' : `上传中 ${progress}%`;
return (
<li key={file.id} className={cx(styles.fileItem, file.status === 'error' && styles.fileItemError)}>
<div className={styles.fileMain}>
<span className={styles.fileIcon} aria-hidden="true">
<Icon name="file" size={18} />
</span>
<div className={styles.fileText}>
<span className={styles.fileName}>{file.name}</span>
<span className={styles.fileMeta}>{formatSize(file.size)} · {statusText}</span>
{file.status === 'error' && file.errorMessage ? <span className={styles.fileError}>{file.errorMessage}</span> : null}
</div>
</div>
<div className={styles.fileActions}>
<FileActions file={file} onCancel={onCancel} onDownload={onDownload} onPreview={onPreview} onRemove={onRemove} onRetry={onRetry} />
</div>
{file.status === 'uploading' ? (
<div className={styles.progressTrack} role="progressbar" aria-label={`${file.name} 上传进度`} aria-valuemin={0} aria-valuemax={100} aria-valuenow={progress}>
<span className={styles.progressFill} style={{ width: `${progress}%` }} />
</div>
) : null}
</li>
);
})}
</ul>
) : null}
</div>
);
}
export interface MediaWallItem {
id: string;
type: 'image' | 'video';
src: string;
alt: string;
}
export interface MediaWallProps {
/** MediaWall 展示素材缩略图和预览意图;图片放大、视频播放和真实上传由业务侧组合实现。 */
items: MediaWallItem[];
helperText?: string;
accept?: string;
multiple?: boolean;
disabled?: boolean;
className?: string;
style?: CSSProperties;
onPreview?: (item: MediaWallItem) => void;
onSelectFiles?: (files: File[]) => void;
}
export function MediaWall({
accept = 'image/*,video/*',
className,
disabled = false,
helperText,
items,
multiple = true,
onPreview,
onSelectFiles,
style,
}: MediaWallProps) {
const inputRef = useRef<HTMLInputElement>(null);
return (
<div className={cx(styles.mediaRoot, className)} style={style}>
<ul className={styles.mediaGrid} data-testid="media-wall-grid" aria-label="媒体资源墙">
{items.map((item) => (
<li key={item.id} className={styles.mediaCell}>
<button className={styles.mediaButton} type="button" aria-label={`预览 ${item.alt}`} onClick={() => onPreview?.(item)}>
<img className={styles.mediaThumb} src={item.src} alt={item.alt} />
<span className={styles.srOnly}>{item.alt}</span>
{item.type === 'video' ? (
<span className={styles.playBadge} aria-label="视频">
<Icon name="play" size={20} />
</span>
) : null}
</button>
</li>
))}
<li className={styles.mediaCell}>
<button className={styles.mediaUpload} type="button" disabled={disabled} onClick={() => inputRef.current?.click()}>
<span className={styles.mediaPlus} aria-hidden="true">
<Icon name="plus" size={22} />
</span>
<span>图片/视频</span>
</button>
<input
ref={inputRef}
aria-label="选择图片或视频"
className={styles.input}
type="file"
accept={accept}
multiple={multiple}
disabled={disabled}
onChange={(event) => {
if (event.target.files) onSelectFiles?.(Array.from(event.target.files));
event.currentTarget.value = '';
}}
/>
</li>
</ul>
{helperText ? <p className={styles.mediaHelper}>{helperText}</p> : null}
</div>
);
}