@tarojs/components-advanced 4.1.12-beta.2 → 4.1.12-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.js +2 -0
- package/dist/components/index.js.map +1 -1
- package/dist/components/list/NoMore.d.ts +30 -0
- package/dist/components/list/NoMore.js +10 -0
- package/dist/components/list/NoMore.js.map +1 -0
- package/dist/components/list/hooks/useItemSizeCache.d.ts +13 -0
- package/dist/components/list/hooks/useItemSizeCache.js +40 -0
- package/dist/components/list/hooks/useItemSizeCache.js.map +1 -0
- package/dist/components/list/hooks/useListNestedScroll.d.ts +18 -0
- package/dist/components/list/hooks/useListNestedScroll.js +61 -0
- package/dist/components/list/hooks/useListNestedScroll.js.map +1 -0
- package/dist/components/list/hooks/useListScrollElementAttach.d.ts +25 -0
- package/dist/components/list/hooks/useListScrollElementAttach.js +88 -0
- package/dist/components/list/hooks/useListScrollElementAttach.js.map +1 -0
- package/dist/components/list/hooks/useListScrollElementAttachWeapp.d.ts +27 -0
- package/dist/components/list/hooks/useListScrollElementAttachWeapp.js +153 -0
- package/dist/components/list/hooks/useListScrollElementAttachWeapp.js.map +1 -0
- package/dist/components/list/hooks/useMeasureStartOffset.d.ts +12 -0
- package/dist/components/list/hooks/useMeasureStartOffset.js +84 -0
- package/dist/components/list/hooks/useMeasureStartOffset.js.map +1 -0
- package/dist/components/list/hooks/useMeasureStartOffsetWeapp.d.ts +13 -0
- package/dist/components/list/hooks/useMeasureStartOffsetWeapp.js +85 -0
- package/dist/components/list/hooks/useMeasureStartOffsetWeapp.js.map +1 -0
- package/dist/components/list/hooks/useRefresher.d.ts +74 -0
- package/dist/components/list/hooks/useRefresher.js +503 -0
- package/dist/components/list/hooks/useRefresher.js.map +1 -0
- package/dist/components/list/hooks/useResizeObserver.d.ts +26 -0
- package/dist/components/list/hooks/useResizeObserver.js +152 -0
- package/dist/components/list/hooks/useResizeObserver.js.map +1 -0
- package/dist/components/list/hooks/useScrollCorrection.d.ts +19 -0
- package/dist/components/list/hooks/useScrollCorrection.js +73 -0
- package/dist/components/list/hooks/useScrollCorrection.js.map +1 -0
- package/dist/components/list/hooks/useScrollParentAutoFind.d.ts +20 -0
- package/dist/components/list/hooks/useScrollParentAutoFind.js +81 -0
- package/dist/components/list/hooks/useScrollParentAutoFind.js.map +1 -0
- package/dist/components/list/index.d.ts +64 -7
- package/dist/components/list/index.js +1041 -162
- package/dist/components/list/index.js.map +1 -1
- package/dist/components/list/utils.d.ts +16 -0
- package/dist/components/list/utils.js +19 -0
- package/dist/components/list/utils.js.map +1 -0
- package/dist/components/virtual-list/vue/list.d.ts +12 -12
- package/dist/components/virtual-waterfall/vue/waterfall.d.ts +11 -11
- package/dist/components/water-flow/flow-item.js +6 -4
- package/dist/components/water-flow/flow-item.js.map +1 -1
- package/dist/components/water-flow/flow-section.js +1 -1
- package/dist/components/water-flow/flow-section.js.map +1 -1
- package/dist/components/water-flow/index.d.ts +1 -1
- package/dist/components/water-flow/interface.d.ts +18 -2
- package/dist/components/water-flow/root.d.ts +35 -4
- package/dist/components/water-flow/root.js +114 -42
- package/dist/components/water-flow/root.js.map +1 -1
- package/dist/components/water-flow/section.d.ts +7 -1
- package/dist/components/water-flow/section.js +54 -9
- package/dist/components/water-flow/section.js.map +1 -1
- package/dist/components/water-flow/utils.d.ts +4 -0
- package/dist/components/water-flow/utils.js +5 -1
- package/dist/components/water-flow/utils.js.map +1 -1
- package/dist/components/water-flow/water-flow-node-cache.d.ts +24 -0
- package/dist/components/water-flow/water-flow-node-cache.js +161 -0
- package/dist/components/water-flow/water-flow-node-cache.js.map +1 -0
- package/dist/components/water-flow/water-flow.d.ts +2 -3
- package/dist/components/water-flow/water-flow.js +286 -31
- package/dist/components/water-flow/water-flow.js.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/scrollElementContext.d.ts +15 -0
- package/dist/utils/scrollElementContext.js +14 -0
- package/dist/utils/scrollElementContext.js.map +1 -0
- package/dist/utils/scrollParent.d.ts +33 -0
- package/dist/utils/scrollParent.js +88 -0
- package/dist/utils/scrollParent.js.map +1 -0
- package/package.json +9 -8
|
@@ -1,11 +1,23 @@
|
|
|
1
|
-
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
1
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
2
|
import { View, ScrollView } from '@tarojs/components';
|
|
3
|
+
import Taro from '@tarojs/taro';
|
|
3
4
|
import React from 'react';
|
|
5
|
+
import '../../utils/index.js';
|
|
6
|
+
import { ScrollElementContextOrFallback } from '../../utils/scrollElementContext.js';
|
|
7
|
+
import { useItemSizeCache } from './hooks/useItemSizeCache.js';
|
|
8
|
+
import { useListNestedScroll } from './hooks/useListNestedScroll.js';
|
|
9
|
+
import { useListScrollElementAttach } from './hooks/useListScrollElementAttach.js';
|
|
10
|
+
import { useListScrollElementAttachWeapp } from './hooks/useListScrollElementAttachWeapp.js';
|
|
11
|
+
import { useRefresher, DEFAULT_REFRESHER_HEIGHT } from './hooks/useRefresher.js';
|
|
12
|
+
import { useResizeObserver } from './hooks/useResizeObserver.js';
|
|
13
|
+
import { useScrollCorrection } from './hooks/useScrollCorrection.js';
|
|
4
14
|
import { ListItem } from './ListItem.js';
|
|
15
|
+
import { NoMore } from './NoMore.js';
|
|
5
16
|
import { StickyHeader } from './StickyHeader.js';
|
|
6
17
|
import { StickySection } from './StickySection.js';
|
|
18
|
+
import { isWeapp, isH5, supportsNativeRefresher } from './utils.js';
|
|
19
|
+
import { getScrollViewContextNode } from '../../utils/dom.js';
|
|
7
20
|
|
|
8
|
-
// 工具:累加数组
|
|
9
21
|
function accumulate(arr) {
|
|
10
22
|
const result = [0];
|
|
11
23
|
for (let i = 0; i < arr.length; i++) {
|
|
@@ -13,46 +25,304 @@ function accumulate(arr) {
|
|
|
13
25
|
}
|
|
14
26
|
return result;
|
|
15
27
|
}
|
|
16
|
-
// 检测抖动
|
|
17
28
|
function isShaking(diffList) {
|
|
18
29
|
if (diffList.length < 3)
|
|
19
30
|
return false;
|
|
20
|
-
// 检查是否有连续的正负交替
|
|
21
31
|
const signs = diffList.map(diff => Math.sign(diff));
|
|
22
32
|
let alternations = 0;
|
|
23
33
|
for (let i = 1; i < signs.length; i++) {
|
|
24
|
-
if (signs[i] !== 0 && signs[i] !== signs[i - 1])
|
|
34
|
+
if (signs[i] !== 0 && signs[i] !== signs[i - 1])
|
|
25
35
|
alternations++;
|
|
26
|
-
}
|
|
27
36
|
}
|
|
28
|
-
// 如果交替次数过多,认为是抖动
|
|
29
37
|
return alternations >= 2;
|
|
30
38
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
// 小程序端:判断 item 是否应该执行 SelectorQuery 测量(仅检查是否已测量过)
|
|
40
|
+
// SelectorQuery 是只读查询,不会触发 setData,滚动期间执行是安全的
|
|
41
|
+
function shouldMeasureWeappItem(index, measuredSet) {
|
|
42
|
+
return !measuredSet.has(index);
|
|
43
|
+
}
|
|
44
|
+
// 小程序端暂不外抛 onItemSizeChange,避免父层重渲染导致 List remount 引发回顶或空白
|
|
45
|
+
function weappDeferItemSizeChange(_index, _size, _onItemSizeChange) { }
|
|
46
|
+
/** 从 scroll 选项解析目标偏移量 */
|
|
47
|
+
function resolveScrollTargetOffset(options, isHorizontal) {
|
|
48
|
+
const opts = options !== null && options !== void 0 ? options : {};
|
|
49
|
+
const top = typeof opts.top === 'number' ? opts.top : undefined;
|
|
50
|
+
const left = typeof opts.left === 'number' ? opts.left : undefined;
|
|
51
|
+
let result = 0;
|
|
52
|
+
if (isHorizontal) {
|
|
53
|
+
if (typeof left === 'number')
|
|
54
|
+
result = left;
|
|
55
|
+
else if (typeof top === 'number')
|
|
56
|
+
result = top;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (typeof top === 'number')
|
|
60
|
+
result = top;
|
|
61
|
+
else if (typeof left === 'number')
|
|
62
|
+
result = left;
|
|
63
|
+
}
|
|
64
|
+
return Number.isFinite(result) ? result : 0;
|
|
65
|
+
}
|
|
66
|
+
// eslint-disable-next-line complexity -- List 多端/多模式逻辑集中,已抽离 useListNestedScroll、useListScrollElementAttach、resolveScrollTargetOffset 等
|
|
67
|
+
const InnerList = (props, ref) => {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
const { stickyHeader = false, space = 0, height = 400, width = '100%', showScrollbar = true, scrollTop: controlledScrollTop, scrollX = false, scrollY = true, onScroll, onScrollToUpper, onScrollToLower, onScrollStart, onScrollEnd, upperThreshold = 50, lowerThreshold = 50, cacheCount = 2, cacheExtent, enableBackToTop, className, style, children, nestedScroll, scrollElement, scrollRef: scrollRefProp, } = props;
|
|
34
70
|
const isHorizontal = scrollX === true;
|
|
71
|
+
const listType = nestedScroll === true ? 'nested' : 'default';
|
|
72
|
+
const { effectiveScrollElement, effectiveStartOffset, effectiveStartOffsetRef, useScrollElementMode, needAutoFind, autoFindStatus, contentWrapperRef, contentId, } = useListNestedScroll(listType, scrollElement, undefined, isHorizontal);
|
|
35
73
|
const DEFAULT_ITEM_WIDTH = 120;
|
|
36
74
|
const DEFAULT_ITEM_HEIGHT = 40;
|
|
75
|
+
const defaultItemSize = isHorizontal ? DEFAULT_ITEM_WIDTH : DEFAULT_ITEM_HEIGHT;
|
|
76
|
+
const normalizeSize = React.useCallback((value) => {
|
|
77
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0)
|
|
78
|
+
return null;
|
|
79
|
+
return value;
|
|
80
|
+
}, []);
|
|
81
|
+
const resolveItemSizeByIndex = React.useCallback((index, fallback) => {
|
|
82
|
+
const { itemSize, itemData } = props;
|
|
83
|
+
const numberSize = normalizeSize(itemSize);
|
|
84
|
+
if (numberSize != null)
|
|
85
|
+
return numberSize;
|
|
86
|
+
if (typeof itemSize === 'function') {
|
|
87
|
+
const functionSize = normalizeSize(itemSize(index, itemData));
|
|
88
|
+
if (functionSize != null)
|
|
89
|
+
return functionSize;
|
|
90
|
+
}
|
|
91
|
+
return fallback;
|
|
92
|
+
}, [props.itemSize, props.itemData, normalizeSize]);
|
|
37
93
|
// 滚动状态管理
|
|
38
94
|
const containerRef = React.useRef(null);
|
|
95
|
+
// 生成唯一 List ID(用于小程序 ResizeObserver)
|
|
96
|
+
const listId = React.useMemo(() => `list-${Math.random().toString(36).slice(2, 11)}`, []);
|
|
39
97
|
// 渲染偏移量 - 用于计算应该渲染哪些元素
|
|
40
98
|
const [renderOffset, setRenderOffset] = React.useState(controlledScrollTop !== null && controlledScrollTop !== void 0 ? controlledScrollTop : 0);
|
|
41
|
-
//
|
|
99
|
+
// 程序性滚动用的目标偏移;用户滑动期间不更新,避免与原生滚动冲突
|
|
42
100
|
const [scrollViewOffset, setScrollViewOffset] = React.useState(controlledScrollTop !== null && controlledScrollTop !== void 0 ? controlledScrollTop : 0);
|
|
43
|
-
|
|
101
|
+
// 用户正在滑动时不再向 ScrollView 传 scrollTop,让滚动完全由原生接管
|
|
102
|
+
const [isUserScrolling, setIsUserScrolling] = React.useState(false);
|
|
103
|
+
// isUserScrolling 的 ref 镜像,供异步上下文读取最新值
|
|
104
|
+
const isUserScrollingRef = React.useRef(false);
|
|
105
|
+
const initialContainerLength = typeof (isHorizontal ? width : height) === 'number' ? (isHorizontal ? width : height) : 400;
|
|
106
|
+
const [containerLength, setContainerLength] = React.useState(initialContainerLength);
|
|
107
|
+
// 用容器实际尺寸更新视口长度,避免 props 与 CSS 不一致导致底部空白
|
|
108
|
+
React.useEffect(() => {
|
|
109
|
+
const el = containerRef.current;
|
|
110
|
+
if (!el || typeof ResizeObserver === 'undefined')
|
|
111
|
+
return;
|
|
112
|
+
const ro = new ResizeObserver((entries) => {
|
|
113
|
+
for (const entry of entries) {
|
|
114
|
+
const { contentRect } = entry;
|
|
115
|
+
const measured = isHorizontal ? contentRect.width : contentRect.height;
|
|
116
|
+
if (measured > 0)
|
|
117
|
+
setContainerLength(measured);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
ro.observe(el);
|
|
121
|
+
return () => ro.disconnect();
|
|
122
|
+
}, [isHorizontal]);
|
|
44
123
|
// 滚动追踪相关refs
|
|
45
124
|
const isScrollingRef = React.useRef(false);
|
|
46
125
|
const lastScrollTopRef = React.useRef(controlledScrollTop !== null && controlledScrollTop !== void 0 ? controlledScrollTop : 0);
|
|
47
126
|
const scrollDiffListRef = React.useRef([0, 0, 0]);
|
|
48
127
|
const scrollTimeoutRef = React.useRef(null);
|
|
49
|
-
//
|
|
128
|
+
// H5:仅程序性滚动时写回 DOM scrollTop,用户滑动结束后不写回避免卡顿
|
|
129
|
+
const programmaticScrollRef = React.useRef(false);
|
|
130
|
+
// 小程序端程序性滚动冷却期:handleScroll 只更新 renderOffset,避免 scrollIntoView 后被原生回调拉回
|
|
131
|
+
const programmaticCooldownRef = React.useRef(false);
|
|
132
|
+
const programmaticCooldownTimerRef = React.useRef(null);
|
|
133
|
+
const scrollViewOffsetRef = React.useRef(0);
|
|
134
|
+
// 处理渲染偏移量更新。
|
|
135
|
+
// syncToScrollView=true:程序性滚动,立即同步 scrollViewOffset。
|
|
136
|
+
// syncToScrollView=false:用户滑动,仅更新 renderOffset。weapp 采用 recycle-view 策略:不把用户滑动位置同步到 scrollViewOffset,避免「传滞后值拉回」和「从有到无归顶」。
|
|
137
|
+
const updateRenderOffset = React.useCallback((newOffset, syncToScrollView, source) => {
|
|
138
|
+
lastScrollTopRef.current = newOffset;
|
|
139
|
+
isScrollingRef.current = true;
|
|
140
|
+
if (scrollTimeoutRef.current) {
|
|
141
|
+
clearTimeout(scrollTimeoutRef.current);
|
|
142
|
+
}
|
|
143
|
+
setRenderOffset(newOffset); // 始终更新虚拟列表用到的偏移
|
|
144
|
+
if (syncToScrollView) {
|
|
145
|
+
isUserScrollingRef.current = false;
|
|
146
|
+
setIsUserScrolling(false);
|
|
147
|
+
// 小程序:target===sv 时 ScrollView 认为无变化不滚动→白屏;imperative/scrollIntoView 时先传中间值再 RAF 传 target 强制触发
|
|
148
|
+
// target=0 用 +0.01;target>0 用 -0.01(统一 +0.01 到底部会被 clamp 成同值无效)
|
|
149
|
+
const same = isWeapp && Math.abs(scrollViewOffsetRef.current - newOffset) < 1;
|
|
150
|
+
const needForce = same && (source === 'imperative' || source === 'scrollIntoView');
|
|
151
|
+
if (needForce) {
|
|
152
|
+
const intermediate = newOffset > 0 ? newOffset - 0.01 : 0.01;
|
|
153
|
+
setScrollViewOffset(intermediate);
|
|
154
|
+
requestAnimationFrame(() => {
|
|
155
|
+
setScrollViewOffset(newOffset);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
setScrollViewOffset(newOffset);
|
|
160
|
+
}
|
|
161
|
+
programmaticScrollRef.current = true;
|
|
162
|
+
if (isWeapp) {
|
|
163
|
+
programmaticCooldownRef.current = true;
|
|
164
|
+
if (programmaticCooldownTimerRef.current) {
|
|
165
|
+
clearTimeout(programmaticCooldownTimerRef.current);
|
|
166
|
+
}
|
|
167
|
+
programmaticCooldownTimerRef.current = setTimeout(() => {
|
|
168
|
+
programmaticCooldownRef.current = false;
|
|
169
|
+
}, 500);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
isUserScrollingRef.current = true;
|
|
174
|
+
setIsUserScrolling(true);
|
|
175
|
+
}
|
|
176
|
+
scrollTimeoutRef.current = setTimeout(() => {
|
|
177
|
+
isScrollingRef.current = false;
|
|
178
|
+
if (!syncToScrollView) {
|
|
179
|
+
isUserScrollingRef.current = false;
|
|
180
|
+
setIsUserScrolling(false);
|
|
181
|
+
// weapp recycle-view 策略:用户滑动结束后不同步 scrollViewOffset,保持 pass 的值不变,避免 从有到无 归顶
|
|
182
|
+
if (!isWeapp) {
|
|
183
|
+
setScrollViewOffset(lastScrollTopRef.current);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}, isWeapp ? 200 : 150);
|
|
187
|
+
}, []);
|
|
188
|
+
// 暴露给外部的实例方法:通过 ref.scroll({ top / left }) 进行程序性滚动
|
|
189
|
+
React.useImperativeHandle(ref, () => ({
|
|
190
|
+
scroll(options) {
|
|
191
|
+
const targetOffset = resolveScrollTargetOffset(options, isHorizontal);
|
|
192
|
+
const el = effectiveScrollElement === null || effectiveScrollElement === void 0 ? void 0 : effectiveScrollElement.current;
|
|
193
|
+
if (el && isH5) {
|
|
194
|
+
const scrollTarget = targetOffset + effectiveStartOffset;
|
|
195
|
+
if (isHorizontal) {
|
|
196
|
+
el.scrollTo({ left: scrollTarget });
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
el.scrollTo({ top: scrollTarget });
|
|
200
|
+
}
|
|
201
|
+
updateRenderOffset(targetOffset, false, 'scrollElement');
|
|
202
|
+
}
|
|
203
|
+
else if (el && isWeapp && useScrollElementMode) {
|
|
204
|
+
// 小程序 scrollElement 模式:需通过 getScrollViewContextNode + node.scrollTo 真正滚动外部 scroll-view
|
|
205
|
+
// updateRenderOffset 必须在 scrollTo 之后调用,否则 getScrollViewContextNode 异步期间会先更新 renderOffset 导致闪一下
|
|
206
|
+
const startOff = effectiveStartOffsetRef.current;
|
|
207
|
+
const scrollTarget = targetOffset + startOff;
|
|
208
|
+
const scrollViewId = el.id || `_ls_${listId}`;
|
|
209
|
+
if (!el.id)
|
|
210
|
+
el.id = scrollViewId;
|
|
211
|
+
getScrollViewContextNode(`#${scrollViewId}`).then((node) => {
|
|
212
|
+
var _a, _b;
|
|
213
|
+
if (isHorizontal) {
|
|
214
|
+
(_a = node === null || node === void 0 ? void 0 : node.scrollTo) === null || _a === void 0 ? void 0 : _a.call(node, { left: scrollTarget, animated: false });
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
(_b = node === null || node === void 0 ? void 0 : node.scrollTo) === null || _b === void 0 ? void 0 : _b.call(node, { top: scrollTarget, animated: false });
|
|
218
|
+
}
|
|
219
|
+
updateRenderOffset(targetOffset, true, 'imperative');
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
updateRenderOffset(targetOffset, true, 'imperative');
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
}), [scrollX, effectiveScrollElement, effectiveStartOffset, effectiveStartOffsetRef, isH5, isWeapp, isHorizontal, listId, useScrollElementMode, updateRenderOffset]);
|
|
227
|
+
// 提取 Refresher 配置(List 属性为 base,Refresher 子组件覆盖)
|
|
228
|
+
const refresherConfig = React.useMemo(() => {
|
|
229
|
+
const listRefresherEnabled = props.refresherEnabled !== false && (props.refresherEnabled === true || props.onRefresherRefresh != null);
|
|
230
|
+
const baseFromList = listRefresherEnabled
|
|
231
|
+
? {
|
|
232
|
+
refresherEnabled: props.refresherEnabled,
|
|
233
|
+
refresherThreshold: props.refresherThreshold,
|
|
234
|
+
refresherDefaultStyle: props.refresherDefaultStyle,
|
|
235
|
+
refresherBackground: props.refresherBackground,
|
|
236
|
+
refresherTriggered: props.refresherTriggered,
|
|
237
|
+
onRefresherPulling: props.onRefresherPulling,
|
|
238
|
+
onRefresherRefresh: props.onRefresherRefresh,
|
|
239
|
+
onRefresherRestore: props.onRefresherRestore,
|
|
240
|
+
onRefresherAbort: props.onRefresherAbort,
|
|
241
|
+
onRefresherWillRefresh: props.onRefresherWillRefresh,
|
|
242
|
+
onRefresherStatusChange: props.onRefresherStatusChange,
|
|
243
|
+
}
|
|
244
|
+
: null;
|
|
245
|
+
const isRefresherComponent = (child) => {
|
|
246
|
+
const type = child.type;
|
|
247
|
+
return (type === null || type === void 0 ? void 0 : type.displayName) === 'Refresher' || (type === null || type === void 0 ? void 0 : type.name) === 'Refresher';
|
|
248
|
+
};
|
|
249
|
+
let refresherChildProps = null;
|
|
250
|
+
let refresherCount = 0;
|
|
251
|
+
React.Children.forEach(children, (child) => {
|
|
252
|
+
if (React.isValidElement(child) && isRefresherComponent(child)) {
|
|
253
|
+
refresherCount++;
|
|
254
|
+
if (refresherCount > 1) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
refresherChildProps = child.props;
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
if (refresherChildProps != null) {
|
|
261
|
+
const base = baseFromList !== null && baseFromList !== void 0 ? baseFromList : {};
|
|
262
|
+
// Refresher 子组件的配置覆盖 List 的配置
|
|
263
|
+
return Object.assign(Object.assign({}, base), refresherChildProps);
|
|
264
|
+
}
|
|
265
|
+
return baseFromList;
|
|
266
|
+
}, [
|
|
267
|
+
children,
|
|
268
|
+
props.refresherEnabled,
|
|
269
|
+
props.refresherThreshold,
|
|
270
|
+
props.refresherDefaultStyle,
|
|
271
|
+
props.refresherBackground,
|
|
272
|
+
props.refresherTriggered,
|
|
273
|
+
props.onRefresherPulling,
|
|
274
|
+
props.onRefresherRefresh,
|
|
275
|
+
props.onRefresherRestore,
|
|
276
|
+
props.onRefresherAbort,
|
|
277
|
+
props.onRefresherWillRefresh,
|
|
278
|
+
props.onRefresherStatusChange,
|
|
279
|
+
]);
|
|
280
|
+
// 提取 NoMore 配置
|
|
281
|
+
const noMoreConfig = React.useMemo(() => {
|
|
282
|
+
var _a;
|
|
283
|
+
let config = null;
|
|
284
|
+
let noMoreCount = 0;
|
|
285
|
+
// 从子组件中提取 NoMore
|
|
286
|
+
React.Children.forEach(children, (child) => {
|
|
287
|
+
if (React.isValidElement(child) && child.type === NoMore) {
|
|
288
|
+
noMoreCount++;
|
|
289
|
+
if (noMoreCount > 1) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const childProps = child.props;
|
|
293
|
+
config = Object.assign(Object.assign({}, childProps), { visible: childProps.visible !== false });
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
// Props 方式转换为配置(优先级低于子组件)
|
|
297
|
+
if (props.showNoMore && !config) {
|
|
298
|
+
config = {
|
|
299
|
+
visible: true,
|
|
300
|
+
text: props.noMoreText,
|
|
301
|
+
style: props.noMoreStyle,
|
|
302
|
+
children: (_a = props.renderNoMore) === null || _a === void 0 ? void 0 : _a.call(props)
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
return config;
|
|
306
|
+
}, [children, props.showNoMore, props.noMoreText, props.noMoreStyle, props.renderNoMore]);
|
|
307
|
+
// Refresher 平台适配:H5 用 addImperativeTouchListeners
|
|
308
|
+
const { scrollViewRefresherProps, scrollViewRefresherHandlers, h5RefresherProps, addImperativeTouchListeners, renderRefresherContent, } = useRefresher(refresherConfig, isH5 && refresherConfig && !supportsNativeRefresher ? 0 : renderOffset, useScrollElementMode
|
|
309
|
+
? (ev) => { var _a, _b; return (_b = (ev.target != null && ((_a = contentWrapperRef.current) === null || _a === void 0 ? void 0 : _a.contains(ev.target)))) !== null && _b !== void 0 ? _b : false; }
|
|
310
|
+
: undefined);
|
|
311
|
+
const refresherTeardownRef = React.useRef(null);
|
|
312
|
+
const addImperativeTouchListenersRef = React.useRef(addImperativeTouchListeners);
|
|
313
|
+
addImperativeTouchListenersRef.current = addImperativeTouchListeners;
|
|
314
|
+
React.useEffect(() => () => {
|
|
315
|
+
if (refresherTeardownRef.current)
|
|
316
|
+
refresherTeardownRef.current();
|
|
317
|
+
}, []);
|
|
318
|
+
// H5 下拉刷新顶栏高度(默认 50px,来自 useRefresher)
|
|
319
|
+
const refresherHeightForH5 = (isH5 && refresherConfig && !supportsNativeRefresher && refresherConfig.refresherEnabled !== false) ? DEFAULT_REFRESHER_HEIGHT : 0;
|
|
320
|
+
// 解析分组结构:StickySection、ListItem 为直接子组件,过滤 Refresher/NoMore
|
|
50
321
|
const sections = React.useMemo(() => {
|
|
51
322
|
const result = [];
|
|
52
323
|
const defaultItems = [];
|
|
53
324
|
React.Children.forEach(children, (child, idx) => {
|
|
54
325
|
if (React.isValidElement(child) && child.type === StickySection) {
|
|
55
|
-
// 分组模式
|
|
56
326
|
const sectionProps = child.props;
|
|
57
327
|
let header = null;
|
|
58
328
|
const items = [];
|
|
@@ -65,7 +335,6 @@ const List = (props) => {
|
|
|
65
335
|
result.push({ header, items, key: child.key || String(idx) });
|
|
66
336
|
}
|
|
67
337
|
else if (React.isValidElement(child) && child.type === ListItem) {
|
|
68
|
-
// 普通 ListItem
|
|
69
338
|
defaultItems.push(child);
|
|
70
339
|
}
|
|
71
340
|
});
|
|
@@ -74,65 +343,159 @@ const List = (props) => {
|
|
|
74
343
|
}
|
|
75
344
|
return result;
|
|
76
345
|
}, [children]);
|
|
77
|
-
//
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
346
|
+
// 动态尺寸管理
|
|
347
|
+
const estimatedSize = resolveItemSizeByIndex(0, defaultItemSize);
|
|
348
|
+
// 计算总 item 数量(跨所有 section)
|
|
349
|
+
const totalItemCount = React.useMemo(() => {
|
|
350
|
+
return sections.reduce((sum, section) => sum + section.items.length, 0);
|
|
351
|
+
}, [sections]);
|
|
352
|
+
// 存储元素引用(用于 ResizeObserver)
|
|
353
|
+
const itemRefsRef = React.useRef(new Map());
|
|
354
|
+
// 存储 header 元素引用(用于 ResizeObserver)
|
|
355
|
+
const headerRefsRef = React.useRef(new Map());
|
|
356
|
+
// 小程序端:已完成 SelectorQuery 测量的 item 索引集,避免 refCallback 重复触发测量导致无限循环
|
|
357
|
+
const weappMeasuredItemsRef = React.useRef(new Set());
|
|
358
|
+
// 动态尺寸缓存更新版本:setItemSize 后递增,用于驱动 sectionOffsets/totalLength 与 item 定位重算
|
|
359
|
+
const [sizeCacheVersion, setSizeCacheVersion] = React.useState(0);
|
|
360
|
+
const sizeCacheRafRef = React.useRef(null);
|
|
361
|
+
// 小程序端:测量触发 re-render 时,reflow 帧传 eff 并同步 scrollViewOffset,避免下一帧传 0 导致跳变
|
|
362
|
+
const measureScrollProtectRef = React.useRef(false);
|
|
363
|
+
// 动态尺寸缓存
|
|
364
|
+
const sizeCache = useItemSizeCache({
|
|
365
|
+
isHorizontal,
|
|
366
|
+
estimatedSize,
|
|
367
|
+
itemCount: totalItemCount
|
|
368
|
+
});
|
|
369
|
+
// header 动态尺寸缓存(sectionIndex -> size)
|
|
370
|
+
const headerSizeCacheRef = React.useRef(new Map());
|
|
371
|
+
// 滚动修正的可见起始索引
|
|
372
|
+
const visibleStartIndexRef = React.useRef(0);
|
|
373
|
+
// ScrollTop 修正(仅 H5):动高时尺寸变化自动修正 scrollTop
|
|
374
|
+
const scrollCorrectionEnabled = !isWeapp && props.useResizeObserver === true;
|
|
375
|
+
const scrollCorrection = useScrollCorrection({
|
|
376
|
+
enabled: scrollCorrectionEnabled,
|
|
377
|
+
visibleStartIndexRef,
|
|
378
|
+
setScrollOffset: (offsetOrUpdater) => {
|
|
379
|
+
const newOffset = typeof offsetOrUpdater === 'function'
|
|
380
|
+
? offsetOrUpdater(renderOffset)
|
|
381
|
+
: offsetOrUpdater;
|
|
382
|
+
updateRenderOffset(newOffset, true, 'scrollCorrection'); // 程序性修正需同步到 ScrollView
|
|
89
383
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
384
|
+
});
|
|
385
|
+
const scrollCorrectionRef = React.useRef(scrollCorrection);
|
|
386
|
+
scrollCorrectionRef.current = scrollCorrection;
|
|
387
|
+
const onScrollRef = React.useRef(onScroll);
|
|
388
|
+
onScrollRef.current = onScroll;
|
|
389
|
+
const onScrollToUpperRef = React.useRef(onScrollToUpper);
|
|
390
|
+
onScrollToUpperRef.current = onScrollToUpper;
|
|
391
|
+
const onScrollToLowerRef = React.useRef(onScrollToLower);
|
|
392
|
+
onScrollToLowerRef.current = onScrollToLower;
|
|
393
|
+
const thresholdRef = React.useRef({ upper: upperThreshold, lower: lowerThreshold });
|
|
394
|
+
thresholdRef.current = { upper: upperThreshold, lower: lowerThreshold };
|
|
395
|
+
const listContentLengthRef = React.useRef(0);
|
|
396
|
+
const inUpperZoneRef = React.useRef(true);
|
|
397
|
+
const inLowerZoneRef = React.useRef(false);
|
|
398
|
+
// 小程序 + 动高(virtual-list 风格):测量变化后同帧重排,不做程序性 scrollTop 回拉
|
|
399
|
+
const scheduleWeappDynamicReflow = React.useCallback(() => {
|
|
400
|
+
if (!isWeapp || props.useResizeObserver !== true)
|
|
401
|
+
return;
|
|
402
|
+
if (sizeCacheRafRef.current != null)
|
|
403
|
+
return;
|
|
404
|
+
sizeCacheRafRef.current = requestAnimationFrame(() => {
|
|
405
|
+
sizeCacheRafRef.current = null;
|
|
406
|
+
measureScrollProtectRef.current = true;
|
|
407
|
+
setSizeCacheVersion((v) => v + 1);
|
|
408
|
+
});
|
|
409
|
+
}, [isWeapp, props.useResizeObserver]);
|
|
410
|
+
// ResizeObserver(当启用动态测量时)
|
|
411
|
+
const resizeObserver = useResizeObserver({
|
|
412
|
+
enabled: props.useResizeObserver === true,
|
|
413
|
+
isHorizontal,
|
|
414
|
+
listId,
|
|
415
|
+
onResize: (index, size) => {
|
|
416
|
+
var _a;
|
|
417
|
+
const oldSize = sizeCache.getItemSize(index);
|
|
418
|
+
sizeCache.setItemSize(index, size);
|
|
419
|
+
if (Math.abs(oldSize - size) >= 1) {
|
|
420
|
+
if (isWeapp && props.useResizeObserver === true) {
|
|
421
|
+
scheduleWeappDynamicReflow();
|
|
422
|
+
}
|
|
423
|
+
else if (sizeCacheRafRef.current == null) {
|
|
424
|
+
sizeCacheRafRef.current = requestAnimationFrame(() => {
|
|
425
|
+
sizeCacheRafRef.current = null;
|
|
426
|
+
setSizeCacheVersion((v) => v + 1);
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
// 触发 ScrollTop 修正
|
|
431
|
+
scrollCorrection.recordSizeChange(index, oldSize, size);
|
|
432
|
+
// 小程序:延迟 onItemSizeChange,避免父组件重渲染导致 List remount
|
|
433
|
+
// H5:直接回调
|
|
434
|
+
if (isWeapp) {
|
|
435
|
+
weappDeferItemSizeChange(index, size, props.onItemSizeChange);
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
(_a = props.onItemSizeChange) === null || _a === void 0 ? void 0 : _a.call(props, index, size);
|
|
439
|
+
}
|
|
100
440
|
}
|
|
101
|
-
};
|
|
102
|
-
//
|
|
103
|
-
const
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
441
|
+
});
|
|
442
|
+
// 嵌套滚动:内层高度变化上报
|
|
443
|
+
const handleReportNestedHeightChange = React.useCallback((height, index) => {
|
|
444
|
+
if (height <= 0)
|
|
445
|
+
return;
|
|
446
|
+
const estimatedHeader = estimatedSize * 0.5;
|
|
447
|
+
const fullHeight = height + estimatedHeader;
|
|
448
|
+
const oldSize = sizeCache.getItemSize(index);
|
|
449
|
+
if (Math.abs(oldSize - fullHeight) < 1)
|
|
450
|
+
return;
|
|
451
|
+
sizeCache.setItemSize(index, fullHeight);
|
|
452
|
+
scrollCorrection.recordSizeChange(index, oldSize, fullHeight);
|
|
453
|
+
if (isWeapp && props.useResizeObserver === true) {
|
|
454
|
+
scheduleWeappDynamicReflow();
|
|
112
455
|
}
|
|
113
|
-
else {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (typeof props.itemSize === 'function')
|
|
119
|
-
return props.itemSize(index, props.itemData) || DEFAULT_ITEM_HEIGHT;
|
|
120
|
-
return DEFAULT_ITEM_HEIGHT;
|
|
456
|
+
else if (sizeCacheRafRef.current == null) {
|
|
457
|
+
sizeCacheRafRef.current = requestAnimationFrame(() => {
|
|
458
|
+
sizeCacheRafRef.current = null;
|
|
459
|
+
setSizeCacheVersion((v) => v + 1);
|
|
460
|
+
});
|
|
121
461
|
}
|
|
122
|
-
};
|
|
123
|
-
|
|
462
|
+
}, [sizeCache, scrollCorrection, estimatedSize, isWeapp, props.useResizeObserver, scheduleWeappDynamicReflow]);
|
|
463
|
+
const getDefaultHeaderSize = React.useCallback(() => {
|
|
464
|
+
const headerSize = normalizeSize(props.headerSize);
|
|
465
|
+
if (headerSize != null)
|
|
466
|
+
return headerSize;
|
|
467
|
+
return resolveItemSizeByIndex(0, defaultItemSize);
|
|
468
|
+
}, [props.headerSize, resolveItemSizeByIndex, defaultItemSize, normalizeSize]);
|
|
469
|
+
// 获取 header 尺寸(支持动态测量)
|
|
470
|
+
const getHeaderSize = React.useCallback((sectionIndex) => {
|
|
471
|
+
if (props.useResizeObserver === true) {
|
|
472
|
+
const cached = headerSizeCacheRef.current.get(sectionIndex);
|
|
473
|
+
if (cached != null && cached > 0)
|
|
474
|
+
return cached;
|
|
475
|
+
}
|
|
476
|
+
return getDefaultHeaderSize();
|
|
477
|
+
}, [props.useResizeObserver, getDefaultHeaderSize]);
|
|
478
|
+
const getItemSize = React.useCallback((index) => {
|
|
479
|
+
if (props.useResizeObserver === true) {
|
|
480
|
+
return sizeCache.getItemSize(index);
|
|
481
|
+
}
|
|
482
|
+
return resolveItemSizeByIndex(index, defaultItemSize);
|
|
483
|
+
}, [props.useResizeObserver, sizeCache, resolveItemSizeByIndex, defaultItemSize]);
|
|
484
|
+
// 分组累积高度/宽度,sizeCacheVersion 变化时重算
|
|
124
485
|
const sectionOffsets = React.useMemo(() => {
|
|
125
486
|
const offsets = [0];
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const
|
|
487
|
+
let globalItemIndex = 0;
|
|
488
|
+
sections.forEach((section, sectionIdx) => {
|
|
489
|
+
const headerSize = getHeaderSize(sectionIdx);
|
|
490
|
+
const itemSizes = section.items.map((_, localIdx) => getItemSize(globalItemIndex + localIdx));
|
|
129
491
|
const groupSize = (section.header ? headerSize : 0) +
|
|
130
492
|
itemSizes.reduce((a, b) => a + b, 0) +
|
|
131
493
|
Math.max(0, section.items.length) * space;
|
|
132
494
|
offsets.push(offsets[offsets.length - 1] + groupSize);
|
|
495
|
+
globalItemIndex += section.items.length;
|
|
133
496
|
});
|
|
134
497
|
return offsets;
|
|
135
|
-
}, [sections, space,
|
|
498
|
+
}, [sections, space, getItemSize, getHeaderSize, sizeCacheVersion]);
|
|
136
499
|
// 外层虚拟滚动:可见分组
|
|
137
500
|
const [startSection, endSection] = React.useMemo(() => {
|
|
138
501
|
let start = 0;
|
|
@@ -151,119 +514,342 @@ const List = (props) => {
|
|
|
151
514
|
}
|
|
152
515
|
return [start, end];
|
|
153
516
|
}, [renderOffset, containerLength, sectionOffsets, sections.length, cacheCount]);
|
|
154
|
-
//
|
|
155
|
-
React.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
517
|
+
// 视口内可见 item 的全局索引范围(供 onScrollIndex)
|
|
518
|
+
const [visibleStartItem, visibleEndItem] = React.useMemo(() => {
|
|
519
|
+
const viewportTop = renderOffset;
|
|
520
|
+
const viewportBottom = renderOffset + containerLength;
|
|
521
|
+
let firstVisible = -1;
|
|
522
|
+
let lastVisible = -1;
|
|
523
|
+
let globalIndex = 0;
|
|
524
|
+
for (let s = 0; s < sections.length; s++) {
|
|
525
|
+
const section = sections[s];
|
|
526
|
+
const headerSize = getHeaderSize(s);
|
|
527
|
+
const sectionStart = sectionOffsets[s] + (section.header ? headerSize : 0);
|
|
528
|
+
let itemTop = sectionStart;
|
|
529
|
+
for (let i = 0; i < section.items.length; i++) {
|
|
530
|
+
const itemSize = getItemSize(globalIndex);
|
|
531
|
+
const itemBottom = itemTop + itemSize;
|
|
532
|
+
// 判断 item 本身(不含 space)是否与视口相交
|
|
533
|
+
if (itemBottom > viewportTop && itemTop < viewportBottom) {
|
|
534
|
+
if (firstVisible < 0)
|
|
535
|
+
firstVisible = globalIndex;
|
|
536
|
+
lastVisible = globalIndex;
|
|
537
|
+
}
|
|
538
|
+
// 下一个 item 的起始位置 = 当前 item 结束 + space
|
|
539
|
+
itemTop = itemBottom + space;
|
|
540
|
+
globalIndex++;
|
|
541
|
+
}
|
|
179
542
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
543
|
+
if (firstVisible < 0 || lastVisible < 0)
|
|
544
|
+
return [0, 0];
|
|
545
|
+
return [firstVisible, lastVisible];
|
|
546
|
+
}, [renderOffset, containerLength, sections, sectionOffsets, getHeaderSize, getItemSize, space]);
|
|
547
|
+
const lastVisibleRangeRef = React.useRef({ start: -1, end: -1 });
|
|
548
|
+
React.useEffect(() => {
|
|
549
|
+
if (props.onScrollIndex) {
|
|
550
|
+
if (lastVisibleRangeRef.current.start !== visibleStartItem ||
|
|
551
|
+
lastVisibleRangeRef.current.end !== visibleEndItem) {
|
|
552
|
+
lastVisibleRangeRef.current = { start: visibleStartItem, end: visibleEndItem };
|
|
553
|
+
props.onScrollIndex(visibleStartItem, visibleEndItem);
|
|
554
|
+
}
|
|
186
555
|
}
|
|
187
|
-
}, []);
|
|
188
|
-
// 智能滚动处理函数
|
|
556
|
+
}, [visibleStartItem, visibleEndItem, props.onScrollIndex]);
|
|
189
557
|
const handleScroll = React.useCallback((e) => {
|
|
190
|
-
|
|
558
|
+
var _a, _b, _c, _d;
|
|
191
559
|
let newOffset;
|
|
192
560
|
if (e.detail) {
|
|
193
|
-
// React版本的事件结构
|
|
194
561
|
newOffset = isHorizontal ? e.detail.scrollLeft : e.detail.scrollTop;
|
|
195
562
|
}
|
|
196
563
|
else {
|
|
197
|
-
// Stencil版本的事件结构
|
|
198
564
|
newOffset = isHorizontal ? e.scrollLeft : e.scrollTop;
|
|
199
565
|
}
|
|
200
|
-
const
|
|
566
|
+
const effectiveOffset = newOffset;
|
|
567
|
+
const currentThreshold = thresholdRef.current;
|
|
568
|
+
const { upper, lower } = currentThreshold;
|
|
569
|
+
const currentContainerLength = containerRef.current
|
|
570
|
+
? (isHorizontal ? containerRef.current.clientWidth : containerRef.current.clientHeight)
|
|
571
|
+
: containerLength;
|
|
572
|
+
const nowInUpper = effectiveOffset <= upper;
|
|
573
|
+
const currentContentLength = listContentLengthRef.current;
|
|
574
|
+
const nowInLower = currentContentLength > 0 && effectiveOffset + currentContainerLength >= currentContentLength - lower;
|
|
575
|
+
const diff = effectiveOffset - lastScrollTopRef.current;
|
|
201
576
|
scrollDiffListRef.current.shift();
|
|
202
577
|
scrollDiffListRef.current.push(diff);
|
|
203
|
-
|
|
204
|
-
if (
|
|
578
|
+
const shaking = isScrollingRef.current && isShaking(scrollDiffListRef.current);
|
|
579
|
+
if (shaking)
|
|
580
|
+
return;
|
|
581
|
+
if (programmaticCooldownRef.current) {
|
|
582
|
+
lastScrollTopRef.current = effectiveOffset;
|
|
583
|
+
setRenderOffset(effectiveOffset);
|
|
584
|
+
const scrollTop = isHorizontal ? 0 : newOffset;
|
|
585
|
+
const scrollLeft = isHorizontal ? newOffset : 0;
|
|
586
|
+
onScroll === null || onScroll === void 0 ? void 0 : onScroll({ scrollTop, scrollLeft, detail: { scrollTop, scrollLeft } });
|
|
587
|
+
if (nowInUpper && !inUpperZoneRef.current)
|
|
588
|
+
(_a = onScrollToUpperRef.current) === null || _a === void 0 ? void 0 : _a.call(onScrollToUpperRef);
|
|
589
|
+
if (nowInLower && !inLowerZoneRef.current)
|
|
590
|
+
(_b = onScrollToLowerRef.current) === null || _b === void 0 ? void 0 : _b.call(onScrollToLowerRef);
|
|
591
|
+
inUpperZoneRef.current = nowInUpper;
|
|
592
|
+
inLowerZoneRef.current = nowInLower;
|
|
205
593
|
return;
|
|
206
594
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
|
|
595
|
+
scrollCorrection.markUserScrolling();
|
|
596
|
+
updateRenderOffset(effectiveOffset, false, 'onScroll');
|
|
597
|
+
const scrollTop = isHorizontal ? 0 : newOffset;
|
|
598
|
+
const scrollLeft = isHorizontal ? newOffset : 0;
|
|
599
|
+
onScroll === null || onScroll === void 0 ? void 0 : onScroll({ scrollTop, scrollLeft, detail: { scrollTop, scrollLeft } });
|
|
600
|
+
if (nowInUpper && !inUpperZoneRef.current)
|
|
601
|
+
(_c = onScrollToUpperRef.current) === null || _c === void 0 ? void 0 : _c.call(onScrollToUpperRef);
|
|
602
|
+
if (nowInLower && !inLowerZoneRef.current)
|
|
603
|
+
(_d = onScrollToLowerRef.current) === null || _d === void 0 ? void 0 : _d.call(onScrollToLowerRef);
|
|
604
|
+
inUpperZoneRef.current = nowInUpper;
|
|
605
|
+
inLowerZoneRef.current = nowInLower;
|
|
606
|
+
}, [isHorizontal, onScroll, updateRenderOffset, scrollCorrection, props.useResizeObserver, containerLength]);
|
|
607
|
+
// 小程序:onScrollEnd 优先结束 isUserScrolling,timeout 兜底
|
|
608
|
+
const handleNativeScrollEnd = React.useCallback(() => {
|
|
609
|
+
if (isWeapp) {
|
|
610
|
+
if (scrollTimeoutRef.current) {
|
|
611
|
+
clearTimeout(scrollTimeoutRef.current);
|
|
612
|
+
scrollTimeoutRef.current = null;
|
|
613
|
+
}
|
|
614
|
+
if (isUserScrollingRef.current) {
|
|
615
|
+
isScrollingRef.current = false;
|
|
616
|
+
isUserScrollingRef.current = false;
|
|
617
|
+
setIsUserScrolling(false);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
onScrollEnd === null || onScrollEnd === void 0 ? void 0 : onScrollEnd();
|
|
621
|
+
}, [isWeapp, onScrollEnd]);
|
|
622
|
+
// 暴露 scrollRef 给父组件,供内层 List/WaterFlow 传入 scrollElement
|
|
623
|
+
React.useLayoutEffect(() => {
|
|
624
|
+
if (!scrollRefProp)
|
|
625
|
+
return;
|
|
626
|
+
const el = useScrollElementMode ? effectiveScrollElement === null || effectiveScrollElement === void 0 ? void 0 : effectiveScrollElement.current : containerRef.current;
|
|
627
|
+
if (el) {
|
|
628
|
+
scrollRefProp.current = el;
|
|
629
|
+
}
|
|
630
|
+
}, [scrollRefProp, useScrollElementMode, effectiveScrollElement]);
|
|
631
|
+
// controlledScrollTop 变化时同步到 ScrollView
|
|
214
632
|
React.useEffect(() => {
|
|
215
633
|
if (typeof controlledScrollTop === 'number') {
|
|
216
|
-
|
|
634
|
+
const sv = scrollViewOffsetRef.current;
|
|
635
|
+
const same = isWeapp && Math.abs(sv - controlledScrollTop) < 1;
|
|
636
|
+
if (same) {
|
|
637
|
+
const intermediate = controlledScrollTop > 0 ? controlledScrollTop - 0.01 : 0.01;
|
|
638
|
+
setRenderOffset(intermediate);
|
|
639
|
+
setScrollViewOffset(intermediate);
|
|
640
|
+
requestAnimationFrame(() => {
|
|
641
|
+
setRenderOffset(controlledScrollTop);
|
|
642
|
+
setScrollViewOffset(controlledScrollTop);
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
else {
|
|
646
|
+
setRenderOffset(controlledScrollTop);
|
|
647
|
+
setScrollViewOffset(controlledScrollTop);
|
|
648
|
+
}
|
|
217
649
|
lastScrollTopRef.current = controlledScrollTop;
|
|
650
|
+
programmaticScrollRef.current = true;
|
|
218
651
|
}
|
|
219
652
|
}, [controlledScrollTop]);
|
|
220
|
-
//
|
|
653
|
+
// 清理定时器、ResizeObserver 与尺寸缓存 RAF(仅在卸载时执行)
|
|
221
654
|
React.useEffect(() => {
|
|
222
655
|
return () => {
|
|
223
656
|
if (scrollTimeoutRef.current) {
|
|
224
657
|
clearTimeout(scrollTimeoutRef.current);
|
|
225
658
|
}
|
|
659
|
+
// 小程序端冷却期定时器清理
|
|
660
|
+
if (isWeapp && programmaticCooldownTimerRef.current) {
|
|
661
|
+
clearTimeout(programmaticCooldownTimerRef.current);
|
|
662
|
+
}
|
|
663
|
+
if (sizeCacheRafRef.current != null) {
|
|
664
|
+
cancelAnimationFrame(sizeCacheRafRef.current);
|
|
665
|
+
sizeCacheRafRef.current = null;
|
|
666
|
+
}
|
|
667
|
+
resizeObserver.disconnect();
|
|
668
|
+
scrollCorrection.clearQueue();
|
|
226
669
|
};
|
|
227
670
|
}, []);
|
|
228
|
-
//
|
|
229
|
-
const
|
|
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
|
-
|
|
671
|
+
// scrollIntoView:仅当 scrollIntoView 变化时执行一次跳动,统一转换为 scrollTop 路径(updateRenderOffset)。
|
|
672
|
+
const lastScrollIntoViewRef = React.useRef(null);
|
|
673
|
+
React.useEffect(() => {
|
|
674
|
+
const targetId = props.scrollIntoView;
|
|
675
|
+
if (!targetId) {
|
|
676
|
+
lastScrollIntoViewRef.current = null;
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (lastScrollIntoViewRef.current === targetId)
|
|
680
|
+
return;
|
|
681
|
+
lastScrollIntoViewRef.current = targetId;
|
|
682
|
+
let targetIndex = -1;
|
|
683
|
+
if (targetId.startsWith('list-item-')) {
|
|
684
|
+
targetIndex = parseInt(targetId.replace('list-item-', ''), 10);
|
|
685
|
+
}
|
|
686
|
+
if (targetIndex >= 0 && targetIndex < totalItemCount) {
|
|
687
|
+
let targetOffset = 0;
|
|
688
|
+
let currentGlobalIndex = 0;
|
|
689
|
+
for (let s = 0; s < sections.length; s++) {
|
|
690
|
+
const section = sections[s];
|
|
691
|
+
const headerSize = section.header ? getHeaderSize(s) : 0;
|
|
692
|
+
if (currentGlobalIndex + section.items.length > targetIndex) {
|
|
693
|
+
targetOffset += headerSize;
|
|
694
|
+
for (let i = 0; i < targetIndex - currentGlobalIndex; i++) {
|
|
695
|
+
targetOffset += getItemSize(currentGlobalIndex + i) + space;
|
|
696
|
+
}
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
else {
|
|
700
|
+
targetOffset += headerSize;
|
|
701
|
+
for (let i = 0; i < section.items.length; i++) {
|
|
702
|
+
targetOffset += getItemSize(currentGlobalIndex + i) + space;
|
|
703
|
+
}
|
|
704
|
+
currentGlobalIndex += section.items.length;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
const el = effectiveScrollElement === null || effectiveScrollElement === void 0 ? void 0 : effectiveScrollElement.current;
|
|
708
|
+
if (useScrollElementMode && el) {
|
|
709
|
+
const scrollTarget = targetOffset + (isWeapp ? effectiveStartOffsetRef.current : effectiveStartOffset);
|
|
710
|
+
if (isH5) {
|
|
711
|
+
if (isHorizontal) {
|
|
712
|
+
el.scrollTo({ left: scrollTarget });
|
|
713
|
+
}
|
|
714
|
+
else {
|
|
715
|
+
el.scrollTo({ top: scrollTarget });
|
|
716
|
+
}
|
|
717
|
+
updateRenderOffset(targetOffset, true, 'scrollIntoView');
|
|
718
|
+
}
|
|
719
|
+
else if (isWeapp) {
|
|
720
|
+
const scrollViewId = el.id || `_ls_${listId}`;
|
|
721
|
+
if (!el.id)
|
|
722
|
+
el.id = scrollViewId;
|
|
723
|
+
getScrollViewContextNode(`#${scrollViewId}`).then((node) => {
|
|
724
|
+
var _a, _b;
|
|
725
|
+
if (isHorizontal) {
|
|
726
|
+
(_a = node === null || node === void 0 ? void 0 : node.scrollTo) === null || _a === void 0 ? void 0 : _a.call(node, { left: scrollTarget, animated: false });
|
|
727
|
+
}
|
|
728
|
+
else {
|
|
729
|
+
(_b = node === null || node === void 0 ? void 0 : node.scrollTo) === null || _b === void 0 ? void 0 : _b.call(node, { top: scrollTarget, animated: false });
|
|
730
|
+
}
|
|
731
|
+
updateRenderOffset(targetOffset, true, 'scrollIntoView');
|
|
732
|
+
});
|
|
258
733
|
}
|
|
259
734
|
else {
|
|
260
|
-
|
|
735
|
+
updateRenderOffset(targetOffset, true, 'scrollIntoView');
|
|
261
736
|
}
|
|
262
737
|
}
|
|
263
|
-
|
|
738
|
+
else {
|
|
739
|
+
updateRenderOffset(targetOffset, true, 'scrollIntoView');
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}, [props.scrollIntoView, totalItemCount, sections, getHeaderSize, getItemSize, space, updateRenderOffset, useScrollElementMode, effectiveScrollElement, effectiveStartOffset, effectiveStartOffsetRef, isH5, isWeapp, isHorizontal, listId]);
|
|
743
|
+
// 容器样式;H5 刷新中禁止滚动
|
|
744
|
+
const containerStyle = Object.assign(Object.assign({ position: 'relative', boxSizing: 'border-box', height,
|
|
745
|
+
width }, style), (isH5 && refresherConfig && !supportsNativeRefresher && h5RefresherProps.isRefreshing
|
|
746
|
+
? { overflow: 'hidden' }
|
|
747
|
+
: {}));
|
|
748
|
+
// ScrollView 属性
|
|
749
|
+
const scrollViewProps = Object.assign(Object.assign(Object.assign(Object.assign({ scrollY: !scrollX && scrollY, scrollX, style: containerStyle, className, enhanced: true, showScrollbar,
|
|
750
|
+
upperThreshold,
|
|
751
|
+
lowerThreshold, scrollWithAnimation: false, onScroll: handleScroll, onScrollStart, onScrollEnd: handleNativeScrollEnd, enableBackToTop }, (isWeapp ? { scrollAnchoring: true } : {})), (typeof cacheExtent === 'number' ? { cacheExtent } : {})), scrollViewRefresherProps), scrollViewRefresherHandlers);
|
|
752
|
+
// H5 对齐小程序:refresherTriggered=true 时顶部立即显示加载指示器,滚到顶部并锁定滚动直至设为 false
|
|
753
|
+
React.useEffect(() => {
|
|
754
|
+
if (!isH5 || !refresherConfig || supportsNativeRefresher || !h5RefresherProps.isRefreshing)
|
|
755
|
+
return;
|
|
756
|
+
updateRenderOffset(0, true, 'refresher');
|
|
757
|
+
}, [h5RefresherProps.isRefreshing, isH5, refresherConfig, supportsNativeRefresher, updateRenderOffset]);
|
|
758
|
+
// H5 下拉刷新:ref 存 addImperativeTouchListeners,避免 config 变化导致 effect 循环
|
|
759
|
+
React.useLayoutEffect(() => {
|
|
760
|
+
const attach = addImperativeTouchListenersRef.current;
|
|
761
|
+
if (!attach)
|
|
762
|
+
return;
|
|
763
|
+
let teardown = null;
|
|
764
|
+
const tryAttach = () => {
|
|
765
|
+
const el = useScrollElementMode ? effectiveScrollElement === null || effectiveScrollElement === void 0 ? void 0 : effectiveScrollElement.current : containerRef.current;
|
|
766
|
+
if (el && !refresherTeardownRef.current) {
|
|
767
|
+
teardown = attach(el);
|
|
768
|
+
refresherTeardownRef.current = teardown;
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
tryAttach();
|
|
772
|
+
const rafId = requestAnimationFrame(tryAttach);
|
|
773
|
+
return () => {
|
|
774
|
+
cancelAnimationFrame(rafId);
|
|
775
|
+
if (teardown) {
|
|
776
|
+
teardown();
|
|
777
|
+
}
|
|
778
|
+
refresherTeardownRef.current = null;
|
|
779
|
+
};
|
|
780
|
+
}, [useScrollElementMode, effectiveScrollElement]);
|
|
781
|
+
scrollViewOffsetRef.current = scrollViewOffset;
|
|
782
|
+
// scrollTop:weapp 传 scrollViewOffset(reflow 帧传 eff 防跳变);H5 仅非用户滑动时传
|
|
783
|
+
if (isWeapp) {
|
|
784
|
+
if (measureScrollProtectRef.current) {
|
|
785
|
+
measureScrollProtectRef.current = false;
|
|
786
|
+
const eff = lastScrollTopRef.current;
|
|
787
|
+
setScrollViewOffset(eff);
|
|
788
|
+
if (isHorizontal) {
|
|
789
|
+
scrollViewProps.scrollLeft = eff;
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
scrollViewProps.scrollTop = eff;
|
|
793
|
+
}
|
|
794
|
+
programmaticScrollRef.current = false;
|
|
795
|
+
}
|
|
796
|
+
else {
|
|
797
|
+
const sv = scrollViewOffset;
|
|
798
|
+
if (isHorizontal) {
|
|
799
|
+
scrollViewProps.scrollLeft = sv;
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
scrollViewProps.scrollTop = sv;
|
|
803
|
+
}
|
|
804
|
+
programmaticScrollRef.current = false;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
// H5:非用户滑动时传
|
|
809
|
+
if (!isUserScrolling) {
|
|
810
|
+
if (isHorizontal) {
|
|
811
|
+
scrollViewProps.scrollLeft = scrollViewOffset;
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
scrollViewProps.scrollTop = scrollViewOffset;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
264
817
|
}
|
|
265
|
-
//
|
|
266
|
-
|
|
818
|
+
// H5:程序性滚动时写回 DOM scrollTop
|
|
819
|
+
React.useEffect(() => {
|
|
820
|
+
if (!isH5)
|
|
821
|
+
return;
|
|
822
|
+
if (isUserScrolling || !containerRef.current || typeof scrollViewOffset !== 'number')
|
|
823
|
+
return;
|
|
824
|
+
if (!programmaticScrollRef.current)
|
|
825
|
+
return;
|
|
826
|
+
const scrollValue = scrollViewOffset;
|
|
827
|
+
if (isHorizontal) {
|
|
828
|
+
containerRef.current.scrollLeft = scrollValue;
|
|
829
|
+
}
|
|
830
|
+
else {
|
|
831
|
+
containerRef.current.scrollTop = scrollValue;
|
|
832
|
+
}
|
|
833
|
+
programmaticScrollRef.current = false;
|
|
834
|
+
}, [isH5, scrollViewOffset, isHorizontal, isUserScrolling]);
|
|
835
|
+
// 总高度/宽度(含 NoMore)
|
|
836
|
+
const noMoreHeight = (noMoreConfig === null || noMoreConfig === void 0 ? void 0 : noMoreConfig.visible) ? (noMoreConfig.height || 60) : 0;
|
|
837
|
+
const listContentLength = sectionOffsets[sectionOffsets.length - 1] + noMoreHeight;
|
|
838
|
+
const totalLength = listContentLength;
|
|
839
|
+
// scrollElement 模式下 onScrollToLower 需用内层内容高度判断,供 scroll handler 读取
|
|
840
|
+
listContentLengthRef.current = listContentLength;
|
|
841
|
+
const scrollAttachRefsRef = React.useRef(null);
|
|
842
|
+
scrollAttachRefsRef.current = {
|
|
843
|
+
scrollCorrection: scrollCorrectionRef.current,
|
|
844
|
+
onScroll: onScrollRef.current,
|
|
845
|
+
onScrollToUpper: onScrollToUpperRef.current,
|
|
846
|
+
onScrollToLower: onScrollToLowerRef.current,
|
|
847
|
+
threshold: thresholdRef.current,
|
|
848
|
+
listContentLength: listContentLengthRef.current,
|
|
849
|
+
};
|
|
850
|
+
useListScrollElementAttach(useScrollElementMode && isH5, effectiveScrollElement, effectiveStartOffset, isHorizontal, setContainerLength, updateRenderOffset, scrollRefProp, scrollAttachRefsRef);
|
|
851
|
+
useListScrollElementAttachWeapp(useScrollElementMode && isWeapp, effectiveScrollElement, effectiveStartOffsetRef, effectiveStartOffset, isHorizontal, setContainerLength, updateRenderOffset, scrollRefProp, scrollAttachRefsRef, initialContainerLength // 兜底:measure 未完成或失败时供 onScrollToUpper/Lower 及 containerLengthRef 使用
|
|
852
|
+
);
|
|
267
853
|
// 吸顶/吸左 header
|
|
268
854
|
const stickyHeaderNode = React.useMemo(() => {
|
|
269
855
|
if (!stickyHeader)
|
|
@@ -272,33 +858,127 @@ const List = (props) => {
|
|
|
272
858
|
if (sectionOffsets[i] <= renderOffset && renderOffset < sectionOffsets[i + 1]) {
|
|
273
859
|
const section = sections[i];
|
|
274
860
|
if (section.header) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
861
|
+
// 吸顶 header 不设固定 height/width,由内容撑开,避免「外部 60px 容器 + 实际内容 40+px」导致 header 内空白
|
|
862
|
+
const stickyHeaderStyle = {
|
|
863
|
+
position: 'sticky',
|
|
864
|
+
top: 0,
|
|
865
|
+
left: 0,
|
|
866
|
+
zIndex: 100,
|
|
867
|
+
background: '#fff',
|
|
868
|
+
boxSizing: 'border-box',
|
|
869
|
+
minHeight: 20,
|
|
870
|
+
overflow: 'hidden',
|
|
871
|
+
lineHeight: 1
|
|
872
|
+
};
|
|
278
873
|
return (jsx(View, { style: stickyHeaderStyle, children: section.header }));
|
|
279
874
|
}
|
|
280
875
|
}
|
|
281
876
|
}
|
|
282
877
|
return null;
|
|
283
|
-
}, [stickyHeader, renderOffset, sectionOffsets, sections
|
|
878
|
+
}, [stickyHeader, renderOffset, sectionOffsets, sections]);
|
|
284
879
|
// 渲染分组+item双层虚拟滚动
|
|
285
880
|
const renderSections = () => {
|
|
286
881
|
const nodes = [];
|
|
287
882
|
let offset = sectionOffsets[startSection];
|
|
883
|
+
let globalItemIndex = 0; // 全局 item 索引(跨 section)
|
|
884
|
+
// 计算起始 section 之前的所有 item 数量
|
|
885
|
+
for (let s = 0; s < startSection; s++) {
|
|
886
|
+
globalItemIndex += sections[s].items.length;
|
|
887
|
+
}
|
|
288
888
|
for (let s = startSection; s <= endSection; s++) {
|
|
289
889
|
const section = sections[s];
|
|
290
|
-
const headerSize = getHeaderSize();
|
|
291
|
-
const itemSizes = section.items.map((_, i) => getItemSize(i));
|
|
890
|
+
const headerSize = getHeaderSize(s);
|
|
891
|
+
const itemSizes = section.items.map((_, i) => getItemSize(globalItemIndex + i));
|
|
292
892
|
// header
|
|
293
893
|
if (section.header) {
|
|
294
|
-
|
|
894
|
+
const sectionIndex = s;
|
|
895
|
+
// 动态测量时外层定位容器不设固定高度,由内层撑开
|
|
295
896
|
const sectionHeaderStyle = Object.assign({ position: 'absolute', zIndex: 2, boxSizing: 'border-box', width: '100%', minHeight: '20px', overflow: 'hidden', lineHeight: 1 }, (isHorizontal
|
|
296
|
-
? { top: 0, height: '100%', left: offset, width: headerSize }
|
|
297
|
-
: { top: offset, height: headerSize }));
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
897
|
+
? { top: 0, height: '100%', left: offset, width: props.useResizeObserver ? undefined : headerSize }
|
|
898
|
+
: { top: offset, height: props.useResizeObserver ? undefined : headerSize }));
|
|
899
|
+
// header ref 回调(用于 ResizeObserver 测量)
|
|
900
|
+
const headerRefCallback = props.useResizeObserver ? (el) => {
|
|
901
|
+
if (el) {
|
|
902
|
+
headerRefsRef.current.set(sectionIndex, el);
|
|
903
|
+
resizeObserver.observe(el, -sectionIndex - 1); // 用负数索引区分 header 和 item
|
|
904
|
+
const measureAndUpdateHeader = (measured) => {
|
|
905
|
+
var _a;
|
|
906
|
+
if (measured > 0) {
|
|
907
|
+
const oldSize = (_a = headerSizeCacheRef.current.get(sectionIndex)) !== null && _a !== void 0 ? _a : getDefaultHeaderSize();
|
|
908
|
+
if (Math.abs(oldSize - measured) >= 1) {
|
|
909
|
+
headerSizeCacheRef.current.set(sectionIndex, measured);
|
|
910
|
+
if (isWeapp && props.useResizeObserver === true) {
|
|
911
|
+
scheduleWeappDynamicReflow();
|
|
912
|
+
}
|
|
913
|
+
else if (sizeCacheRafRef.current == null) {
|
|
914
|
+
sizeCacheRafRef.current = requestAnimationFrame(() => {
|
|
915
|
+
sizeCacheRafRef.current = null;
|
|
916
|
+
setSizeCacheVersion((v) => v + 1);
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
};
|
|
922
|
+
if (isH5) {
|
|
923
|
+
requestAnimationFrame(() => {
|
|
924
|
+
if (!headerRefsRef.current.has(sectionIndex))
|
|
925
|
+
return;
|
|
926
|
+
const rect = el.getBoundingClientRect();
|
|
927
|
+
measureAndUpdateHeader(isHorizontal ? rect.width : rect.height);
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
else if (isWeapp) {
|
|
931
|
+
Taro.nextTick(() => {
|
|
932
|
+
if (!headerRefsRef.current.has(sectionIndex))
|
|
933
|
+
return;
|
|
934
|
+
Taro.createSelectorQuery()
|
|
935
|
+
.select(`#${listId}-list-header-inner-${sectionIndex}`)
|
|
936
|
+
.boundingClientRect((rect) => {
|
|
937
|
+
if (rect) {
|
|
938
|
+
measureAndUpdateHeader(isHorizontal ? rect.width : rect.height);
|
|
939
|
+
}
|
|
940
|
+
})
|
|
941
|
+
.exec();
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
else {
|
|
946
|
+
const oldEl = headerRefsRef.current.get(sectionIndex);
|
|
947
|
+
if (oldEl) {
|
|
948
|
+
resizeObserver.unobserve(oldEl);
|
|
949
|
+
headerRefsRef.current.delete(sectionIndex);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
} : undefined;
|
|
953
|
+
// 动态尺寸时:外层定位,内层撑开以便测量
|
|
954
|
+
const headerContentStyle = props.useResizeObserver
|
|
955
|
+
? (isHorizontal
|
|
956
|
+
// weapp 下 max-content 兼容性不稳定,改用 inline-flex 收缩包裹以测得真实宽度
|
|
957
|
+
? (isWeapp
|
|
958
|
+
? { boxSizing: 'border-box', display: 'inline-flex', height: '100%' }
|
|
959
|
+
: { boxSizing: 'border-box', width: 'max-content', height: '100%' })
|
|
960
|
+
: { boxSizing: 'border-box', width: '100%' })
|
|
961
|
+
: {};
|
|
962
|
+
if (props.useResizeObserver) {
|
|
963
|
+
const headerInnerProps = {
|
|
964
|
+
ref: headerRefCallback,
|
|
965
|
+
style: headerContentStyle,
|
|
966
|
+
'data-index': String(-sectionIndex - 1), // 用于 unobserve 获取 index;与 observe(el, -sectionIndex-1) 对应
|
|
967
|
+
};
|
|
968
|
+
if (isWeapp) {
|
|
969
|
+
headerInnerProps.id = `${listId}-list-header-inner-${sectionIndex}`;
|
|
970
|
+
}
|
|
971
|
+
nodes.push(React.createElement(View, {
|
|
972
|
+
key: section.key + '-header',
|
|
973
|
+
style: sectionHeaderStyle,
|
|
974
|
+
}, React.createElement(View, headerInnerProps, section.header)));
|
|
975
|
+
}
|
|
976
|
+
else {
|
|
977
|
+
nodes.push(React.createElement(View, {
|
|
978
|
+
key: section.key + '-header',
|
|
979
|
+
style: sectionHeaderStyle,
|
|
980
|
+
}, section.header));
|
|
981
|
+
}
|
|
302
982
|
offset += headerSize;
|
|
303
983
|
}
|
|
304
984
|
// item offsets
|
|
@@ -318,9 +998,17 @@ const List = (props) => {
|
|
|
318
998
|
break;
|
|
319
999
|
}
|
|
320
1000
|
}
|
|
1001
|
+
// 更新可见起始索引(用于 ScrollCorrection)
|
|
1002
|
+
if (s === startSection && startItem === 0) {
|
|
1003
|
+
visibleStartIndexRef.current = globalItemIndex;
|
|
1004
|
+
}
|
|
1005
|
+
else if (s === startSection) {
|
|
1006
|
+
visibleStartIndexRef.current = globalItemIndex + startItem;
|
|
1007
|
+
}
|
|
321
1008
|
// 渲染可见item
|
|
322
1009
|
for (let i = startItem; i <= endItem; i++) {
|
|
323
|
-
|
|
1010
|
+
const currentGlobalIndex = globalItemIndex + i;
|
|
1011
|
+
const itemId = `list-item-${currentGlobalIndex}`;
|
|
324
1012
|
const sectionItemStyle = Object.assign({ position: 'absolute', zIndex: 1, boxSizing: 'border-box', width: '100%', minHeight: '20px', overflow: 'hidden', lineHeight: 1 }, (isHorizontal
|
|
325
1013
|
? {
|
|
326
1014
|
top: 0,
|
|
@@ -334,17 +1022,208 @@ const List = (props) => {
|
|
|
334
1022
|
height: itemSizes[i],
|
|
335
1023
|
marginBottom: space
|
|
336
1024
|
}));
|
|
337
|
-
|
|
1025
|
+
// ResizeObserver:绑定内层内容容器测量真实尺寸,尺寸变化时才 bump 版本
|
|
1026
|
+
const refCallback = (el) => {
|
|
1027
|
+
if (el) {
|
|
1028
|
+
const capturedIndex = currentGlobalIndex;
|
|
1029
|
+
itemRefsRef.current.set(capturedIndex, el);
|
|
1030
|
+
resizeObserver.observe(el, capturedIndex);
|
|
1031
|
+
// H5:使用 getBoundingClientRect 进行 fallback 测量
|
|
1032
|
+
// 小程序:使用 SelectorQuery 进行 fallback 测量(小程序没有 getBoundingClientRect)
|
|
1033
|
+
const measureAndUpdate = (measured) => {
|
|
1034
|
+
var _a;
|
|
1035
|
+
if (measured > 0) {
|
|
1036
|
+
const oldSize = sizeCache.getItemSize(capturedIndex);
|
|
1037
|
+
if (Math.abs(oldSize - measured) < 1)
|
|
1038
|
+
return;
|
|
1039
|
+
sizeCache.setItemSize(capturedIndex, measured);
|
|
1040
|
+
scrollCorrection.recordSizeChange(capturedIndex, oldSize, measured);
|
|
1041
|
+
if (isWeapp && props.useResizeObserver === true) {
|
|
1042
|
+
scheduleWeappDynamicReflow();
|
|
1043
|
+
}
|
|
1044
|
+
else if (sizeCacheRafRef.current == null) {
|
|
1045
|
+
sizeCacheRafRef.current = requestAnimationFrame(() => {
|
|
1046
|
+
sizeCacheRafRef.current = null;
|
|
1047
|
+
setSizeCacheVersion((v) => v + 1);
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
// 小程序:延迟 onItemSizeChange,避免父组件重渲染导致 List remount
|
|
1051
|
+
if (isWeapp) {
|
|
1052
|
+
weappDeferItemSizeChange(capturedIndex, measured, props.onItemSizeChange);
|
|
1053
|
+
}
|
|
1054
|
+
else {
|
|
1055
|
+
(_a = props.onItemSizeChange) === null || _a === void 0 ? void 0 : _a.call(props, capturedIndex, measured);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
if (isH5) {
|
|
1060
|
+
requestAnimationFrame(() => {
|
|
1061
|
+
if (!itemRefsRef.current.has(capturedIndex))
|
|
1062
|
+
return;
|
|
1063
|
+
const rect = el.getBoundingClientRect();
|
|
1064
|
+
measureAndUpdate(isHorizontal ? rect.width : rect.height);
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
else if (isWeapp) {
|
|
1068
|
+
// 小程序端:使用 SelectorQuery 测量内层内容容器的实际尺寸
|
|
1069
|
+
// 注意:必须选 inner 容器(无固定高度,由内容撑开),不能选 outer(有虚拟列表设置的固定高度)
|
|
1070
|
+
// 已测量过的 item 跳过,避免 refCallback 重复触发导致无限循环
|
|
1071
|
+
// 滚动期间跳过 SelectorQuery(不加入 weappMeasuredItemsRef),避免异步查询干扰 scroll-view
|
|
1072
|
+
// SelectorQuery 是只读查询,滚动期间执行安全
|
|
1073
|
+
if (shouldMeasureWeappItem(capturedIndex, weappMeasuredItemsRef.current)) {
|
|
1074
|
+
weappMeasuredItemsRef.current.add(capturedIndex);
|
|
1075
|
+
// 使用 Taro.nextTick 代替 setTimeout(50):等待下一帧渲染完成后再测量
|
|
1076
|
+
// 比固定延时更快(减少"预估→实测"闪烁)且更可靠(保证 DOM 已更新)
|
|
1077
|
+
Taro.nextTick(() => {
|
|
1078
|
+
if (!itemRefsRef.current.has(capturedIndex))
|
|
1079
|
+
return;
|
|
1080
|
+
Taro.createSelectorQuery()
|
|
1081
|
+
// 页面上可能同时存在多个 List,inner id 必须带 listId 前缀避免跨列表误命中
|
|
1082
|
+
.select(`#${listId}-list-item-inner-${capturedIndex}`)
|
|
1083
|
+
.boundingClientRect((rect) => {
|
|
1084
|
+
if (rect) {
|
|
1085
|
+
measureAndUpdate(isHorizontal ? rect.width : rect.height);
|
|
1086
|
+
}
|
|
1087
|
+
})
|
|
1088
|
+
.exec();
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
else {
|
|
1094
|
+
const oldEl = itemRefsRef.current.get(currentGlobalIndex);
|
|
1095
|
+
if (oldEl) {
|
|
1096
|
+
resizeObserver.unobserve(oldEl);
|
|
1097
|
+
itemRefsRef.current.delete(currentGlobalIndex);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
// 动态尺寸时:外层负责定位,内层由内容撑开以便测量真实尺寸。
|
|
1102
|
+
// 纵向:内层 width:100% 无 height,由内容撑开,测到的是内容高度。
|
|
1103
|
+
// 横向:width:max-content + height:100%:
|
|
1104
|
+
// - width:max-content 便于测量真实宽度;
|
|
1105
|
+
// - height:100% 让测量层与外层 slot 条带高度一致,避免「外层条带 180px、内层 wrapper 只有内容高度」导致的内外高度差。
|
|
1106
|
+
const contentWrapperStyle = props.useResizeObserver
|
|
1107
|
+
? (isHorizontal
|
|
1108
|
+
// weapp 下 max-content 兼容性不稳定,改用 inline-flex 收缩包裹以测得真实宽度
|
|
1109
|
+
? (isWeapp
|
|
1110
|
+
? { boxSizing: 'border-box', display: 'inline-flex', height: '100%' }
|
|
1111
|
+
: { boxSizing: 'border-box', width: 'max-content', height: '100%' })
|
|
1112
|
+
: { boxSizing: 'border-box', width: '100%' })
|
|
1113
|
+
: {};
|
|
1114
|
+
const outerItemProps = {
|
|
338
1115
|
key: section.key + '-item-' + i,
|
|
1116
|
+
id: itemId,
|
|
339
1117
|
style: sectionItemStyle,
|
|
340
|
-
}
|
|
1118
|
+
};
|
|
1119
|
+
if (props.useResizeObserver) {
|
|
1120
|
+
const innerProps = {
|
|
1121
|
+
ref: refCallback,
|
|
1122
|
+
style: contentWrapperStyle,
|
|
1123
|
+
};
|
|
1124
|
+
// 小程序端需要 id 用于 SelectorQuery 测量内容真实尺寸;H5 端不需要(用 getBoundingClientRect)
|
|
1125
|
+
if (isWeapp) {
|
|
1126
|
+
// 页面内多 List 并存时避免 id 冲突(例如 demo 同页含多个 List)
|
|
1127
|
+
innerProps.id = `${listId}-list-item-inner-${currentGlobalIndex}`;
|
|
1128
|
+
}
|
|
1129
|
+
innerProps['data-index'] = String(currentGlobalIndex);
|
|
1130
|
+
const itemNode = React.createElement(View, outerItemProps, React.createElement(View, innerProps, section.items[i]));
|
|
1131
|
+
// 任务 4.1:当 List 暴露 scrollRef 时,为每个 item 提供 Context,供内层 WaterFlow 使用
|
|
1132
|
+
const itemWithContext = scrollRefProp && !useScrollElementMode
|
|
1133
|
+
? React.createElement(ScrollElementContextOrFallback.Provider, {
|
|
1134
|
+
key: outerItemProps.key,
|
|
1135
|
+
value: {
|
|
1136
|
+
scrollRef: scrollRefProp,
|
|
1137
|
+
containerHeight: containerLength,
|
|
1138
|
+
startOffset: offset + itemOffsets[i],
|
|
1139
|
+
reportNestedHeightChange: props.useResizeObserver
|
|
1140
|
+
? (h) => handleReportNestedHeightChange(h, currentGlobalIndex)
|
|
1141
|
+
: undefined,
|
|
1142
|
+
},
|
|
1143
|
+
}, itemNode)
|
|
1144
|
+
: itemNode;
|
|
1145
|
+
nodes.push(itemWithContext);
|
|
1146
|
+
}
|
|
1147
|
+
else {
|
|
1148
|
+
const itemNode = React.createElement(View, outerItemProps, section.items[i]);
|
|
1149
|
+
const itemWithContext = scrollRefProp && !useScrollElementMode
|
|
1150
|
+
? React.createElement(ScrollElementContextOrFallback.Provider, {
|
|
1151
|
+
key: outerItemProps.key,
|
|
1152
|
+
value: {
|
|
1153
|
+
scrollRef: scrollRefProp,
|
|
1154
|
+
containerHeight: containerLength,
|
|
1155
|
+
startOffset: offset + itemOffsets[i],
|
|
1156
|
+
reportNestedHeightChange: props.useResizeObserver
|
|
1157
|
+
? (h) => handleReportNestedHeightChange(h, currentGlobalIndex)
|
|
1158
|
+
: undefined,
|
|
1159
|
+
},
|
|
1160
|
+
}, itemNode)
|
|
1161
|
+
: itemNode;
|
|
1162
|
+
nodes.push(itemWithContext);
|
|
1163
|
+
}
|
|
341
1164
|
}
|
|
1165
|
+
globalItemIndex += section.items.length;
|
|
342
1166
|
offset += itemOffsets[itemOffsets.length - 1];
|
|
343
1167
|
}
|
|
344
1168
|
return nodes;
|
|
345
1169
|
};
|
|
346
|
-
|
|
1170
|
+
// 渲染 NoMore 内容
|
|
1171
|
+
const renderNoMoreContent = () => {
|
|
1172
|
+
if (!noMoreConfig || !noMoreConfig.visible)
|
|
1173
|
+
return null;
|
|
1174
|
+
const noMoreHeightValue = noMoreConfig.height || 60;
|
|
1175
|
+
const listContentEnd = sectionOffsets[sectionOffsets.length - 1];
|
|
1176
|
+
const defaultStyle = Object.assign(Object.assign(Object.assign({ position: 'absolute' }, (isHorizontal
|
|
1177
|
+
? { left: listContentEnd, top: 0, width: noMoreHeightValue, height: '100%' }
|
|
1178
|
+
: { top: listContentEnd, left: 0, width: '100%', height: noMoreHeightValue })), { display: 'flex', alignItems: 'center', justifyContent: 'center', textAlign: 'center', color: '#999', fontSize: '14px', boxSizing: 'border-box' }), noMoreConfig.style);
|
|
1179
|
+
return (jsx(View, { style: defaultStyle, children: noMoreConfig.children || noMoreConfig.text || '没有更多了' }));
|
|
1180
|
+
};
|
|
1181
|
+
// 空列表场景:仅显示 NoMore
|
|
1182
|
+
if (sections.length === 0 && (noMoreConfig === null || noMoreConfig === void 0 ? void 0 : noMoreConfig.visible)) {
|
|
1183
|
+
return (jsx(ScrollView, Object.assign({ ref: containerRef }, scrollViewProps, { children: jsx(View, { style: Object.assign({ minHeight: containerLength, display: 'flex', alignItems: 'center', justifyContent: 'center' }, containerStyle), children: renderNoMoreContent() }) })));
|
|
1184
|
+
}
|
|
1185
|
+
// 可滚区域总尺寸
|
|
1186
|
+
// H5 refresher 用负 translateY 隐藏,有上方混排时可能延伸到 sibling 区域;overflow:hidden 裁剪避免重叠
|
|
1187
|
+
const needsRefresherClip = refresherHeightForH5 > 0;
|
|
1188
|
+
const contentWrapperStyle = isHorizontal
|
|
1189
|
+
? Object.assign({ width: totalLength, position: 'relative', height: '100%' }, (needsRefresherClip && { overflow: 'hidden' })) : Object.assign({ height: totalLength, position: 'relative', width: '100%' }, (needsRefresherClip && { overflow: 'hidden' }));
|
|
1190
|
+
const listWrapperStyle = isHorizontal
|
|
1191
|
+
? { width: listContentLength, position: 'relative', height: '100%' }
|
|
1192
|
+
: { height: listContentLength, position: 'relative', width: '100%' };
|
|
1193
|
+
const pullTranslate = refresherHeightForH5 > 0 && h5RefresherProps.pullDistance !== 0
|
|
1194
|
+
? { transform: `translateY(${h5RefresherProps.pullDistance}px)` }
|
|
1195
|
+
: {};
|
|
1196
|
+
const h5RefresherTranslateY = -refresherHeightForH5 + h5RefresherProps.pullDistance;
|
|
1197
|
+
// 内容区域渲染
|
|
1198
|
+
const renderContentArea = () => {
|
|
1199
|
+
var _a, _b;
|
|
1200
|
+
return (jsx(View, { style: contentWrapperStyle, children: refresherHeightForH5 > 0 ? (jsxs(Fragment, { children: [jsx(View, { style: {
|
|
1201
|
+
position: 'absolute',
|
|
1202
|
+
top: 0,
|
|
1203
|
+
left: 0,
|
|
1204
|
+
right: 0,
|
|
1205
|
+
height: refresherHeightForH5,
|
|
1206
|
+
zIndex: 0,
|
|
1207
|
+
transform: `translateY(${h5RefresherTranslateY}px)`,
|
|
1208
|
+
}, children: renderRefresherContent() }), jsxs(View, { style: Object.assign(Object.assign(Object.assign({}, listWrapperStyle), pullTranslate), { zIndex: 1, background: (_b = (_a = style === null || style === void 0 ? void 0 : style.background) !== null && _a !== void 0 ? _a : style === null || style === void 0 ? void 0 : style.backgroundColor) !== null && _b !== void 0 ? _b : '#fff' }), children: [stickyHeaderNode, renderSections(), renderNoMoreContent()] })] })) : (jsxs(Fragment, { children: [!supportsNativeRefresher && renderRefresherContent(), stickyHeaderNode, renderSections(), renderNoMoreContent()] })) }));
|
|
1209
|
+
};
|
|
1210
|
+
// useScrollElementMode 或 needAutoFind&&pending:渲染 View 以便监听外部滚动或 probe 阶段查找滚动父节点
|
|
1211
|
+
const renderView = useScrollElementMode || (!!needAutoFind && autoFindStatus === 'pending');
|
|
1212
|
+
if (renderView) {
|
|
1213
|
+
// 任务 2.4:恢复 refresher DOM 结构(refresher 层 + listWrapperStyle)
|
|
1214
|
+
return (jsx(View, { ref: contentWrapperRef, id: contentId, style: contentWrapperStyle, children: refresherHeightForH5 > 0 ? (jsxs(Fragment, { children: [jsx(View, { style: {
|
|
1215
|
+
position: 'absolute',
|
|
1216
|
+
top: 0,
|
|
1217
|
+
left: 0,
|
|
1218
|
+
right: 0,
|
|
1219
|
+
height: refresherHeightForH5,
|
|
1220
|
+
zIndex: 0,
|
|
1221
|
+
transform: `translateY(${h5RefresherTranslateY}px)`,
|
|
1222
|
+
}, children: renderRefresherContent() }), jsxs(View, { style: Object.assign(Object.assign(Object.assign({}, listWrapperStyle), pullTranslate), { zIndex: 1, background: (_b = (_a = style === null || style === void 0 ? void 0 : style.background) !== null && _a !== void 0 ? _a : style === null || style === void 0 ? void 0 : style.backgroundColor) !== null && _b !== void 0 ? _b : '#fff' }), children: [stickyHeaderNode, renderSections(), renderNoMoreContent()] })] })) : (jsxs(Fragment, { children: [stickyHeaderNode, renderSections(), renderNoMoreContent()] })) }));
|
|
1223
|
+
}
|
|
1224
|
+
return (jsxs(ScrollView, Object.assign({ ref: containerRef }, scrollViewProps, { id: listId, children: [supportsNativeRefresher && renderRefresherContent(), renderContentArea()] })));
|
|
347
1225
|
};
|
|
1226
|
+
const List = React.forwardRef(InnerList);
|
|
348
1227
|
|
|
349
|
-
export { List, ListItem, StickyHeader, StickySection, accumulate, List as default, isShaking };
|
|
1228
|
+
export { List, ListItem, ScrollElementContextOrFallback as ListScrollElementContext, NoMore, StickyHeader, StickySection, accumulate, List as default, isShaking };
|
|
350
1229
|
//# sourceMappingURL=index.js.map
|