Debian Codegraph @

node/@patternfly/react-table/src/components/Table/examples/TableTree.tsx

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