| 1 |
import { useState } from 'react'; |
| 2 |
import { Table, Thead, Tr, Th, Tbody, Td, TreeRowWrapper, TdProps } from '@patternfly/react-table'; |
| 3 |
import LeafIcon from '@patternfly/react-icons/dist/esm/icons/leaf-icon'; |
| 4 |
|
| 5 |
interface RepositoriesTreeNode { |
| 6 |
name: string; |
| 7 |
branches: string; |
| 8 |
pullRequests: string; |
| 9 |
workspaces: string; |
| 10 |
children?: RepositoriesTreeNode[]; |
| 11 |
} |
| 12 |
|
| 13 |
export const TableTreeNoInset: React.FunctionComponent = () => { |
| 14 |
// In real usage, this data would come from some external source like an API via props. |
| 15 |
const data: RepositoriesTreeNode[] = [ |
| 16 |
{ |
| 17 |
name: 'Repositories one', |
| 18 |
branches: 'Branch one', |
| 19 |
pullRequests: 'Pull request one', |
| 20 |
workspaces: 'Workplace one', |
| 21 |
children: [] |
| 22 |
}, |
| 23 |
{ |
| 24 |
name: 'Repositories seven', |
| 25 |
branches: 'Branch seven', |
| 26 |
pullRequests: 'Pull request seven', |
| 27 |
workspaces: 'Workplace seven', |
| 28 |
children: [] |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: 'Repositories nine', |
| 32 |
branches: 'Branch nine', |
| 33 |
pullRequests: 'Pull request nine', |
| 34 |
workspaces: 'Workplace nine' |
| 35 |
} |
| 36 |
]; |
| 37 |
|
| 38 |
const columnNames = { |
| 39 |
name: 'Repositories', |
| 40 |
branches: 'Branches', |
| 41 |
prs: 'Pull requests', |
| 42 |
workspaces: 'Workspaces' |
| 43 |
}; |
| 44 |
|
| 45 |
const [selectedNodeNames, setSelectedNodeNames] = useState<string[]>([]); |
| 46 |
const isNodeChecked = (node: RepositoriesTreeNode) => selectedNodeNames.includes(node.name); |
| 47 |
|
| 48 |
/** |
| 49 |
Recursive function which flattens the data into an array of flattened TreeRowWrapper components |
| 50 |
params: |
| 51 |
- nodes - array of a single level of tree nodes |
| 52 |
- level - number representing how deeply nested the current row is |
| 53 |
- posinset - position of the row relative to this row's siblings |
| 54 |
- currentRowIndex - position of the row relative to the entire table |
| 55 |
- isHidden - defaults to false, true if this row's parent is expanded |
| 56 |
*/ |
| 57 |
const renderRows = ( |
| 58 |
[node, ...remainingNodes]: RepositoriesTreeNode[], |
| 59 |
level = 1, |
| 60 |
posinset = 1, |
| 61 |
rowIndex = 0, |
| 62 |
isHidden = false |
| 63 |
): React.ReactNode[] => { |
| 64 |
if (!node) { |
| 65 |
return []; |
| 66 |
} |
| 67 |
const isChecked = isNodeChecked(node); |
| 68 |
const icon = <LeafIcon />; |
| 69 |
|
| 70 |
const treeRow: TdProps['treeRow'] = { |
| 71 |
onCollapse: () => {}, |
| 72 |
onToggleRowDetails: () => {}, |
| 73 |
onCheckChange: (_event, isChecking) => { |
| 74 |
setSelectedNodeNames((prevSelected) => { |
| 75 |
const otherSelectedNodeNames = prevSelected.filter((name) => name === node.name); |
| 76 |
return !isChecking ? otherSelectedNodeNames : [...otherSelectedNodeNames, node.name]; |
| 77 |
}); |
| 78 |
}, |
| 79 |
rowIndex, |
| 80 |
props: { |
| 81 |
isHidden, |
| 82 |
'aria-level': level, |
| 83 |
'aria-posinset': posinset, |
| 84 |
'aria-setsize': node.children ? node.children.length : 0, |
| 85 |
isChecked, |
| 86 |
checkboxId: `flat_checkbox_id_${node.name.toLowerCase().replace(/\s+/g, '_')}`, |
| 87 |
icon |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
const childRows = |
| 92 |
node.children && node.children.length ? renderRows(node.children, level + 1, 1, rowIndex + 1, isHidden) : []; |
| 93 |
|
| 94 |
return [ |
| 95 |
<TreeRowWrapper key={node.name} row={{ props: treeRow.props }}> |
| 96 |
<Td dataLabel={columnNames.name} treeRow={treeRow}> |
| 97 |
{node.name} |
| 98 |
</Td> |
| 99 |
<Td dataLabel={columnNames.branches}>{node.branches}</Td> |
| 100 |
<Td dataLabel={columnNames.prs}>{node.pullRequests}</Td> |
| 101 |
<Td dataLabel={columnNames.workspaces}>{node.workspaces}</Td> |
| 102 |
</TreeRowWrapper>, |
| 103 |
...childRows, |
| 104 |
...renderRows(remainingNodes, level, posinset + 1, rowIndex + 1 + childRows.length, isHidden) |
| 105 |
]; |
| 106 |
}; |
| 107 |
|
| 108 |
return ( |
| 109 |
<Table isTreeTable aria-label="Tree table" hasNoInset> |
| 110 |
<Thead> |
| 111 |
<Tr> |
| 112 |
<Th width={40}>{columnNames.name}</Th> |
| 113 |
<Th>{columnNames.branches}</Th> |
| 114 |
<Th>{columnNames.prs}</Th> |
| 115 |
<Th>{columnNames.workspaces}</Th> |
| 116 |
</Tr> |
| 117 |
</Thead> |
| 118 |
<Tbody>{renderRows(data)}</Tbody> |
| 119 |
</Table> |
| 120 |
); |
| 121 |
}; |