| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { Table, TableHeader, TableBody, TableProps } from '@patternfly/react-table/deprecated'; |
| 3 |
import { compoundExpand } from '@patternfly/react-table'; |
| 4 |
|
| 5 |
import RhUiCodeIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-code-icon'; |
| 6 |
import RhUiBranchFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-branch-fill-icon'; |
| 7 |
import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon'; |
| 8 |
import spacing from '@patternfly/react-styles/css/utilities/Spacing/spacing'; |
| 9 |
|
| 10 |
interface Repository { |
| 11 |
name: string; |
| 12 |
branches: number; |
| 13 |
prs: number; |
| 14 |
workspaces: number; |
| 15 |
lastCommit: string; |
| 16 |
} |
| 17 |
|
| 18 |
export const LegacyTableCompoundExpandable: React.FunctionComponent = () => { |
| 19 |
// In real usage, this data would come from some external source like an API via props. |
| 20 |
const repositories: Repository[] = [ |
| 21 |
{ name: 'siemur/test-space', branches: 10, prs: 4, workspaces: 4, lastCommit: '20 minutes' }, |
| 22 |
{ name: 'siemur/test-space-2', branches: 3, prs: 4, workspaces: 4, lastCommit: '20 minutes' } |
| 23 |
]; |
| 24 |
|
| 25 |
// In this example, expanded cells are tracked by the repo and property names from each row. This could be any pair of unique identifiers. |
| 26 |
// This is to prevent state from being based on row and column order index in case we later add sorting and rearranging columns. |
| 27 |
// Note that these columnKeys must be in the same order as the `columns` below, because columnIndex will be used to look up the matching key. |
| 28 |
type ColumnKey = 'name' | 'branches' | 'prs' | 'workspaces' | 'lastCommit'; // For typechecking |
| 29 |
const columnKeys: ColumnKey[] = ['name', 'branches', 'prs', 'workspaces', 'lastCommit']; // For indexed order |
| 30 |
const [expandedCells, setExpandedCells] = useState<Record<string, ColumnKey>>({ |
| 31 |
'siemur/test-space': 'branches' // Default to the first cell of the first row being expanded |
| 32 |
}); |
| 33 |
const setCellExpanded = (repo: Repository, columnKey: ColumnKey, isExpanding = true) => { |
| 34 |
const newExpandedCells = { ...expandedCells }; |
| 35 |
if (isExpanding) { |
| 36 |
newExpandedCells[repo.name] = columnKey; |
| 37 |
} else { |
| 38 |
delete newExpandedCells[repo.name]; |
| 39 |
} |
| 40 |
setExpandedCells(newExpandedCells); |
| 41 |
}; |
| 42 |
|
| 43 |
// We want to be able to reference the original data object based on row index. But because parent/child rows are |
| 44 |
// multiple elements in the rows array, repositories[rowIndex] will not be accurate like it would in a normal table. |
| 45 |
// One solution to this is to create an array of data objects indexed by the displayed row index. |
| 46 |
const reposByRowIndex: (Repository | null)[] = []; |
| 47 |
|
| 48 |
const columns: TableProps['cells'] = [ |
| 49 |
'Repositories', |
| 50 |
{ |
| 51 |
title: 'Branches', |
| 52 |
cellTransforms: [compoundExpand] |
| 53 |
}, |
| 54 |
{ |
| 55 |
title: 'Pull requests', |
| 56 |
cellTransforms: [compoundExpand] |
| 57 |
}, |
| 58 |
{ |
| 59 |
title: 'Workspaces', |
| 60 |
cellTransforms: [compoundExpand] |
| 61 |
}, |
| 62 |
'Last Commit', |
| 63 |
{ |
| 64 |
title: '', |
| 65 |
dataLabel: 'Action' |
| 66 |
} |
| 67 |
]; |
| 68 |
const rows: TableProps['rows'] = []; |
| 69 |
repositories.forEach((repo) => { |
| 70 |
const expandedCellKey = expandedCells[repo.name]; |
| 71 |
const isRowExpanded = !!expandedCellKey; |
| 72 |
rows.push({ |
| 73 |
isOpen: isRowExpanded, |
| 74 |
cells: [ |
| 75 |
{ title: <a href="#">{repo.name}</a>, props: { component: 'th' } }, |
| 76 |
{ |
| 77 |
title: ( |
| 78 |
<Fragment> |
| 79 |
<RhUiBranchFillIcon key="icon" /> {repo.branches} |
| 80 |
</Fragment> |
| 81 |
), |
| 82 |
props: { |
| 83 |
isOpen: expandedCellKey === 'branches', |
| 84 |
ariaControls: `compound-expansion-table-${repo.name}-1` |
| 85 |
} |
| 86 |
}, |
| 87 |
{ |
| 88 |
title: ( |
| 89 |
<Fragment> |
| 90 |
<RhUiCodeIcon key="icon" /> {repo.prs} |
| 91 |
</Fragment> |
| 92 |
), |
| 93 |
props: { |
| 94 |
isOpen: expandedCellKey === 'prs', |
| 95 |
ariaControls: `compound-expansion-table-${repo.name}-2` |
| 96 |
} |
| 97 |
}, |
| 98 |
{ |
| 99 |
title: ( |
| 100 |
<Fragment> |
| 101 |
<CubeIcon key="icon" /> {repo.branches} |
| 102 |
</Fragment> |
| 103 |
), |
| 104 |
props: { |
| 105 |
isOpen: expandedCellKey === 'workspaces', |
| 106 |
ariaControls: `compound-expansion-table-${repo.name}-3` |
| 107 |
} |
| 108 |
}, |
| 109 |
repo.lastCommit, |
| 110 |
{ title: <a href="#">Open in Github</a> } |
| 111 |
] |
| 112 |
}); |
| 113 |
reposByRowIndex.push(repo); |
| 114 |
const parentIndex = rows.length - 1; |
| 115 |
rows.push({ |
| 116 |
parent: parentIndex, |
| 117 |
compoundParent: 1, |
| 118 |
cells: [ |
| 119 |
{ |
| 120 |
title: ( |
| 121 |
<div className={spacing.mMd} id={`compound-expansion-table-${repo.name}-1`}> |
| 122 |
Expanded content for {repo.name}: Branches goes here! |
| 123 |
</div> |
| 124 |
), |
| 125 |
props: { colSpan: 6, className: 'pf-m-no-padding' } |
| 126 |
} |
| 127 |
] |
| 128 |
}); |
| 129 |
reposByRowIndex.push(null); |
| 130 |
rows.push({ |
| 131 |
parent: parentIndex, |
| 132 |
compoundParent: 2, |
| 133 |
cells: [ |
| 134 |
{ |
| 135 |
title: ( |
| 136 |
<div className={spacing.mMd} id={`compound-expansion-table-${repo.name}-2`}> |
| 137 |
Expanded content for {repo.name}: Pull requests goes here! |
| 138 |
</div> |
| 139 |
), |
| 140 |
props: { colSpan: 6, className: 'pf-m-no-padding' } |
| 141 |
} |
| 142 |
] |
| 143 |
}); |
| 144 |
reposByRowIndex.push(null); |
| 145 |
rows.push({ |
| 146 |
parent: parentIndex, |
| 147 |
compoundParent: 3, |
| 148 |
cells: [ |
| 149 |
{ |
| 150 |
title: ( |
| 151 |
<div className={spacing.mMd} id={`compound-expansion-table-${repo.name}-3`}> |
| 152 |
Expanded content for {repo.name}: Workspaces goes here! |
| 153 |
</div> |
| 154 |
), |
| 155 |
props: { colSpan: 6, className: 'pf-m-no-padding' } |
| 156 |
} |
| 157 |
] |
| 158 |
}); |
| 159 |
reposByRowIndex.push(null); |
| 160 |
}); |
| 161 |
|
| 162 |
return ( |
| 163 |
<Table |
| 164 |
aria-label="Compound expandable table" |
| 165 |
onExpand={(_event, rowIndex, colIndex, isOpen) => { |
| 166 |
if (reposByRowIndex[rowIndex]) { |
| 167 |
setCellExpanded(reposByRowIndex[rowIndex], columnKeys[colIndex], !isOpen); |
| 168 |
} |
| 169 |
}} |
| 170 |
rows={rows} |
| 171 |
cells={columns} |
| 172 |
expandId="compound-expandable-table-toggle" |
| 173 |
contentId="compound-expandable-table-content" |
| 174 |
> |
| 175 |
<TableHeader /> |
| 176 |
<TableBody /> |
| 177 |
</Table> |
| 178 |
); |
| 179 |
}; |