Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/lib/src/data-grid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const DxcDataGrid = ({
const [rowsToRender, setRowsToRender] = useState<GridRow[] | HierarchyGridRow[] | ExpandableGridRow[]>([...rows]);
const [page, changePage] = useState(defaultPage);
const [colHeight, setColHeight] = useState(36);
const [loadingChildren, setLoadingChildren] = useState(false);

const goToPage = (newPage: number) => {
if (onPageChange) {
Expand Down Expand Up @@ -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) {
Expand All @@ -252,7 +253,9 @@ const DxcDataGrid = ({
uniqueRowId,
firstColumnKey,
setRowsToRender,
row.childrenTrigger,
loadingChildren,
setLoadingChildren,
row.childrenTrigger
)}
</HierarchyContainer>
);
Expand Down
9 changes: 5 additions & 4 deletions packages/lib/src/data-grid/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ export const renderHierarchyTrigger = (
uniqueRowId: string,
columnKey: string,
setRowsToRender: (_value: SetStateAction<GridRow[] | ExpandableGridRow[] | HierarchyGridRow[]>) => void,
loading?: boolean,
setLoading?: (_value: SetStateAction<boolean>) => void,
childrenTrigger?: (
_open: boolean,
_selectedRow: HierarchyGridRow
) => (HierarchyGridRow[] | GridRow[]) | Promise<HierarchyGridRow[] | GridRow[]>
) => {
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);
Expand All @@ -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);
Expand Down