@@ -35,166 +35,178 @@ const computeNbColumns = (expand, children, hasBulkActions) =>
35
35
36
36
const defaultClasses = { expandIconCell : '' , checkbox : '' , rowCell : '' } ;
37
37
38
- const DatagridRow : FC < DatagridRowProps > = ( {
39
- basePath,
40
- children,
41
- classes = defaultClasses ,
42
- className,
43
- expand,
44
- hasBulkActions,
45
- hover,
46
- id,
47
- onToggleItem,
48
- record,
49
- resource,
50
- rowClick,
51
- selected,
52
- style,
53
- selectable,
54
- ...rest
55
- } ) => {
56
- const [ expanded , toggleExpanded ] = useExpanded ( resource , id ) ;
57
- const [ nbColumns , setNbColumns ] = useState (
58
- computeNbColumns ( expand , children , hasBulkActions )
59
- ) ;
60
- useEffect ( ( ) => {
61
- // Fields can be hidden dynamically based on permissions;
62
- // The expand panel must span over the remaining columns
63
- // So we must recompute the number of columns to span on
64
- const newNbColumns = computeNbColumns ( expand , children , hasBulkActions ) ;
65
- if ( newNbColumns !== nbColumns ) {
66
- setNbColumns ( newNbColumns ) ;
67
- }
68
- } , [ expand , nbColumns , children , hasBulkActions ] ) ;
69
-
70
- const history = useHistory ( ) ;
71
-
72
- const handleToggleExpand = useCallback (
73
- event => {
74
- toggleExpanded ( ) ;
75
- event . stopPropagation ( ) ;
76
- } ,
77
- [ toggleExpanded ]
78
- ) ;
79
- const handleToggleSelection = useCallback (
80
- event => {
81
- if ( ! selectable ) return ;
82
- onToggleItem ( id ) ;
83
- event . stopPropagation ( ) ;
84
- } ,
85
- [ id , onToggleItem , selectable ]
86
- ) ;
87
- const handleClick = useCallback (
88
- async event => {
89
- if ( ! rowClick ) return ;
90
- event . persist ( ) ;
91
-
92
- const effect =
93
- typeof rowClick === 'function'
94
- ? await rowClick ( id , basePath , record )
95
- : rowClick ;
96
- switch ( effect ) {
97
- case 'edit' :
98
- history . push ( linkToRecord ( basePath , id ) ) ;
99
- return ;
100
- case 'show' :
101
- history . push ( linkToRecord ( basePath , id , 'show' ) ) ;
102
- return ;
103
- case 'expand' :
104
- handleToggleExpand ( event ) ;
105
- return ;
106
- case 'toggleSelection' :
107
- handleToggleSelection ( event ) ;
108
- return ;
109
- default :
110
- if ( effect ) history . push ( effect ) ;
111
- return ;
112
- }
113
- } ,
114
- [
38
+ const DatagridRow : FC < DatagridRowProps > = React . forwardRef (
39
+ (
40
+ {
115
41
basePath,
116
- history ,
117
- handleToggleExpand ,
118
- handleToggleSelection ,
42
+ children,
43
+ classes = defaultClasses ,
44
+ className,
45
+ expand,
46
+ hasBulkActions,
47
+ hover,
119
48
id,
49
+ onToggleItem,
120
50
record,
51
+ resource,
121
52
rowClick,
122
- ]
123
- ) ;
53
+ selected,
54
+ style,
55
+ selectable,
56
+ ...rest
57
+ } ,
58
+ ref
59
+ ) => {
60
+ const [ expanded , toggleExpanded ] = useExpanded ( resource , id ) ;
61
+ const [ nbColumns , setNbColumns ] = useState (
62
+ computeNbColumns ( expand , children , hasBulkActions )
63
+ ) ;
64
+ useEffect ( ( ) => {
65
+ // Fields can be hidden dynamically based on permissions;
66
+ // The expand panel must span over the remaining columns
67
+ // So we must recompute the number of columns to span on
68
+ const newNbColumns = computeNbColumns (
69
+ expand ,
70
+ children ,
71
+ hasBulkActions
72
+ ) ;
73
+ if ( newNbColumns !== nbColumns ) {
74
+ setNbColumns ( newNbColumns ) ;
75
+ }
76
+ } , [ expand , nbColumns , children , hasBulkActions ] ) ;
124
77
125
- return (
126
- < Fragment >
127
- < TableRow
128
- className = { className }
129
- key = { id }
130
- style = { style }
131
- hover = { hover }
132
- onClick = { handleClick }
133
- { ...rest }
134
- >
135
- { expand && (
136
- < TableCell
137
- padding = "none"
138
- className = { classes . expandIconCell }
139
- >
140
- < ExpandRowButton
141
- classes = { classes }
142
- expanded = { expanded }
143
- onClick = { handleToggleExpand }
144
- expandContentId = { `${ id } -expand` }
145
- />
146
- </ TableCell >
147
- ) }
148
- { hasBulkActions && (
149
- < TableCell padding = "checkbox" >
150
- { selectable && (
151
- < Checkbox
152
- color = "primary"
153
- className = { `select-item ${ classes . checkbox } ` }
154
- checked = { selected }
155
- onClick = { handleToggleSelection }
78
+ const history = useHistory ( ) ;
79
+
80
+ const handleToggleExpand = useCallback (
81
+ event => {
82
+ toggleExpanded ( ) ;
83
+ event . stopPropagation ( ) ;
84
+ } ,
85
+ [ toggleExpanded ]
86
+ ) ;
87
+ const handleToggleSelection = useCallback (
88
+ event => {
89
+ if ( ! selectable ) return ;
90
+ onToggleItem ( id ) ;
91
+ event . stopPropagation ( ) ;
92
+ } ,
93
+ [ id , onToggleItem , selectable ]
94
+ ) ;
95
+ const handleClick = useCallback (
96
+ async event => {
97
+ if ( ! rowClick ) return ;
98
+ event . persist ( ) ;
99
+
100
+ const effect =
101
+ typeof rowClick === 'function'
102
+ ? await rowClick ( id , basePath , record )
103
+ : rowClick ;
104
+ switch ( effect ) {
105
+ case 'edit' :
106
+ history . push ( linkToRecord ( basePath , id ) ) ;
107
+ return ;
108
+ case 'show' :
109
+ history . push ( linkToRecord ( basePath , id , 'show' ) ) ;
110
+ return ;
111
+ case 'expand' :
112
+ handleToggleExpand ( event ) ;
113
+ return ;
114
+ case 'toggleSelection' :
115
+ handleToggleSelection ( event ) ;
116
+ return ;
117
+ default :
118
+ if ( effect ) history . push ( effect ) ;
119
+ return ;
120
+ }
121
+ } ,
122
+ [
123
+ basePath ,
124
+ history ,
125
+ handleToggleExpand ,
126
+ handleToggleSelection ,
127
+ id ,
128
+ record ,
129
+ rowClick ,
130
+ ]
131
+ ) ;
132
+
133
+ return (
134
+ < Fragment >
135
+ < TableRow
136
+ ref = { ref }
137
+ className = { className }
138
+ key = { id }
139
+ style = { style }
140
+ hover = { hover }
141
+ onClick = { handleClick }
142
+ { ...rest }
143
+ >
144
+ { expand && (
145
+ < TableCell
146
+ padding = "none"
147
+ className = { classes . expandIconCell }
148
+ >
149
+ < ExpandRowButton
150
+ classes = { classes }
151
+ expanded = { expanded }
152
+ onClick = { handleToggleExpand }
153
+ expandContentId = { `${ id } -expand` }
156
154
/>
157
- ) }
158
- </ TableCell >
159
- ) }
160
- { React . Children . map ( children , ( field , index ) =>
161
- isValidElement ( field ) ? (
162
- < DatagridCell
163
- key = { `${ id } -${ ( field . props as any ) . source ||
164
- index } `}
165
- className = { classnames (
166
- `column-${ ( field . props as any ) . source } ` ,
167
- classes . rowCell
155
+ </ TableCell >
156
+ ) }
157
+ { hasBulkActions && (
158
+ < TableCell padding = "checkbox" >
159
+ { selectable && (
160
+ < Checkbox
161
+ color = "primary"
162
+ className = { `select-item ${
163
+ classes . checkbox
164
+ } `}
165
+ checked = { selected }
166
+ onClick = { handleToggleSelection }
167
+ />
168
168
) }
169
- record = { record }
170
- { ...{ field, basePath, resource } }
171
- />
172
- ) : null
173
- ) }
174
- </ TableRow >
175
- { expand && expanded && (
176
- < TableRow key = { `${ id } -expand` } id = { `${ id } -expand` } >
177
- < TableCell colSpan = { nbColumns } >
178
- { isValidElement ( expand )
179
- ? cloneElement ( expand , {
180
- // @ts -ignore
181
- record,
182
- basePath,
183
- resource,
184
- id : String ( id ) ,
185
- } )
186
- : createElement ( expand , {
187
- record,
188
- basePath,
189
- resource,
190
- id : String ( id ) ,
191
- } ) }
192
- </ TableCell >
169
+ </ TableCell >
170
+ ) }
171
+ { React . Children . map ( children , ( field , index ) =>
172
+ isValidElement ( field ) ? (
173
+ < DatagridCell
174
+ key = { `${ id } -${ ( field . props as any ) . source ||
175
+ index } `}
176
+ className = { classnames (
177
+ `column-${ ( field . props as any ) . source } ` ,
178
+ classes . rowCell
179
+ ) }
180
+ record = { record }
181
+ { ...{ field, basePath, resource } }
182
+ />
183
+ ) : null
184
+ ) }
193
185
</ TableRow >
194
- ) }
195
- </ Fragment >
196
- ) ;
197
- } ;
186
+ { expand && expanded && (
187
+ < TableRow key = { `${ id } -expand` } id = { `${ id } -expand` } >
188
+ < TableCell colSpan = { nbColumns } >
189
+ { isValidElement ( expand )
190
+ ? cloneElement ( expand , {
191
+ // @ts -ignore
192
+ record,
193
+ basePath,
194
+ resource,
195
+ id : String ( id ) ,
196
+ } )
197
+ : createElement ( expand , {
198
+ record,
199
+ basePath,
200
+ resource,
201
+ id : String ( id ) ,
202
+ } ) }
203
+ </ TableCell >
204
+ </ TableRow >
205
+ ) }
206
+ </ Fragment >
207
+ ) ;
208
+ }
209
+ ) ;
198
210
199
211
DatagridRow . propTypes = {
200
212
basePath : PropTypes . string ,
0 commit comments