Skip to content

Commit f881513

Browse files
committed
feat: add props renderHead, renderFoot. method: getRootElement
1 parent 240969c commit f881513

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/VirtualList.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import React, {
33
useMemo, useRef, ReactNode, useLayoutEffect, useImperativeHandle
44
} from 'react';
55

6-
type OptionalKeys<T> = {
7-
[K in keyof T]?: T[K];
8-
};
96
// fix forwardRef type for generic types. refer: https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref
107
export type FixedForwardRef = < T, P = {} > (
118
render: (props: P, ref: React.Ref<T>) => React.ReactElement | null,
@@ -41,9 +38,17 @@ export type VirtualListProps<ITEM> = {
4138
* listen to scroll event.
4239
*/
4340
onScroll?: typeof document.onscroll,
41+
/**
42+
* Insert elements at the head. Recommended to only insert elements that do not take up space or take very little space, such as position absolute.
43+
*/
44+
renderHead?: () => ReactNode,
45+
/**
46+
* Insert elements at the foot. Recommended to only insert elements that do not take up space or take very little space, such as position absolute.
47+
*/
48+
renderFoot?: () => ReactNode,
4449
className?: string,
4550
style?: React.CSSProperties,
46-
} & OptionalKeys<typeof defaultProps>
51+
} & Partial<typeof defaultProps>
4752

4853
export const defaultProps = {
4954
/**
@@ -59,6 +64,7 @@ export const defaultProps = {
5964
export interface VirtualListHandle {
6065
scrollToIndex(index: number, block?: 'start' | 'end' | 'center' | 'nearest'): void
6166
forceUpdate(): void
67+
getRootElement(): HTMLElement
6268
}
6369

6470
export const VirtualList = forwardRef(function <ITEM>(
@@ -180,6 +186,9 @@ export const VirtualList = forwardRef(function <ITEM>(
180186
forceUpdate() {
181187
setforceRerender([])
182188
},
189+
getRootElement() {
190+
return list.current!
191+
},
183192
}), [itemSize]);
184193
// scrollToIndex
185194
useLayoutEffect(() => {
@@ -197,9 +206,11 @@ export const VirtualList = forwardRef(function <ITEM>(
197206
}, [shouldScrollToIndex])
198207
//
199208
return <div ref={list} onScroll={handleScroll} className={props.className} style={{ overflow: 'auto', ...props.style }}>
209+
{props.renderHead?.()}
200210
<div ref={listInner} style={{ display: 'flex', flexDirection: 'column', ...(props.virtual && listInnerStyle) }}>
201211
{visible.map((item, i) => props.renderItem(item, visibleIndices[i]))}
202212
</div>
213+
{props.renderFoot?.()}
203214
</div>
204215
})
205216

0 commit comments

Comments
 (0)