@@ -59,6 +59,35 @@ export const renderSortStatus = ({ sortDirection }: RenderSortStatusProps) => (
5959 </ div >
6060) ;
6161
62+ /**
63+ * Expands a given row by inserting a new child row with the expanded content.
64+ * @param {ExpandableGridRow } row - The row object to expand.
65+ * @param {ExpandableGridRow[] } rows - The current list of all rows (as rendered).
66+ * @param {string } uniqueRowId - Unique identifier key used for each row.
67+ */
68+ export const expandRow = ( row : ExpandableGridRow , rows : ExpandableGridRow [ ] , uniqueRowId : string ) => {
69+ const rowIndex = rows . findIndex ( ( r ) => r === row ) ;
70+ addRow ( rows , rowIndex + 1 , {
71+ isExpandedChildContent : true ,
72+ [ uniqueRowId ] : `${ rowKeyGetter ( row , uniqueRowId ) } _expanded` ,
73+ expandedChildContent : row . expandedContent ,
74+ triggerRowKey : rowKeyGetter ( row , uniqueRowId ) ,
75+ expandedContentHeight : row . expandedContentHeight ,
76+ } ) ;
77+ } ;
78+
79+ /**
80+ * Collapses a given row by removing its expanded child row.
81+ * @param {ExpandableGridRow } row - The row object to collapse.
82+ * @param {ExpandableGridRow[] } rows - The current list of all rows (as rendered).
83+ */
84+ export const collapseRow = ( row : ExpandableGridRow , rows : ExpandableGridRow [ ] ) => {
85+ const rowIndex = rows . findIndex ( ( r ) => r === row ) ;
86+ const newRows = [ ...rows ] ;
87+ deleteRow ( newRows , rowIndex + 1 ) ;
88+ return newRows ;
89+ } ;
90+
6291/**
6392 * Renders an expandable trigger icon that toggles row expansion.
6493 * @param {ExpandableGridRow } row - Row object that can be expanded or collapsed.
@@ -80,25 +109,13 @@ export const renderExpandableTrigger = (
80109 onClick = { ( ) => {
81110 row . contentIsExpanded = ! row . contentIsExpanded ;
82111 if ( row . contentIsExpanded ) {
83- const rowIndex = rows . findIndex ( ( rowToRender ) => row === rowToRender ) ;
84112 setRowsToRender ( ( currentRows ) => {
85- const newRows = [ ...currentRows ] ;
86- addRow ( newRows , rowIndex + 1 , {
87- isExpandedChildContent : row . contentIsExpanded ,
88- [ uniqueRowId ] : `${ rowKeyGetter ( row , uniqueRowId ) } _expanded` ,
89- expandedChildContent : row . expandedContent ,
90- triggerRowKey : rowKeyGetter ( row , uniqueRowId ) ,
91- expandedContentHeight : row . expandedContentHeight ,
92- } ) ;
93- return newRows ;
113+ const finalRows = [ ...currentRows ] ;
114+ expandRow ( row , finalRows , uniqueRowId ) ;
115+ return finalRows ;
94116 } ) ;
95117 } else {
96- const rowIndex = rows . findIndex ( ( rowToRender ) => row === rowToRender ) ;
97- setRowsToRender ( ( currentRows ) => {
98- const newRows = [ ...currentRows ] ;
99- deleteRow ( newRows , rowIndex + 1 ) ;
100- return newRows ;
101- } ) ;
118+ setRowsToRender ( ( currentRows ) => collapseRow ( row , [ ...currentRows ] ) ) ;
102119 }
103120 } }
104121 disabled = { ! rows . some ( ( row ) => uniqueRowId in row ) }
@@ -136,11 +153,9 @@ export const renderHierarchyTrigger = (
136153 } ) ;
137154 } else {
138155 // The children of the row that is being collapsed are added to an array
139- const rowsToRemove : HierarchyGridRow [ ] = [
140- ...rows . filter (
141- ( rowToRender ) => rowToRender . parentKey && rowToRender . parentKey === rowKeyGetter ( triggerRow , uniqueRowId )
142- ) ,
143- ] ;
156+ const rowsToRemove : HierarchyGridRow [ ] = rows . filter (
157+ ( rowToRender ) => rowToRender . parentKey && rowToRender . parentKey === rowKeyGetter ( triggerRow , uniqueRowId )
158+ ) ;
144159 // The children are checked if any of them has any other children of their own
145160 const rowsToCheck = [ ...rowsToRemove ] ;
146161 while ( rowsToCheck . length > 0 ) {
0 commit comments