import { useState } from 'react'; import { Table, Thead, Tr, Th, Tbody, Td, TdProps, ExpandableRowContent } from '@patternfly/react-table'; import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon'; import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; interface Repository { name: string; branches: number; prs: number; workspaces: number; lastCommit: string; } export const TableCompoundExpandable: React.FunctionComponent = () => { // In real usage, this data would come from some external source like an API via props. const repositories: Repository[] = [ { name: 'siemur/test-space', branches: 10, prs: 4, workspaces: 4, lastCommit: '20 minutes' }, { name: 'siemur/test-space-2', branches: 3, prs: 4, workspaces: 4, lastCommit: '20 minutes' } ]; const columnNames = { name: 'Repositories', branches: 'Branches', prs: 'Pull requests', workspaces: 'Workspaces', lastCommit: 'Last commit' }; type ColumnKey = keyof typeof columnNames; // In this example, expanded cells are tracked by the repo and property names from each row. This could be any pair of unique identifiers. // This is to prevent state from being based on row and column order index in case we later add sorting and rearranging columns. // Note that this behavior is very similar to selection state. const [expandedCells, setExpandedCells] = useState>({ 'siemur/test-space': 'branches' // Default to the first cell of the first row being expanded }); const setCellExpanded = (repo: Repository, columnKey: ColumnKey, isExpanding = true) => { const newExpandedCells = { ...expandedCells }; if (isExpanding) { newExpandedCells[repo.name] = columnKey; } else { delete newExpandedCells[repo.name]; } setExpandedCells(newExpandedCells); }; const compoundExpandParams = ( repo: Repository, columnKey: ColumnKey, rowIndex: number, columnIndex: number ): TdProps['compoundExpand'] => ({ isExpanded: expandedCells[repo.name] === columnKey, onToggle: () => setCellExpanded(repo, columnKey, expandedCells[repo.name] !== columnKey), expandId: 'compound-expandable-example', rowIndex, columnIndex }); return ( {repositories.map((repo: Repository, rowIndex: number) => { const expandedCellKey = expandedCells[repo.name]; const isRowExpanded = !!expandedCellKey; return ( ); })}
{columnNames.name} {columnNames.branches} {columnNames.prs} {columnNames.workspaces} {columnNames.lastCommit}
{repo.name} {repo.branches} {repo.prs} {repo.workspaces} {repo.lastCommit} Open in GitHub
Expanded content for {repo.name}: branches goes here!
Expanded content for {repo.name}: prs goes here!
Expanded content for {repo.name}: workspaces goes here!
); };