File 0010-add-onExpand-prop-to-ListingTable.patch of Package cockpit

From a51d1777cc88f4df2e4aa37b2643bdf8862ac61c Mon Sep 17 00:00:00 2001
From: Alice Brooks <alice.brooks@suse.com>
Date: Wed, 26 Nov 2025 13:22:52 +0000
Subject: [PATCH] lib: add onExpand prop to ListingTable

---
 pkg/lib/cockpit-components-table.tsx | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/pkg/lib/cockpit-components-table.tsx b/pkg/lib/cockpit-components-table.tsx
index 839b3e1c9e94..8af8a4bbc32f 100644
--- a/pkg/lib/cockpit-components-table.tsx
+++ b/pkg/lib/cockpit-components-table.tsx
@@ -65,6 +65,7 @@ const _ = cockpit.gettext;
  * - sortMethod: callback function used for sorting rows. Called with 3 parameters: sortMethod(rows, activeSortDirection, activeSortIndex)
  * - style: object of additional css rules
  * - afterToggle: function to be called when content is toggled
+ * - onExpand: function to be called when content is expanded
  * - onSelect: function to be called when a checkbox is clicked. Called with 5 parameters:
  *   event, isSelected, rowIndex, rowData, extraData. rowData contains props with an id property of the clicked row.
  * - onHeaderSelect: event, isSelected.
@@ -98,9 +99,12 @@ export interface ListingTableColumnProps {
     props?: ThProps;
 }
 
+export type RowRecord = Record<string | number, boolean>;
+
 export interface ListingTableProps extends Omit<TableProps, 'rows' | 'onSelect'> {
     actions?: React.ReactNode[],
     afterToggle?: (expanded: boolean) => void,
+    onExpand?: (rows: RowRecord) => void,
     caption?: string,
     className?: string,
     columns: (string | ListingTableColumnProps)[],
@@ -121,6 +125,7 @@ export interface ListingTableProps extends Omit<TableProps, 'rows' | 'onSelect'>
 export const ListingTable = ({
     actions = [],
     afterToggle,
+    onExpand,
     caption = '',
     className = '',
     columns: cells = [],
@@ -139,7 +144,7 @@ export const ListingTable = ({
     ...extraProps
 } : ListingTableProps) => {
     let rows = [...tableRows];
-    const [expanded, setExpanded] = useState<Record<string | number, boolean>>({});
+    const [expanded, setExpanded] = useState<RowRecord>({});
     const [newItems, setNewItems] = useState<React.Key[]>([]);
     const [currentRowsKeys, setCurrentRowsKeys] = useState<React.Key[]>([]);
     const [activeSortIndex, setActiveSortIndex] = useState(sortBy?.index ?? 0);
@@ -165,6 +170,11 @@ export const ListingTable = ({
         setCurrentRowsKeys(crk => [...new Set([...crk, ..._rowKeys])]);
     }, [currentRowsKeysStr, rowKeysStr]);
 
+    useEffect(() => {
+        if (onExpand)
+            onExpand(expanded);
+    }, [expanded, onExpand]);
+
     const isSortable = cells.some(col => typeof col != "string" && col.sortable);
     const isExpandable = rows.some(row => row.expandedContent);
 
openSUSE Build Service is sponsored by