import { FC, useMemo, useState, useRef } from 'react'; import { ExpandableRowContent, InnerScrollContainer, OuterScrollContainer, Table, TableProps, Tbody, Td, Tr, } from '@patternfly/react-table'; import { useInternalContext } from '../InternalContext'; import { DataViewTableHead } from '../DataViewTableHead'; import { DataViewTh, DataViewTr, isDataViewTdObject, isDataViewTrObject } from '../DataViewTable'; import { DataViewState } from '../DataView/DataView'; export interface ExpandableContent { rowId: string | number; columnId?: number; content: React.ReactNode; } /** extends TableProps */ export interface DataViewTableBasicProps extends Omit { /** Columns definition */ columns: DataViewTh[]; /** Current page rows */ rows: DataViewTr[]; /** Expanded rows content */ expandedRows?: ExpandableContent[]; /** Table head states to be displayed when active */ headStates?: Partial> /** Table body states to be displayed when active */ bodyStates?: Partial> /** Custom OUIA ID */ ouiaId?: string; /** @hide Indicates if the table is resizable */ hasResizableColumns?: boolean; /** Toggles expandable */ isExpandable?: boolean; /** Toggles sticky columns and header */ isSticky?: boolean; /** Property name used as unique row identifier for expansion tracking. When provided, expanded state follows the data through sort/filter/pagination instead of using the array index. Applicable when rows are objects with an identifying property (e.g., `id`). */ indexBy?: string; } export const DataViewTableBasic: FC = ({ columns, rows, expandedRows, ouiaId = 'DataViewTableBasic', headStates, bodyStates, hasResizableColumns, isExpandable = false, isSticky = false, indexBy, ...props }: DataViewTableBasicProps) => { const { selection, activeState, isSelectable } = useInternalContext(); const { onSelect, isSelected, isSelectDisabled } = selection ?? {}; const activeHeadState = useMemo(() => activeState ? headStates?.[activeState] : undefined, [ activeState, headStates ]); const activeBodyState = useMemo(() => activeState ? bodyStates?.[activeState] : undefined, [ activeState, bodyStates ]); const [expandedRowsState, setExpandedRowsState] = useState>({}) const [expandedColumnIndex, setExpandedColumnIndex] = useState>({}) const tableRef = useRef(null); const needsSeparateTbody = isExpandable; const isCompoundExpandable = isExpandable && !!expandedRows?.length && expandedRows.every((row) => row.columnId !== undefined); const renderedRows = useMemo(() => rows.map((row, rowIndex) => { const rowIsObject = isDataViewTrObject(row); // Resolve the key used for expansion state tracking const rowKey: string | number = indexBy && rowIsObject && indexBy in row ? String((row as unknown as Record)[indexBy]) : rowIndex; const isRowExpanded = expandedRowsState[rowKey] || false; const expandedColIndex = expandedColumnIndex[rowKey]; // Get the first cell to extract the row ID (used for expandable content matching when indexBy is not set) const rowData = rowIsObject ? row.row : row; const firstCell = rowData[0]; const rowId = isDataViewTdObject(firstCell) ? (firstCell as { id?: string | number }).id : undefined; const matchesRow = (content: ExpandableContent) => indexBy ? String(content.rowId) === String(rowKey) : content.rowId === rowId; // Find all expandable contents for this row const rowExpandableContents = isExpandable ? expandedRows?.filter(matchesRow) : []; const rowContent = ( {isSelectable && ( { onSelect?.(isSelecting, rowIsObject ? row : [ row ]); }, isSelected: isSelected?.(row) || false, isDisabled: isSelectDisabled?.(row) || false, }} /> )} {isExpandable && !isCompoundExpandable && ( rowExpandableContents?.length ? setExpandedRowsState(prev => ({ ...prev, [rowKey]: !prev[rowKey] })), }} /> : )} {(rowIsObject ? row.row : row).map((cell, colIndex) => { const cellIsObject = isDataViewTdObject(cell); const cellExpandableContent = isCompoundExpandable ? expandedRows?.find((entry) => matchesRow(entry) && entry.columnId === colIndex) : undefined; return ( { setExpandedRowsState(prev => { const isSameColumn = expandedColIndex === colIndex; const wasExpanded = prev[rowKey]; return { ...prev, [rowKey]: isSameColumn ? !wasExpanded : true }; }); setExpandedColumnIndex(prev => ({ ...prev, [rowKey]: colIndex })); }, rowIndex, columnIndex: colIndex } })} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`} > {cellIsObject ? cell.cell : cell} ); })} ); if (needsSeparateTbody) { return ( {rowContent} {rowExpandableContents?.map((expandableContent, expandIndex) => ( {expandableContent.content} ))} ); } else { return rowContent; } }), [ rows, isSelectable, isSelected, isSelectDisabled, onSelect, ouiaId, expandedRowsState, expandedColumnIndex, expandedRows, isExpandable, isCompoundExpandable, needsSeparateTbody, indexBy ]); const bodyContent = activeBodyState || (needsSeparateTbody ? renderedRows : {renderedRows}); if (isSticky) { return ( { activeHeadState || } { bodyContent }
); } else { return ( { activeHeadState || } { bodyContent }
); } }; export default DataViewTableBasic;