1
1
import React , { SVGProps , useCallback , useContext , useLayoutEffect , useRef } from "react" ;
2
- import { CacheFunction , ColumnProps , Generic , RowProps } from "../index" ;
2
+ import {
3
+ CacheFunction ,
4
+ ClickFunction ,
5
+ ColumnProps ,
6
+ Generic ,
7
+ RowProps ,
8
+ RowRenderProps
9
+ } from "../index" ;
3
10
//@ts -ignore TS2307
4
11
import Minus from "./svg/minus-circle.svg" ;
5
12
//@ts -ignore TS2307
@@ -16,6 +23,15 @@ interface TableCellProps {
16
23
onExpanderClick : ( event ?: React . MouseEvent < Element , MouseEvent > ) => void ;
17
24
}
18
25
26
+ interface RowContainerProps {
27
+ row : Generic ;
28
+ index : number ;
29
+ children : React . ReactNode ;
30
+ containerStyle : React . CSSProperties ;
31
+ onRowClick : ClickFunction ;
32
+ rowRenderer : React . ElementType < RowRenderProps > ;
33
+ }
34
+
19
35
type CSSFunction = ( index : number ) => React . CSSProperties ;
20
36
21
37
const getRowStyle = ( index : number , rowStyle ?: React . CSSProperties | CSSFunction ) => {
@@ -74,6 +90,42 @@ const TableCell = React.memo(
74
90
}
75
91
) ;
76
92
93
+ const RowContainer = ( {
94
+ row,
95
+ index,
96
+ children,
97
+ onRowClick,
98
+ containerStyle,
99
+ rowRenderer : RowRenderer
100
+ } : RowContainerProps ) => {
101
+ const onContainerClick = useCallback (
102
+ event => {
103
+ if ( onRowClick ) {
104
+ onRowClick ( event , { index } ) ;
105
+ }
106
+ } ,
107
+ [ index , onRowClick ]
108
+ ) ;
109
+
110
+ if ( RowRenderer ) {
111
+ const style = {
112
+ ...containerStyle ,
113
+ display : "flex"
114
+ } ;
115
+ return (
116
+ < RowRenderer row = { row } index = { index } style = { style } >
117
+ { children }
118
+ </ RowRenderer >
119
+ ) ;
120
+ }
121
+
122
+ return (
123
+ < div className = "row-container" style = { containerStyle } onClick = { onContainerClick } >
124
+ { children }
125
+ </ div >
126
+ ) ;
127
+ } ;
128
+
77
129
const Row = ( {
78
130
row,
79
131
index,
@@ -83,10 +135,10 @@ const Row = ({
83
135
rowHeight,
84
136
onRowClick,
85
137
useRowWidth,
138
+ rowRenderer,
86
139
clearSizeCache,
87
140
calculateHeight,
88
141
generateKeyFromRow,
89
- rowRenderer : RowRenderer ,
90
142
subComponent : SubComponent
91
143
} : RowProps ) => {
92
144
// hooks
@@ -118,15 +170,6 @@ const Row = ({
118
170
} ;
119
171
120
172
// function(s)
121
- const onContainerClick = useCallback (
122
- event => {
123
- if ( onRowClick ) {
124
- onRowClick ( event , { index } ) ;
125
- }
126
- } ,
127
- [ index , onRowClick ]
128
- ) ;
129
-
130
173
const onExpanderClick = useCallback ( ( ) => {
131
174
dispatch ( { type : "updateExpanded" , key : generateKeyFromRow ( row , index ) } ) ;
132
175
expandedCalledRef . current = true ;
@@ -155,28 +198,6 @@ const Row = ({
155
198
expandedCalledRef . current = false ;
156
199
} , [ isExpanded , expandedCalledRef , resetHeight , index , clearSizeCache ] ) ;
157
200
158
- // components
159
- // row renderer
160
- const RowContainer = ( { children } : { children : React . ReactNode } ) => {
161
- if ( RowRenderer ) {
162
- const style = {
163
- ...containerStyle ,
164
- display : "flex"
165
- } ;
166
- return (
167
- < RowRenderer row = { row } index = { index } style = { style } >
168
- { children }
169
- </ RowRenderer >
170
- ) ;
171
- }
172
-
173
- return (
174
- < div className = "row-container" style = { containerStyle } onClick = { onContainerClick } >
175
- { children }
176
- </ div >
177
- ) ;
178
- } ;
179
-
180
201
return (
181
202
< div
182
203
ref = { rowRef }
@@ -185,7 +206,13 @@ const Row = ({
185
206
data-row-key = { rowKey }
186
207
style = { { ...style , borderBottom, width : useRowWidth ? style . width : undefined } }
187
208
>
188
- < RowContainer >
209
+ < RowContainer
210
+ row = { row }
211
+ index = { index }
212
+ onRowClick = { onRowClick }
213
+ rowRenderer = { rowRenderer }
214
+ containerStyle = { containerStyle }
215
+ >
189
216
{ columns . map ( ( c , i ) => (
190
217
< TableCell
191
218
key = { `${ uuid } -${ c . key } -${ key } ` }
0 commit comments