| 1 |
import { useState } from 'react'; |
| 2 |
import { Table, Thead, Tr, Th, Tbody, Td, TdProps, ExpandableRowContent } from '@patternfly/react-table'; |
| 3 |
|
| 4 |
import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon'; |
| 5 |
import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; |
| 6 |
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; |
| 7 |
|
| 8 |
interface Repository { |
| 9 |
name: string; |
| 10 |
branches: number; |
| 11 |
prs: number; |
| 12 |
workspaces: number; |
| 13 |
lastCommit: string; |
| 14 |
} |
| 15 |
|
| 16 |
export const TableCompoundExpandable: React.FunctionComponent = () => { |
| 17 |
// In real usage, this data would come from some external source like an API via props. |
| 18 |
const repositories: Repository[] = [ |
| 19 |
{ name: 'siemur/test-space', branches: 10, prs: 4, workspaces: 4, lastCommit: '20 minutes' }, |
| 20 |
{ name: 'siemur/test-space-2', branches: 3, prs: 4, workspaces: 4, lastCommit: '20 minutes' } |
| 21 |
]; |
| 22 |
|
| 23 |
const columnNames = { |
| 24 |
name: 'Repositories', |
| 25 |
branches: 'Branches', |
| 26 |
prs: 'Pull requests', |
| 27 |
workspaces: 'Workspaces', |
| 28 |
lastCommit: 'Last commit' |
| 29 |
}; |
| 30 |
type ColumnKey = keyof typeof columnNames; |
| 31 |
|
| 32 |
// In this example, expanded cells are tracked by the repo and property names from each row. This could be any pair of unique identifiers. |
| 33 |
// This is to prevent state from being based on row and column order index in case we later add sorting and rearranging columns. |
| 34 |
// Note that this behavior is very similar to selection state. |
| 35 |
const [expandedCells, setExpandedCells] = useState<Record<string, ColumnKey>>({ |
| 36 |
'siemur/test-space': 'branches' // Default to the first cell of the first row being expanded |
| 37 |
}); |
| 38 |
const setCellExpanded = (repo: Repository, columnKey: ColumnKey, isExpanding = true) => { |
| 39 |
const newExpandedCells = { ...expandedCells }; |
| 40 |
if (isExpanding) { |
| 41 |
newExpandedCells[repo.name] = columnKey; |
| 42 |
} else { |
| 43 |
delete newExpandedCells[repo.name]; |
| 44 |
} |
| 45 |
setExpandedCells(newExpandedCells); |
| 46 |
}; |
| 47 |
const compoundExpandParams = ( |
| 48 |
repo: Repository, |
| 49 |
columnKey: ColumnKey, |
| 50 |
rowIndex: number, |
| 51 |
columnIndex: number |
| 52 |
): TdProps['compoundExpand'] => ({ |
| 53 |
isExpanded: expandedCells[repo.name] === columnKey, |
| 54 |
onToggle: () => setCellExpanded(repo, columnKey, expandedCells[repo.name] !== columnKey), |
| 55 |
expandId: 'compound-expandable-example', |
| 56 |
rowIndex, |
| 57 |
columnIndex |
| 58 |
}); |
| 59 |
|
| 60 |
return ( |
| 61 |
<Table aria-label="Compound expandable table" isExpandable hasAnimations> |
| 62 |
<Thead> |
| 63 |
<Tr> |
| 64 |
<Th>{columnNames.name}</Th> |
| 65 |
<Th>{columnNames.branches}</Th> |
| 66 |
<Th>{columnNames.prs}</Th> |
| 67 |
<Th>{columnNames.workspaces}</Th> |
| 68 |
<Th>{columnNames.lastCommit}</Th> |
| 69 |
<Th screenReaderText="URL" /> |
| 70 |
</Tr> |
| 71 |
</Thead> |
| 72 |
{repositories.map((repo: Repository, rowIndex: number) => { |
| 73 |
const expandedCellKey = expandedCells[repo.name]; |
| 74 |
const isRowExpanded = !!expandedCellKey; |
| 75 |
return ( |
| 76 |
<Tbody key={repo.name} isExpanded={isRowExpanded}> |
| 77 |
<Tr isContentExpanded={isRowExpanded} isControlRow> |
| 78 |
<Td width={25} dataLabel={columnNames.name} component="th"> |
| 79 |
<a href="#">{repo.name}</a> |
| 80 |
</Td> |
| 81 |
<Td |
| 82 |
width={10} |
| 83 |
dataLabel={columnNames.branches} |
| 84 |
compoundExpand={compoundExpandParams(repo, 'branches', rowIndex, 1)} |
| 85 |
> |
| 86 |
<RhUiBranchFillIcon key="icon" /> {repo.branches} |
| 87 |
</Td> |
| 88 |
<Td |
| 89 |
width={10} |
| 90 |
dataLabel={columnNames.prs} |
| 91 |
compoundExpand={compoundExpandParams(repo, 'prs', rowIndex, 2)} |
| 92 |
> |
| 93 |
<RhUiCodeIcon key="icon" /> {repo.prs} |
| 94 |
</Td> |
| 95 |
<Td |
| 96 |
width={10} |
| 97 |
dataLabel={columnNames.workspaces} |
| 98 |
compoundExpand={compoundExpandParams(repo, 'workspaces', rowIndex, 3)} |
| 99 |
> |
| 100 |
<CubeIcon key="icon" /> {repo.workspaces} |
| 101 |
</Td> |
| 102 |
<Td width={15} dataLabel={columnNames.lastCommit}> |
| 103 |
{repo.lastCommit} |
| 104 |
</Td> |
| 105 |
<Td width={30}> |
| 106 |
<a href="#">Open in GitHub</a> |
| 107 |
</Td> |
| 108 |
</Tr> |
| 109 |
<Tr isExpanded={columnNames[expandedCellKey] === columnNames.branches}> |
| 110 |
<Td dataLabel={columnNames[expandedCellKey]} colSpan={6}> |
| 111 |
<ExpandableRowContent> |
| 112 |
<div>Expanded content for {repo.name}: branches goes here!</div> |
| 113 |
</ExpandableRowContent> |
| 114 |
</Td> |
| 115 |
</Tr> |
| 116 |
<Tr isExpanded={columnNames[expandedCellKey] === columnNames.prs}> |
| 117 |
<Td dataLabel={columnNames[expandedCellKey]} colSpan={6}> |
| 118 |
<ExpandableRowContent> |
| 119 |
<div>Expanded content for {repo.name}: prs goes here!</div> |
| 120 |
</ExpandableRowContent> |
| 121 |
</Td> |
| 122 |
</Tr> |
| 123 |
<Tr isExpanded={columnNames[expandedCellKey] === columnNames.workspaces}> |
| 124 |
<Td dataLabel={columnNames[expandedCellKey]} colSpan={6}> |
| 125 |
<ExpandableRowContent> |
| 126 |
<div>Expanded content for {repo.name}: workspaces goes here!</div> |
| 127 |
</ExpandableRowContent> |
| 128 |
</Td> |
| 129 |
</Tr> |
| 130 |
</Tbody> |
| 131 |
); |
| 132 |
})} |
| 133 |
</Table> |
| 134 |
); |
| 135 |
}; |