@@ -138,58 +138,73 @@ export const renderHierarchyTrigger = (
138
138
uniqueRowId : string ,
139
139
columnKey : string ,
140
140
setRowsToRender : ( _value : SetStateAction < GridRow [ ] | ExpandableGridRow [ ] | HierarchyGridRow [ ] > ) => void ,
141
- loading ?: boolean ,
142
- setLoading ?: ( _value : SetStateAction < boolean > ) => void ,
141
+ loadingChildren ?: ( string | number ) [ ] ,
142
+ setLoadingChildren ?: ( _value : SetStateAction < ( string | number ) [ ] > ) => void ,
143
143
childrenTrigger ?: (
144
144
_open : boolean ,
145
145
_selectedRow : HierarchyGridRow
146
146
) => ( HierarchyGridRow [ ] | GridRow [ ] ) | Promise < HierarchyGridRow [ ] | GridRow [ ] >
147
147
) => {
148
- const onClick = async ( ) => {
149
- if ( loading ) return ; // Prevent double clicks while loading
150
- if ( ! triggerRow . visibleChildren ) {
151
- if ( childrenTrigger ) {
152
- setLoading ?.( true ) ;
153
- triggerRow . loadingChildren = true ;
154
- try {
155
- const dynamicChildren = await childrenTrigger ( true , triggerRow ) ;
156
- triggerRow . childRows = dynamicChildren ;
157
-
158
- setRowsToRender ( ( currentRows ) => {
159
- const newRowsToRender = [ ...currentRows ] ;
160
- // Prevents adding children if the triggerRow has been removed
161
- if ( newRowsToRender . some ( ( row ) => rowKeyGetter ( row , uniqueRowId ) === triggerRow [ uniqueRowId ] ) ) {
162
- const rowIndex = currentRows . findIndex ( ( row ) => triggerRow === row ) ;
163
-
164
- dynamicChildren . forEach ( ( childRow : HierarchyGridRow , index : number ) => {
165
- childRow . rowLevel =
166
- triggerRow . rowLevel && typeof triggerRow . rowLevel === "number" ? triggerRow . rowLevel + 1 : 1 ;
167
- childRow . parentKey = rowKeyGetter ( triggerRow , uniqueRowId ) ;
168
- addRow ( newRowsToRender , rowIndex + 1 + index , childRow ) ;
169
- } ) ;
170
- }
171
- return newRowsToRender ;
172
- } ) ;
173
- } catch ( error ) {
174
- console . error ( "Error loading children:" , error ) ;
175
- } finally {
176
- setLoading ?.( false ) ;
177
- }
178
- } else if ( triggerRow ?. childRows ) {
148
+ const isLoading = ! ! loadingChildren ?. includes ( rowKeyGetter ( triggerRow , uniqueRowId ) ) ;
149
+ const expandChildren = async ( ) => {
150
+ if ( childrenTrigger && ! triggerRow . childRows ?. length ) {
151
+ setLoadingChildren ?.( ( currentLoadingChildren ) => [
152
+ ...currentLoadingChildren ,
153
+ rowKeyGetter ( triggerRow , uniqueRowId ) ,
154
+ ] ) ;
155
+ triggerRow . loadingChildren = true ;
156
+ try {
157
+ const dynamicChildren = await childrenTrigger ( true , triggerRow ) ;
158
+ triggerRow . childRows = dynamicChildren ;
159
+
179
160
setRowsToRender ( ( currentRows ) => {
180
161
const newRowsToRender = [ ...currentRows ] ;
181
- const rowIndex = currentRows . findIndex ( ( row ) => triggerRow === row ) ;
182
-
183
- triggerRow . childRows ? .forEach ( ( childRow : HierarchyGridRow , index : number ) => {
184
- childRow . rowLevel =
185
- triggerRow . rowLevel && typeof triggerRow . rowLevel === "number" ? triggerRow . rowLevel + 1 : 1 ;
186
- childRow . parentKey = rowKeyGetter ( triggerRow , uniqueRowId ) ;
187
- addRow ( newRowsToRender , rowIndex + 1 + index , childRow ) ;
188
- } ) ;
189
-
162
+ if ( newRowsToRender . some ( ( row ) => rowKeyGetter ( row , uniqueRowId ) === triggerRow [ uniqueRowId ] ) ) {
163
+ const rowIndex = currentRows . findIndex ( ( row ) => triggerRow === row ) ;
164
+ dynamicChildren . forEach ( ( childRow : HierarchyGridRow , index : number ) => {
165
+ childRow . rowLevel =
166
+ triggerRow . rowLevel && typeof triggerRow . rowLevel === "number" ? triggerRow . rowLevel + 1 : 1 ;
167
+ childRow . parentKey = rowKeyGetter ( triggerRow , uniqueRowId ) ;
168
+ addRow ( newRowsToRender , rowIndex + 1 + index , childRow ) ;
169
+ } ) ;
170
+ }
190
171
return newRowsToRender ;
191
172
} ) ;
173
+ } catch ( e ) {
174
+ console . error ( "Error loading children:" , e ) ;
175
+ } finally {
176
+ setLoadingChildren ?.( ( currentLoadingChildren ) =>
177
+ currentLoadingChildren . filter ( ( key ) => key !== rowKeyGetter ( triggerRow , uniqueRowId ) )
178
+ ) ;
192
179
}
180
+ } else if ( triggerRow ?. childRows ) {
181
+ setRowsToRender ( ( currentRows ) => {
182
+ const newRowsToRender = [ ...currentRows ] ;
183
+ const rowIndex = currentRows . findIndex ( ( row ) => triggerRow === row ) ;
184
+
185
+ triggerRow . childRows ?. forEach ( ( childRow : HierarchyGridRow , index : number ) => {
186
+ childRow . rowLevel =
187
+ triggerRow . rowLevel && typeof triggerRow . rowLevel === "number" ? triggerRow . rowLevel + 1 : 1 ;
188
+ childRow . parentKey = rowKeyGetter ( triggerRow , uniqueRowId ) ;
189
+ addRow ( newRowsToRender , rowIndex + 1 + index , childRow ) ;
190
+ } ) ;
191
+
192
+ return newRowsToRender ;
193
+ } ) ;
194
+ }
195
+ } ;
196
+ if (
197
+ triggerRow . visibleChildren &&
198
+ triggerRow . childRows ?. length &&
199
+ ! rows . some ( ( row ) => row . parentKey === rowKeyGetter ( triggerRow , uniqueRowId ) )
200
+ ) {
201
+ expandChildren ( ) ;
202
+ }
203
+ const onClick = async ( ) => {
204
+ if ( isLoading ) return ; // Prevent double clicks while loading
205
+ triggerRow . visibleChildren = ! triggerRow . visibleChildren ;
206
+ if ( triggerRow . visibleChildren ) {
207
+ await expandChildren ( ) ;
193
208
} else {
194
209
setRowsToRender ( ( currentRows ) => {
195
210
// The children of the row that is being collapsed are added to an array
@@ -221,12 +236,10 @@ export const renderHierarchyTrigger = (
221
236
return newRowsToRender ;
222
237
} ) ;
223
238
}
224
-
225
- triggerRow . visibleChildren = ! triggerRow . visibleChildren ;
226
239
} ;
227
240
return (
228
241
< button type = "button" disabled = { ! rows . some ( ( row ) => uniqueRowId in row ) } onClick = { onClick } >
229
- { loading ? (
242
+ { isLoading ? (
230
243
< DxcSpinner mode = "small" />
231
244
) : (
232
245
< DxcIcon icon = { triggerRow . visibleChildren ? "Keyboard_Arrow_Down" : "Chevron_Right" } />
@@ -264,10 +277,7 @@ export const renderCheckbox = (
264
277
onChange = { ( checked ) => {
265
278
handleCheckboxUpdate ( rows , row , uniqueRowId , selectedRows , checked , onSelectRows ) ;
266
279
} }
267
- disabled = {
268
- // row.loadingChildren ||
269
- ! rows . some ( ( row ) => uniqueRowId in row )
270
- }
280
+ disabled = { ! rows . some ( ( row ) => uniqueRowId in row ) }
271
281
/>
272
282
) ;
273
283
} ;
0 commit comments