@@ -3,9 +3,6 @@ import React, {
3
3
useMemo , useRef , ReactNode , useLayoutEffect , useImperativeHandle
4
4
} from 'react' ;
5
5
6
- type OptionalKeys < T > = {
7
- [ K in keyof T ] ?: T [ K ] ;
8
- } ;
9
6
// fix forwardRef type for generic types. refer: https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref
10
7
export type FixedForwardRef = < T , P = { } > (
11
8
render : ( props : P , ref : React . Ref < T > ) => React . ReactElement | null ,
@@ -41,9 +38,17 @@ export type VirtualListProps<ITEM> = {
41
38
* listen to scroll event.
42
39
*/
43
40
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 ,
44
49
className ?: string ,
45
50
style ?: React . CSSProperties ,
46
- } & OptionalKeys < typeof defaultProps >
51
+ } & Partial < typeof defaultProps >
47
52
48
53
export const defaultProps = {
49
54
/**
@@ -59,6 +64,7 @@ export const defaultProps = {
59
64
export interface VirtualListHandle {
60
65
scrollToIndex ( index : number , block ?: 'start' | 'end' | 'center' | 'nearest' ) : void
61
66
forceUpdate ( ) : void
67
+ getRootElement ( ) : HTMLElement
62
68
}
63
69
64
70
export const VirtualList = forwardRef ( function < ITEM > (
@@ -180,6 +186,9 @@ export const VirtualList = forwardRef(function <ITEM>(
180
186
forceUpdate ( ) {
181
187
setforceRerender ( [ ] )
182
188
} ,
189
+ getRootElement ( ) {
190
+ return list . current !
191
+ } ,
183
192
} ) , [ itemSize ] ) ;
184
193
// scrollToIndex
185
194
useLayoutEffect ( ( ) => {
@@ -197,9 +206,11 @@ export const VirtualList = forwardRef(function <ITEM>(
197
206
} , [ shouldScrollToIndex ] )
198
207
//
199
208
return < div ref = { list } onScroll = { handleScroll } className = { props . className } style = { { overflow : 'auto' , ...props . style } } >
209
+ { props . renderHead ?.( ) }
200
210
< div ref = { listInner } style = { { display : 'flex' , flexDirection : 'column' , ...( props . virtual && listInnerStyle ) } } >
201
211
{ visible . map ( ( item , i ) => props . renderItem ( item , visibleIndices [ i ] ) ) }
202
212
</ div >
213
+ { props . renderFoot ?.( ) }
203
214
</ div >
204
215
} )
205
216
0 commit comments