| 1 |
import { useState } from 'react'; |
| 2 |
import { treeRow, IRow, OnTreeRowCollapse, OnCheckChange, OnToggleRowDetails } from '@patternfly/react-table'; |
| 3 |
import { Table, TableHeader, TableBody } from '@patternfly/react-table/deprecated'; |
| 4 |
import LeafIcon from '@patternfly/react-icons/dist/esm/icons/leaf-icon'; |
| 5 |
import FolderIcon from '@patternfly/react-icons/dist/esm/icons/folder-icon'; |
| 6 |
import FolderOpenIcon from '@patternfly/react-icons/dist/esm/icons/folder-open-icon'; |
| 7 |
|
| 8 |
interface RepositoriesTreeNode { |
| 9 |
name: string; |
| 10 |
branches: string; |
| 11 |
pullRequests: string; |
| 12 |
workspaces: string; |
| 13 |
children?: RepositoriesTreeNode[]; |
| 14 |
} |
| 15 |
|
| 16 |
export const LegacyTableTree: React.FunctionComponent = () => { |
| 17 |
// In real usage, this data would come from some external source like an API via props. |
| 18 |
const data: RepositoriesTreeNode[] = [ |
| 19 |
{ |
| 20 |
name: 'Repositories one', |
| 21 |
branches: 'Branch one', |
| 22 |
pullRequests: 'Pull request one', |
| 23 |
workspaces: 'Workplace one', |
| 24 |
children: [ |
| 25 |
{ |
| 26 |
name: 'Repositories two', |
| 27 |
branches: 'Branch two', |
| 28 |
pullRequests: 'Pull request two', |
| 29 |
workspaces: 'Workplace two', |
| 30 |
children: [ |
| 31 |
{ |
| 32 |
name: 'Repositories three', |
| 33 |
branches: 'Branch three', |
| 34 |
pullRequests: 'Pull request three', |
| 35 |
workspaces: 'Workplace three' |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: 'Repositories four', |
| 39 |
branches: 'Branch four', |
| 40 |
pullRequests: 'Pull request four', |
| 41 |
workspaces: 'Workplace four' |
| 42 |
} |
| 43 |
] |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: 'Repositories five', |
| 47 |
branches: 'Branch five', |
| 48 |
pullRequests: 'Pull request five', |
| 49 |
workspaces: 'Workplace five' |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: 'Repositories six', |
| 53 |
branches: 'Branch six', |
| 54 |
pullRequests: 'Pull request six', |
| 55 |
workspaces: 'Workplace six' |
| 56 |
} |
| 57 |
] |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: 'Repositories seven', |
| 61 |
branches: 'Branch seven', |
| 62 |
pullRequests: 'Pull request seven', |
| 63 |
workspaces: 'Workplace seven', |
| 64 |
children: [ |
| 65 |
{ |
| 66 |
name: 'Repositories eight', |
| 67 |
branches: 'Branch eight', |
| 68 |
pullRequests: 'Pull request eight', |
| 69 |
workspaces: 'Workplace eight' |
| 70 |
} |
| 71 |
] |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: 'Repositories nine', |
| 75 |
branches: 'Branch nine', |
| 76 |
pullRequests: 'Pull request nine', |
| 77 |
workspaces: 'Workplace nine' |
| 78 |
} |
| 79 |
]; |
| 80 |
|
| 81 |
const [expandedNodeNames, setExpandedNodeNames] = useState<string[]>(['Repositories one']); |
| 82 |
const [expandedDetailsNodeNames, setExpandedDetailsNodeNames] = useState<string[]>([]); |
| 83 |
const [selectedNodeNames, setSelectedNodeNames] = useState<string[]>([]); |
| 84 |
|
| 85 |
const getDescendants = (node: RepositoriesTreeNode): RepositoriesTreeNode[] => |
| 86 |
[node].concat(...(node.children ? node.children.map(getDescendants) : [])); |
| 87 |
const areAllDescendantsSelected = (node: RepositoriesTreeNode) => |
| 88 |
getDescendants(node).every((n) => selectedNodeNames.includes(n.name)); |
| 89 |
const areSomeDescendantsSelected = (node: RepositoriesTreeNode) => |
| 90 |
getDescendants(node).some((n) => selectedNodeNames.includes(n.name)); |
| 91 |
|
| 92 |
const isNodeChecked = (node: RepositoriesTreeNode) => { |
| 93 |
if (areAllDescendantsSelected(node)) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
if (areSomeDescendantsSelected(node)) { |
| 97 |
return null; |
| 98 |
} |
| 99 |
return false; |
| 100 |
}; |
| 101 |
|
| 102 |
// We index the tree nodes in the order of the table rows, for looking up by rowIndex |
| 103 |
const flattenedNodes: RepositoriesTreeNode[] = []; |
| 104 |
|
| 105 |
/** |
| 106 |
Recursive function which flattens the data into an array of flattened IRow objects |
| 107 |
params: |
| 108 |
- nodes - array of a single level of tree nodes |
| 109 |
- level - number representing how deeply nested the current row is |
| 110 |
- posinset - position of the row relative to this row's siblings |
| 111 |
- isHidden - defaults to false, true if this row's parent is expanded |
| 112 |
- currentRowIndex - position of the row relative to the entire table |
| 113 |
*/ |
| 114 |
const buildRows = ( |
| 115 |
[node, ...remainingNodes]: RepositoriesTreeNode[], |
| 116 |
level = 1, |
| 117 |
posinset = 1, |
| 118 |
rowIndex = 0, |
| 119 |
isHidden = false |
| 120 |
): IRow[] => { |
| 121 |
if (!node) { |
| 122 |
return []; |
| 123 |
} |
| 124 |
const isExpanded = expandedNodeNames.includes(node.name); |
| 125 |
const isDetailsExpanded = expandedDetailsNodeNames.includes(node.name); |
| 126 |
const isChecked = isNodeChecked(node); |
| 127 |
let icon = <LeafIcon />; |
| 128 |
if (node.children) { |
| 129 |
icon = isExpanded ? <FolderOpenIcon /> : <FolderIcon />; |
| 130 |
} |
| 131 |
flattenedNodes.push(node); |
| 132 |
|
| 133 |
const childRows = |
| 134 |
node.children && node.children.length |
| 135 |
? buildRows(node.children, level + 1, 1, rowIndex + 1, !isExpanded || isHidden) |
| 136 |
: []; |
| 137 |
|
| 138 |
return [ |
| 139 |
{ |
| 140 |
cells: [node.name, node.branches, node.pullRequests, node.workspaces], |
| 141 |
props: { |
| 142 |
isExpanded, |
| 143 |
isDetailsExpanded, |
| 144 |
isHidden, |
| 145 |
'aria-level': level, |
| 146 |
'aria-posinset': posinset, |
| 147 |
'aria-setsize': node.children ? node.children.length : 0, |
| 148 |
isChecked, |
| 149 |
icon |
| 150 |
} |
| 151 |
}, |
| 152 |
...childRows, |
| 153 |
...buildRows(remainingNodes, level, posinset + 1, rowIndex + 1 + childRows.length, isHidden) |
| 154 |
]; |
| 155 |
}; |
| 156 |
|
| 157 |
const onCollapse: OnTreeRowCollapse = (_event, rowIndex) => { |
| 158 |
const node = flattenedNodes[rowIndex]; |
| 159 |
const isExpanded = expandedNodeNames.includes(node.name); |
| 160 |
setExpandedNodeNames((prevExpanded) => { |
| 161 |
const otherExpandedNodeNames = prevExpanded.filter((name) => name !== node.name); |
| 162 |
return isExpanded ? otherExpandedNodeNames : [...otherExpandedNodeNames, node.name]; |
| 163 |
}); |
| 164 |
}; |
| 165 |
|
| 166 |
const onCheck: OnCheckChange = (_event, isChecking, rowIndex) => { |
| 167 |
const node = flattenedNodes[rowIndex]; |
| 168 |
const nodeNamesToCheck = getDescendants(node).map((n) => n.name); |
| 169 |
setSelectedNodeNames((prevSelected) => { |
| 170 |
const otherSelectedNodeNames = prevSelected.filter((name) => !nodeNamesToCheck.includes(name)); |
| 171 |
return !isChecking ? otherSelectedNodeNames : [...otherSelectedNodeNames, ...nodeNamesToCheck]; |
| 172 |
}); |
| 173 |
}; |
| 174 |
|
| 175 |
const onToggleRowDetails: OnToggleRowDetails = (_event, rowIndex) => { |
| 176 |
const node = flattenedNodes[rowIndex]; |
| 177 |
const isDetailsExpanded = expandedDetailsNodeNames.includes(node.name); |
| 178 |
setExpandedDetailsNodeNames((prevDetailsExpanded) => { |
| 179 |
const otherDetailsExpandedNodeNames = prevDetailsExpanded.filter((name) => name !== node.name); |
| 180 |
return isDetailsExpanded ? otherDetailsExpandedNodeNames : [...otherDetailsExpandedNodeNames, node.name]; |
| 181 |
}); |
| 182 |
}; |
| 183 |
|
| 184 |
return ( |
| 185 |
<Table |
| 186 |
isTreeTable |
| 187 |
aria-label="Tree table" |
| 188 |
cells={[ |
| 189 |
{ |
| 190 |
title: 'Repositories', |
| 191 |
cellTransforms: [treeRow(onCollapse, onCheck, onToggleRowDetails)] |
| 192 |
}, |
| 193 |
'Branches', |
| 194 |
'Pull requests', |
| 195 |
'Workspaces' |
| 196 |
]} |
| 197 |
rows={buildRows(data)} |
| 198 |
> |
| 199 |
<TableHeader /> |
| 200 |
<TableBody /> |
| 201 |
</Table> |
| 202 |
); |
| 203 |
}; |