Skip to content

Commit 3054485

Browse files
committed
feat: add prop 'virtual'
1 parent d1ad312 commit 3054485

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

lib/VirtualList.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export const defaultProps = {
5050
* The visible space of the list. It is only used before DOM created(SSR).
5151
*/
5252
listSize: 1000,
53+
/**
54+
* Whether to enable the virtual list feature.
55+
*/
56+
virtual: true,
5357
}
5458

5559
export interface VirtualListHandle {
@@ -72,7 +76,7 @@ export const VirtualList = forwardRef(function <ITEM>(
7276
const [scrollTop, setscrollTop] = useState(0);
7377
const [listSize, setlistSize] = useState(props.listSize!);
7478
const [forceRerender, setforceRerender] = useState([]); // change value to force rerender
75-
const ignoreScrollOnce = useRef(false);
79+
const ignoreUpdateScrollTopOnce = useRef(false);
7680
//
7781
const totalSpace = itemSize * count
7882
let topSpace = scrollTop - buffer
@@ -144,8 +148,8 @@ export const VirtualList = forwardRef(function <ITEM>(
144148

145149
setlistSize(list.current!.clientHeight)
146150

147-
if (ignoreScrollOnce.current) {
148-
ignoreScrollOnce.current = false
151+
if (ignoreUpdateScrollTopOnce.current) {
152+
ignoreUpdateScrollTopOnce.current = false
149153
} else {
150154
const scrollTop = list.current!.scrollTop;
151155
if (Math.abs(prevScrollTop.current - scrollTop) > (props.triggerDistance ?? itemSize)) {
@@ -183,7 +187,7 @@ export const VirtualList = forwardRef(function <ITEM>(
183187
if (el) {
184188
// @ts-ignore
185189
el.scrollIntoView({ block })
186-
ignoreScrollOnce.current = true
190+
ignoreUpdateScrollTopOnce.current = true
187191
}
188192
}
189193
}, [shouldScrollToIndex])

src/examples/scrollToIndex.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ export default function ScrollToIndexExample() {
1919
ref={ref}
2020
items={exampleData}
2121
style={{ height: '600px', border: '1px solid #ccc', padding: '10px' }}
22-
renderItem={(item, index) => <div key={index} style={{ marginBottom: '10px' }}>
23-
<h3>{index}. {item.headline}</h3>
24-
<div>
25-
<div style={{ float: 'left', width: '100px', height: '100px', background: '#f0f0f0', borderRadius: '5px', marginRight: '10px' }}></div>
26-
{item.content}
22+
renderItem={
23+
(item, index) => <div key={index} style={{ marginBottom: '10px' }}>
24+
<h3>{index}. {item.headline}</h3>
25+
<div>
26+
<div style={{ float: 'left', width: '100px', height: '100px', background: '#f0f0f0', borderRadius: '5px', marginRight: '10px' }}></div>
27+
{item.content}
28+
</div>
2729
</div>
28-
</div>}
30+
}
2931
></VirtualList>
3032
<br />
3133
<a href="https://github.com/phphe/react-base-virtual-list/blob/main/src/examples/scrollToIndex.tsx">Source Code</a>

0 commit comments

Comments
 (0)