@@ -19,6 +19,7 @@ import { get } from 'lodash';
19
19
import classnames from 'classnames' ;
20
20
import EmptyHolder from 'common/components/empty-holder' ;
21
21
import ErdaIcon from 'common/components/erda-icon' ;
22
+ import { useInViewPort } from 'common/use-hooks' ;
22
23
23
24
interface CardColumnsProps < T > {
24
25
dataIndex : keyof T | string [ ] ;
@@ -41,6 +42,7 @@ interface IProps<T = Record<string, any>> {
41
42
rowClassName ?: string ;
42
43
columns : CardColumnsProps < T > [ ] ;
43
44
emptyHolder ?: React . ReactNode ;
45
+ onViewChange ?: ( data : T , flag ?: boolean ) => void ;
44
46
}
45
47
46
48
const renderChild = < T , > ( record : T , columns : CardColumnsProps < T > [ ] , index : number ) => {
@@ -64,17 +66,41 @@ const renderChild = <T,>(record: T, columns: CardColumnsProps<T>[], index: numbe
64
66
} ) ;
65
67
} ;
66
68
69
+ interface IRowProps < T > {
70
+ rowClass ?: string ;
71
+ rowId : string | number ;
72
+ rowClick : ( ) => void ;
73
+ columns : CardColumnsProps < T > [ ] ;
74
+ record : T ;
75
+ index : number ;
76
+ onViewChange ?: ( data : T , flag ?: boolean ) => void ;
77
+ }
78
+
79
+ const RowItem = < T , > ( { rowClick, rowId, rowClass, record, columns, index, onViewChange } : IRowProps < T > ) => {
80
+ const rowRef = React . useRef ( ) ;
81
+ const [ isInView ] = useInViewPort ( rowRef ) ;
82
+ React . useEffect ( ( ) => {
83
+ onViewChange ?.( record , isInView ) ;
84
+ } , [ isInView , record ] ) ;
85
+ return (
86
+ < Row ref = { rowRef } onClick = { rowClick } key = { rowId } className = { rowClass } >
87
+ { renderChild < T > ( record , columns , index ) }
88
+ </ Row >
89
+ ) ;
90
+ } ;
91
+
67
92
const CardList = < T , > ( {
68
93
loading,
69
94
dataSource,
70
- rowKey = 'key' ,
95
+ rowKey,
71
96
rowClassName,
72
97
columns,
73
98
rowClick,
74
99
slot,
75
100
size = 'default' ,
76
101
emptyHolder,
77
102
onRefresh,
103
+ onViewChange
78
104
} : IProps < T > ) => {
79
105
return (
80
106
< div className = "card-list flex flex-1 flex-col bg-white shadow pb-2" >
@@ -114,15 +140,17 @@ const CardList = <T,>({
114
140
} ,
115
141
) ;
116
142
return (
117
- < Row
118
- onClick = { ( ) => {
143
+ < RowItem < T >
144
+ rowId = { rowId }
145
+ rowClass = { rowClass }
146
+ rowClick = { ( ) => {
119
147
rowClick ?.( record ) ;
120
148
} }
121
- key = { rowId }
122
- className = { rowClass }
123
- >
124
- { renderChild < T > ( record , columns , index ) }
125
- </ Row >
149
+ columns = { columns }
150
+ index = { index }
151
+ record = { record }
152
+ onViewChange = { onViewChange }
153
+ / >
126
154
) ;
127
155
} )
128
156
: emptyHolder || < EmptyHolder relative /> }
0 commit comments