fileList.tsx
3.7 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
import React, { useState, useRef, useEffect } from 'react';
import { Tooltip, Button, Empty } from 'antd';
import { SearchOutlined } from '@ant-design/icons';
import RightIcon from '@/app/icons/right.svg';
interface FileListProps {
filelist: string[];
}
export function FileList(props: FileListProps) {
const { filelist } = props;
const containerRef = useRef<HTMLDivElement>(null);
const [showLeftButton, setShowLeftButton] = useState(false);
const [showRightButton, setShowRightButton] = useState(false);
// 检测是否需要显示按钮
useEffect(() => {
if (containerRef.current) {
const { scrollWidth, clientWidth, scrollLeft } = containerRef.current;
setShowLeftButton(scrollLeft > 0);
setShowRightButton(scrollLeft < scrollWidth - clientWidth);
}
}, [filelist]);
// 滚动步长
const scrollStep = 60; // 每次滚动的像素数
// 向左滚动
const scrollLeft = () => {
if (containerRef.current) {
containerRef.current.scrollBy({
left: -scrollStep,
behavior: 'smooth',
});
}
};
// 向右滚动
const scrollRight = () => {
if (containerRef.current) {
containerRef.current.scrollBy({
left: scrollStep,
behavior: 'smooth',
});
}
};
return (
<div>
{filelist.length ? (
<div
ref={containerRef}
style={{
display: 'flex',
justifyContent: 'center',
overflowX: 'auto',
gap: '10px',
padding: '10px',
position: 'relative',
}}
>
{/* 左侧按钮 */}
{showLeftButton && (
<Button
shape="circle"
icon={<SearchOutlined />}
size="large"
onClick={scrollLeft}
style={{
position: 'absolute',
left: 0,
top: '50%',
transform: 'translateY(-50%)',
zIndex: 1,
}}
/>
)}
{/* 图片列表 */}
{filelist.map((fileUrl, index) => (
<img
key={index}
src={fileUrl}
alt={`image-${index}`}
style={{ width: '60px', height: '60px', objectFit: 'cover', borderRadius: '8px' }}
/>
))}
{/* 右侧按钮 */}
{showRightButton && (
<Button
shape="circle"
icon={RightIcon}
size="large"
onClick={scrollRight}
style={{
position: 'absolute',
right: 0,
top: '50%',
transform: 'translateY(-50%)',
zIndex: 1,
}}
/>
)}
</div>
) : (
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
)}
</div>
);
}