forked from testing-library/testing-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStickyList.js
48 lines (45 loc) · 998 Bytes
/
StickyList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React, { forwardRef, useEffect, useRef } from 'react';
import { FixedSizeList as List } from 'react-window';
function StickyList(
{
follow = false,
mode = 'bottom',
height,
itemCount,
itemData,
itemSize,
width,
outerElementType,
children,
},
ref,
) {
const innerRef = useRef();
useEffect(() => {
if (ref.current && follow && innerRef.current) {
if (
mode === 'bottom' &&
innerRef.current.offsetHeight > ref.current.props.height
) {
ref.current.scrollTo(innerRef.current.offsetHeight);
} else if (mode === 'top') {
ref.current.scrollTo(0);
}
}
}, [itemCount, follow]);
return (
<List
ref={ref}
height={height}
itemCount={itemCount}
itemData={itemData}
itemSize={itemSize}
width={width}
innerRef={innerRef}
outerElementType={outerElementType}
>
{children}
</List>
);
}
export default forwardRef(StickyList);