1
1
import * as React from 'react' ;
2
- import { useRef , useState } from 'react' ;
2
+ import { useRef , useState , useLayoutEffect } from 'react' ;
3
3
import classNames from 'classnames' ;
4
4
import Filler from './Filler' ;
5
5
import ScrollBar from './ScrollBar' ;
@@ -50,6 +50,8 @@ export interface ListProps<T> extends React.HTMLAttributes<any> {
50
50
virtual ?: boolean ;
51
51
52
52
onScroll ?: React . UIEventHandler < HTMLElement > ;
53
+ /** Trigger when render list item changed */
54
+ onVisibleChange ?: ( visibleList : T [ ] , fullList : T [ ] ) => void ;
53
55
}
54
56
55
57
export function RawList < T > ( props : ListProps < T > , ref : React . Ref < ListRef > ) {
@@ -66,6 +68,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
66
68
virtual,
67
69
component : Component = 'div' ,
68
70
onScroll,
71
+ onVisibleChange,
69
72
...restProps
70
73
} = props ;
71
74
@@ -99,7 +102,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
99
102
100
103
// ================================ Scroll ================================
101
104
function syncScrollTop ( newTop : number | ( ( prev : number ) => number ) ) {
102
- setScrollTop ( origin => {
105
+ setScrollTop ( ( origin ) => {
103
106
let value : number ;
104
107
if ( typeof newTop === 'function' ) {
105
108
value = newTop ( origin ) ;
@@ -242,8 +245,8 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
242
245
useVirtual ,
243
246
isScrollAtTop ,
244
247
isScrollAtBottom ,
245
- offsetY => {
246
- syncScrollTop ( top => {
248
+ ( offsetY ) => {
249
+ syncScrollTop ( ( top ) => {
247
250
const newTop = top + offsetY ;
248
251
return newTop ;
249
252
} ) ;
@@ -260,7 +263,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
260
263
return true ;
261
264
} ) ;
262
265
263
- React . useLayoutEffect ( ( ) => {
266
+ useLayoutEffect ( ( ) => {
264
267
// Firefox only
265
268
function onMozMousePixelScroll ( e : Event ) {
266
269
if ( useVirtual ) {
@@ -297,6 +300,14 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
297
300
scrollTo,
298
301
} ) ) ;
299
302
303
+ // ================================ Effect ================================
304
+ /** We need told outside that some list not rendered */
305
+ useLayoutEffect ( ( ) => {
306
+ const renderList = mergedData . slice ( start , end ) ;
307
+
308
+ onVisibleChange ?.( renderList , mergedData ) ;
309
+ } , [ start , end , mergedData ] ) ;
310
+
300
311
// ================================ Render ================================
301
312
const listChildren = useChildren ( mergedData , start , end , setInstanceRef , children , sharedConfig ) ;
302
313
0 commit comments