From 76b081694ab0a0866922c70ef3db7b9f1b419436 Mon Sep 17 00:00:00 2001 From: Mil4n0r Date: Mon, 22 Sep 2025 10:23:37 +0200 Subject: [PATCH 1/2] Fixed problem when setting the hierarchical grid to an empty array --- packages/lib/src/data-grid/DataGrid.tsx | 9 ++++++--- packages/lib/src/data-grid/utils.tsx | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/lib/src/data-grid/DataGrid.tsx b/packages/lib/src/data-grid/DataGrid.tsx index 53f198551..f8e3736e3 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..536c754f5 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); @@ -174,12 +175,12 @@ export const renderHierarchyTrigger = ( } finally { setLoading(false); } - } else if (triggerRow.childRows) { + } else if (triggerRow?.childRows) { setRowsToRender((currentRows) => { const newRowsToRender = [...currentRows]; const rowIndex = currentRows.findIndex((row) => triggerRow === row); - triggerRow.childRows?.forEach((childRow: HierarchyGridRow, index: number) => { + triggerRow?.childRows?.forEach((childRow: HierarchyGridRow, index: number) => { childRow.rowLevel = triggerRow.rowLevel && typeof triggerRow.rowLevel === "number" ? triggerRow.rowLevel + 1 : 1; childRow.parentKey = rowKeyGetter(triggerRow, uniqueRowId); From a46de51827962492b684bb0aa59b3d2fa5fa500b Mon Sep 17 00:00:00 2001 From: Mil4n0r Date: Mon, 22 Sep 2025 10:26:03 +0200 Subject: [PATCH 2/2] Minor refactoring --- packages/lib/src/data-grid/DataGrid.tsx | 2 +- packages/lib/src/data-grid/utils.tsx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/lib/src/data-grid/DataGrid.tsx b/packages/lib/src/data-grid/DataGrid.tsx index f8e3736e3..ddaecb5cd 100644 --- a/packages/lib/src/data-grid/DataGrid.tsx +++ b/packages/lib/src/data-grid/DataGrid.tsx @@ -233,7 +233,7 @@ const DxcDataGrid = ({ } const rowHasHierarchy = (row: GridRow | HierarchyGridRow): row is HierarchyGridRow => { return ( - (Array.isArray(row?.childRows) && row?.childRows?.length > 0) || + (Array.isArray(row.childRows) && row.childRows?.length > 0) || typeof (row as HierarchyGridRow)?.childrenTrigger === "function" ); }; diff --git a/packages/lib/src/data-grid/utils.tsx b/packages/lib/src/data-grid/utils.tsx index 536c754f5..c60e08f19 100644 --- a/packages/lib/src/data-grid/utils.tsx +++ b/packages/lib/src/data-grid/utils.tsx @@ -173,14 +173,14 @@ export const renderHierarchyTrigger = ( } catch (error) { console.error("Error loading children:", error); } finally { - setLoading(false); + setLoading?.(false); } } else if (triggerRow?.childRows) { setRowsToRender((currentRows) => { const newRowsToRender = [...currentRows]; const rowIndex = currentRows.findIndex((row) => triggerRow === row); - triggerRow?.childRows?.forEach((childRow: HierarchyGridRow, index: number) => { + triggerRow.childRows?.forEach((childRow: HierarchyGridRow, index: number) => { childRow.rowLevel = triggerRow.rowLevel && typeof triggerRow.rowLevel === "number" ? triggerRow.rowLevel + 1 : 1; childRow.parentKey = rowKeyGetter(triggerRow, uniqueRowId);