@tarojs/components-advanced 4.2.1-beta.2 → 4.2.1

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