diff --git a/packages/lib/src/data-grid/DataGrid.tsx b/packages/lib/src/data-grid/DataGrid.tsx index 53f198551..ddaecb5cd 100644 --- a/packages/lib/src/data-grid/DataGrid.tsx +++ b/packages/lib/src/data-grid/DataGrid.tsx @@ -180,6 +180,7 @@ const DxcDataGrid = ({ const [rowsToRender, setRowsToRender] = useState([...rows]); const [page, changePage] = useState(defaultPage); const [colHeight, setColHeight] = useState(36); + const [loadingChildren, setLoadingChildren] = useState(false); const goToPage = (newPage: number) => { if (onPageChange) { @@ -232,8 +233,8 @@ const DxcDataGrid = ({ } const rowHasHierarchy = (row: GridRow | HierarchyGridRow): row is HierarchyGridRow => { return ( - (Array.isArray(row.childRows) && row.childRows.length > 0) || - typeof (row as HierarchyGridRow).childrenTrigger === "function" + (Array.isArray(row.childRows) && row.childRows?.length > 0) || + typeof (row as HierarchyGridRow)?.childrenTrigger === "function" ); }; if (!expandable && rows.some((row) => rowHasHierarchy(row)) && uniqueRowId) { @@ -252,7 +253,9 @@ const DxcDataGrid = ({ uniqueRowId, firstColumnKey, setRowsToRender, - row.childrenTrigger, + loadingChildren, + setLoadingChildren, + row.childrenTrigger )} ); diff --git a/packages/lib/src/data-grid/utils.tsx b/packages/lib/src/data-grid/utils.tsx index 112f115d5..c60e08f19 100644 --- a/packages/lib/src/data-grid/utils.tsx +++ b/packages/lib/src/data-grid/utils.tsx @@ -138,17 +138,18 @@ export const renderHierarchyTrigger = ( uniqueRowId: string, columnKey: string, setRowsToRender: (_value: SetStateAction) => void, + loading?: boolean, + setLoading?: (_value: SetStateAction) => void, childrenTrigger?: ( _open: boolean, _selectedRow: HierarchyGridRow ) => (HierarchyGridRow[] | GridRow[]) | Promise ) => { - const [loading, setLoading] = useState(false); const onClick = async () => { if (loading) return; // Prevent double clicks while loading if (!triggerRow.visibleChildren) { if (childrenTrigger) { - setLoading(true); + setLoading?.(true); triggerRow.loadingChildren = true; try { const dynamicChildren = await childrenTrigger(true, triggerRow); @@ -172,9 +173,9 @@ export const renderHierarchyTrigger = ( } catch (error) { console.error("Error loading children:", error); } finally { - setLoading(false); + setLoading?.(false); } - } else if (triggerRow.childRows) { + } else if (triggerRow?.childRows) { setRowsToRender((currentRows) => { const newRowsToRender = [...currentRows]; const rowIndex = currentRows.findIndex((row) => triggerRow === row);