-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPullRefreshList.tsx
More file actions
364 lines (319 loc) · 9.46 KB
/
PullRefreshList.tsx
File metadata and controls
364 lines (319 loc) · 9.46 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* PullRefreshList - 通用下拉刷新列表组件
*
* 支持下拉刷新、上拉加载、单列/多列瀑布流布局
*/
import { useRef, useState, useCallback } from '@lynx-js/react';
import type { ReactNode } from '@lynx-js/react';
import type { PullRefreshListProps } from './types';
import {
DEFAULT_PULL_THRESHOLD,
DEFAULT_DAMPING,
DEFAULT_LOADING_HEIGHT,
DEFAULT_SPAN_COUNT,
DEFAULT_LOWER_THRESHOLD_ITEM_COUNT,
MIN_PULL_DISTANCE,
} from './constants';
import refreshIconSrc from './assets/loading.png';
import './PullRefreshList.css';
/// <reference types="@lynx-js/types" />
export function PullRefreshList<T>({
// 核心功能
data,
renderItem,
keyExtractor,
onRefresh,
// 布局配置
listType = 'flow',
spanCount = DEFAULT_SPAN_COUNT,
listMainAxisGap = '9px',
listCrossAxisGap = '8px',
// 头部/尾部
headerComponent,
headerHeight = 0,
emptyComponent,
footerComponent,
// 分页加载
onLoadMore,
hasMoreData = true,
lowerThresholdItemCount = DEFAULT_LOWER_THRESHOLD_ITEM_COUNT,
// 滚动事件
onScroll,
onScrollToEdge,
// 样式定制
style,
listStyle,
// 下拉刷新配置
pullThreshold = DEFAULT_PULL_THRESHOLD,
damping = DEFAULT_DAMPING,
loadingHeight = DEFAULT_LOADING_HEIGHT,
refreshIcon = refreshIconSrc,
}: PullRefreshListProps<T>) {
const listRef = useRef<any>(null);
// 下拉刷新相关状态
const [pullStartY, setPullStartY] = useState(0);
const [isPulling, setIsPulling] = useState(false);
const [pullDistance, setPullDistance] = useState(0);
const [indicatorHeight, setIndicatorHeight] = useState(0);
const [isAtTop, setIsAtTop] = useState(true);
const [iconRotation, setIconRotation] = useState(0);
const [isLoadingState, setIsLoadingState] = useState(false);
const [iconClassName, setIconClassName] = useState('');
const [touchStartedInHeader, setTouchStartedInHeader] = useState(false);
// ===== 触摸事件处理 =====
const handleTouchStart = useCallback((event: any) => {
if (!isAtTop || isLoadingState || !onRefresh) {
return;
}
const touches = event.detail?.touches || event.touches;
if (touches && touches[0]) {
const touchY = touches[0].clientY;
// 检查触摸位置是否在 header 区域内
if (headerHeight > 0 && touchY < headerHeight) {
setTouchStartedInHeader(true);
return;
}
// 触摸在有效区域,重置标记并记录位置
setTouchStartedInHeader(false);
setPullStartY(touchY);
}
}, [isAtTop, isLoadingState, onRefresh, headerHeight]);
const handleTouchMove = useCallback((event: any) => {
// 如果触摸从 header 区域开始,完全忽略这次手势
if (touchStartedInHeader) {
return;
}
if (isLoadingState || !onRefresh) {
return;
}
const touches = event.detail?.touches || event.touches;
if (!touches || !touches[0]) {
return;
}
const touch = touches[0];
const deltaY = touch.clientY - pullStartY;
// 如果还没开始下拉,检查是否应该开始
if (!isPulling) {
// 必须在顶部才能开始下拉
if (!isAtTop) {
return;
}
// 只有明确的下拉手势(向下且超过阈值)才开始拦截
if (deltaY > MIN_PULL_DISTANCE) {
setIsPulling(true);
} else {
// 向上滑动或距离不够,不拦截,让列表自然滚动
return;
}
}
// 到这里,isPulling 肯定是 true,继续处理下拉逻辑
if (deltaY > 0) {
// 下拉中,指示器跟随手指
const distance = deltaY * damping;
setPullDistance(distance);
// 更新指示器高度
const height = Math.min(distance, 100);
setIndicatorHeight(height);
// 更新图标旋转角度
const rotation = (distance / 100) * 360;
setIconRotation(rotation);
} else {
// 用户往上推超过起始点(deltaY <= 0)
// 只收起指示器,但保持 isPulling=true,继续拦截触摸,防止列表滚动
setPullDistance(0);
setIndicatorHeight(0);
setIconRotation(0);
// 注意:不取消 isPulling 状态,等 touchend 时再取消
}
}, [touchStartedInHeader, isPulling, isLoadingState, pullStartY, onRefresh, damping, isAtTop]);
const handleTouchEnd = useCallback(async () => {
if (!isPulling || isLoadingState || !onRefresh) {
return;
}
setIsPulling(false);
const currentDistance = pullDistance;
if (currentDistance >= pullThreshold) {
// 达到阈值,触发刷新
setIndicatorHeight(loadingHeight);
setIconClassName('spinning');
setIsLoadingState(true);
await onRefresh();
// 开始重置
setIndicatorHeight(0);
// 延迟300ms再重置其他状态,让图标在收起过程中继续旋转
setTimeout(() => {
setIconRotation(0);
setPullDistance(0);
setIsLoadingState(false);
setIconClassName('');
}, 300); // 匹配过渡动画时长
} else {
// 未达到阈值,回弹
setIndicatorHeight(0);
setIconRotation(0);
setPullDistance(0);
}
}, [isPulling, isLoadingState, pullDistance, onRefresh, pullThreshold, loadingHeight]);
// ===== 滚动事件处理 =====
const handleScrollToTop = useCallback(() => {
setIsAtTop(true);
}, []);
const handleScrollEvent = useCallback((event: any) => {
const scrollTop = event.detail?.scrollTop || 0;
if (scrollTop > 10) {
setIsAtTop(false);
}
if (onScroll) {
onScroll(event);
}
}, [onScroll]);
const handleScrollToLower = useCallback((e: any) => {
// 只有在有更多数据时才调用 onLoadMore
if (hasMoreData && onLoadMore) {
onLoadMore();
}
}, [hasMoreData, onLoadMore]);
// ===== 样式合并 =====
const containerStyle = {
...styles.touchContainer,
...style,
};
const finalListStyle = {
...styles.listContainer,
listMainAxisGap,
listCrossAxisGap,
...listStyle,
};
return (
<view
style={containerStyle}
bindtouchstart={onRefresh ? handleTouchStart : undefined}
bindtouchmove={onRefresh ? handleTouchMove : undefined}
bindtouchend={onRefresh ? handleTouchEnd : undefined}
>
<list
ref={listRef}
style={finalListStyle}
list-type={listType}
span-count={listType === 'flow' ? spanCount : 1}
scroll-orientation="vertical"
bounces={true}
enable-scroll={!isPulling}
lower-threshold-item-count={lowerThresholdItemCount}
bindscroll={onRefresh ? handleScrollEvent : onScroll}
bindscrolltoupper={(e) => {
if (onRefresh) {
handleScrollToTop();
} else {
if (onScroll) onScroll(e);
}
onScrollToEdge?.(e);
}}
bindscrolltolower={handleScrollToLower}
>
{/* 头部组件 */}
{headerComponent && (
<list-item
key="header"
item-key="header"
full-span={true}
style={{ marginLeft: '-9px', marginRight: '-9px' }}
>
{headerComponent}
</list-item>
)}
{/* 下拉刷新指示器 */}
{onRefresh && (
<list-item
key="pull-refresh-indicator"
item-key="pull-refresh-indicator"
full-span={true}
recyclable={false}
>
<view
style={{
...styles.pullRefreshIndicator,
height: `${indicatorHeight}px`,
transition: (isLoadingState || indicatorHeight === 0)
? 'height 0.3s ease-in-out'
: 'none',
}}
>
<image
src={refreshIcon}
class={iconClassName}
style={{
...styles.refreshIcon,
...(isLoadingState ? {} : { transform: `rotate(${iconRotation}deg)` }),
}}
/>
</view>
</list-item>
)}
{/* 内容列表 */}
{data.length > 0 ? (
data.map((item, index) => {
const key = keyExtractor(item, index);
return (
<list-item
key={key}
item-key={String(key)}
>
{renderItem(item, index)}
</list-item>
);
})
) : (
emptyComponent && (
<list-item
key="empty-state"
item-key="empty-state"
full-span={true}
recyclable={false}
>
{emptyComponent}
</list-item>
)
)}
{/* 尾部组件 */}
{footerComponent && (
<list-item
key="footer"
item-key="footer"
full-span={true}
recyclable={false}
>
{footerComponent}
</list-item>
)}
</list>
</view>
);
}
const styles = {
touchContainer: {
width: '100%',
height: '100%',
},
pullRefreshIndicator: {
width: '100%',
height: '0px',
display: 'flex' as const,
justifyContent: 'center' as const,
alignItems: 'flex-end' as const,
backgroundColor: '#ffffff',
overflow: 'hidden' as const,
},
refreshIcon: {
width: '20px',
height: '20px',
marginBottom: '7px',
},
listContainer: {
width: '100%',
height: '100%',
paddingLeft: '9px',
paddingRight: '9px',
paddingBottom: '20px',
},
};