Skip to content

Commit ce756b6

Browse files
authored
[codex] expose scrollTop in extraRender info (#357)
1 parent 402e156 commit ce756b6

9 files changed

Lines changed: 46 additions & 11 deletions

File tree

src/List.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { clsx } from 'clsx';
22
import type { ResizeObserverProps } from '@rc-component/resize-observer';
33
import ResizeObserver from '@rc-component/resize-observer';
4-
import { useEvent } from '@rc-component/util';
5-
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
4+
import { useEvent, useLayoutEffect } from '@rc-component/util';
65
import * as React from 'react';
76
import { useRef, useState } from 'react';
87
import { flushSync } from 'react-dom';
@@ -558,6 +557,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
558557
end,
559558
virtual: inVirtual,
560559
offsetX: offsetLeft,
560+
scrollTop: offsetTop,
561561
offsetY: fillerOffset,
562562
rtl: isRTL,
563563
getSize,

src/ScrollBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { clsx } from 'clsx';
2-
import raf from '@rc-component/util/lib/raf';
2+
import { raf } from '@rc-component/util';
33
import * as React from 'react';
44
import { getPageXY } from './hooks/useScrollDrag';
55

src/hooks/useFrameWheel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import raf from '@rc-component/util/lib/raf';
1+
import { raf } from '@rc-component/util';
22
import { useRef } from 'react';
33
import isFF from '../utils/isFirefox';
44
import useOriginScroll from './useOriginScroll';

src/hooks/useMobileTouchMove.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
1+
import { useLayoutEffect } from '@rc-component/util';
22
import type * as React from 'react';
33
import { useRef } from 'react';
44

src/hooks/useScrollDrag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import raf from '@rc-component/util/lib/raf';
1+
import { raf } from '@rc-component/util';
22
import * as React from 'react';
33

44
function smoothScrollOffset(offset: number) {

src/hooks/useScrollTo.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/* eslint-disable no-param-reassign */
22
import * as React from 'react';
3-
import raf from '@rc-component/util/lib/raf';
3+
import { raf, useLayoutEffect, warning } from '@rc-component/util';
44
import type { GetKey } from '../interface';
55
import type CacheMap from '../utils/CacheMap';
6-
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
7-
import { warning } from '@rc-component/util';
86

97
const MAX_TIMES = 10;
108

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import List from './List';
22

3-
export type { ListRef, ListProps } from './List';
3+
export type { ListRef, ListProps, ScrollConfig, ScrollTo } from './List';
4+
export { default as MockList } from './mock';
45

56
export default List;

src/interface.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ export interface ExtraRenderInfo {
1919
end: number;
2020
/** Is current in virtual render */
2121
virtual: boolean;
22-
/** Used for `scrollWidth` tell the horizontal offset */
22+
/**
23+
* Horizontal scroll offset applied to rendered items when `scrollWidth` is set.
24+
* 当设置 `scrollWidth` 时,应用到已渲染元素上的横向滚动偏移量。
25+
*/
2326
offsetX: number;
27+
/**
28+
* Current vertical scrollTop of the holder element.
29+
* holder 元素当前真实的纵向 `scrollTop`,表示视口滚动到了哪里。
30+
*/
31+
scrollTop: number;
32+
/**
33+
* Vertical translate offset of the rendered filler content.
34+
* 已渲染 filler 内容的纵向 `translateY` 偏移量,表示这一段内容被平移到虚拟列表中的哪个位置。
35+
*/
2436
offsetY: number;
2537

2638
rtl: boolean;

tests/scroll.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ describe('List.Scroll', () => {
113113

114114
wrapper.unmount();
115115
});
116+
117+
it('passes current scrollTop to extraRender', () => {
118+
const listRef = React.createRef();
119+
const extraRender = jest.fn(() => null);
120+
const wrapper = genList({
121+
itemHeight: 20,
122+
height: 100,
123+
data: genData(100),
124+
ref: listRef,
125+
extraRender,
126+
});
127+
128+
listRef.current.scrollTo(80);
129+
jest.runAllTimers();
130+
wrapper.update();
131+
132+
expect(extraRender).toHaveBeenLastCalledWith(
133+
expect.objectContaining({
134+
scrollTop: 80,
135+
}),
136+
);
137+
138+
wrapper.unmount();
139+
});
116140
});
117141

118142
describe('scroll to object', () => {

0 commit comments

Comments
 (0)