Debian Codegraph @

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

raw
1 import { Fragment, useState } from 'react';
2 import { Table, Thead, Tr, Th, Tbody, Td, ExpandableRowContent } from '@patternfly/react-table';
3 import { Checkbox } from '@patternfly/react-core';
4
5 interface Repository {
6 name: string;
7 branches: string;
8 prs: string;
9 workspaces: string;
10 lastCommit: string;
11 details?: {
12 detail1?: string;
13 detail2?: string;
14 detail3?: string;
15 detailFormat: 0 | 1 | 2 | 3;
16 };
17 }
18
19 export const TableStripedExpandable: React.FunctionComponent = () => {
20 // In real usage, this data would come from some external source like an API via props.
21 const repositories: Repository[] = [
22 { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
23 {
24 name: 'parent 1',
25 branches: 'two',
26 prs: 'k',
27 workspaces: 'four',
28 lastCommit: 'five',
29 // This `details` structure is just for this example. You can drive expanded content from any kind of data.
30 details: { detailFormat: 0, detail1: 'single cell' }
31 },
32 {
33 name: 'parent 2',
34 branches: 'two',
35 prs: 'b',
36 workspaces: 'four',
37 lastCommit: 'five',
38 details: { detailFormat: 1, detail1: 'single cell - fullWidth' }
39 },
40 {
41 name: 'parent 3',
42 branches: '2',
43 prs: 'b',
44 workspaces: 'four',
45 lastCommit: 'five',
46 details: { detailFormat: 2, detail1: 'single cell - noPadding' }
47 },
48 {
49 name: 'parent 4',
50 branches: '2',
51 prs: 'b',
52 workspaces: 'four',
53 lastCommit: 'five',
54 details: { detailFormat: 3, detail1: 'single cell - fullWidth & noPadding' }
55 },
56 {
57 name: 'parent 5',
58 branches: '2',
59 prs: 'b',
60 workspaces: 'four',
61 lastCommit: 'five',
62 details: {
63 detailFormat: 0,
64 detail1: "spans 'Repositories and 'Branches'",
65 detail2: "spans 'Pull requests' and 'Workspaces', and 'Last commit'"
66 }
67 },
68 {
69 name: 'parent 6',
70 branches: '2',
71 prs: 'b',
72 workspaces: 'four',
73 lastCommit: 'five',
74 details: {
75 detailFormat: 1,
76 detail1: "fullWidth, spans the collapsible column and 'Repositories'",
77 detail2: "fullWidth, spans 'Branches' and 'Pull requests'",
78 detail3: "fullWidth, spans 'Workspaces' and 'Last commit'"
79 }
80 }
81 ];
82
83 const columnNames = {
84 name: 'Repositories',
85 branches: 'Branches',
86 prs: 'Pull requests',
87 workspaces: 'Workspaces',
88 lastCommit: 'Last commit'
89 };
90
91 // In this example, expanded rows are tracked by the repo names from each row. This could be any unique identifier.
92 // This is to prevent state from being based on row order index in case we later add sorting.
93 // Note that this behavior is very similar to selection state.
94 const initialExpandedRepoNames = repositories.filter((repo) => !!repo.details).map((repo) => repo.name); // Default to all expanded
95 const [expandedRepoNames, setExpandedRepoNames] = useState<string[]>(initialExpandedRepoNames);
96 const setRepoExpanded = (repo: Repository, isExpanding = true) =>
97 setExpandedRepoNames((prevExpanded) => {
98 const otherExpandedRepoNames = prevExpanded.filter((r) => r !== repo.name);
99 return isExpanding ? [...otherExpandedRepoNames, repo.name] : otherExpandedRepoNames;
100 });
101 const isRepoExpanded = (repo: Repository) => expandedRepoNames.includes(repo.name);
102
103 const [isExampleCompact, setIsExampleCompact] = useState(true);
104
105 return (
106 <Fragment>
107 <Checkbox
108 label="Compact"
109 isChecked={isExampleCompact}
110 onChange={(_event, checked) => setIsExampleCompact(checked)}
111 aria-label="toggle striped compact variation"
112 id="toggle-compact-striped"
113 name="toggle-compact-striped"
114 />
115 <Table
116 aria-label="Expandable table"
117 variant={isExampleCompact ? 'compact' : undefined}
118 isStriped
119 isExpandable
120 hasAnimations
121 >
122 <Thead>
123 <Tr>
124 <Th screenReaderText="Row expansion" />
125 <Th width={25}>{columnNames.name}</Th>
126 <Th width={10}>{columnNames.branches}</Th>
127 <Th width={15}>{columnNames.prs}</Th>
128 <Th width={30}>{columnNames.workspaces}</Th>
129 <Th width={10}>{columnNames.lastCommit}</Th>
130 </Tr>
131 </Thead>
132 {repositories.map((repo, rowIndex) => {
133 // Some arbitrary examples of how you could customize the child row based on your needs
134 let childIsFullWidth = false;
135 let childHasNoPadding = false;
136 let detail1Colspan = 1;
137 let detail2Colspan = 1;
138 let detail3Colspan = 1;
139 if (repo.details) {
140 const { detail1, detail2, detail3, detailFormat } = repo.details;
141 const numColumns = 5;
142 childIsFullWidth = [1, 3].includes(detailFormat);
143 childHasNoPadding = [2, 3].includes(detailFormat);
144 if (detail1 && !detail2 && !detail3) {
145 detail1Colspan = childIsFullWidth ? numColumns : numColumns + 1; // Account for toggle column
146 } else if (detail1 && detail2 && !detail3) {
147 detail1Colspan = 2;
148 detail2Colspan = childIsFullWidth ? 3 : 4;
149 } else if (detail1 && detail2 && detail3) {
150 detail1Colspan = 2;
151 detail2Colspan = 2;
152 detail3Colspan = childIsFullWidth ? 1 : 2;
153 }
154 }
155 return (
156 <Tbody key={repo.name} isExpanded={isRepoExpanded(repo)}>
157 <Tr isContentExpanded={isRepoExpanded(repo)}>
158 <Td
159 expand={
160 repo.details
161 ? {
162 rowIndex,
163 isExpanded: isRepoExpanded(repo),
164 onToggle: () => setRepoExpanded(repo, !isRepoExpanded(repo))
165 }
166 : undefined
167 }
168 />
169 <Td dataLabel={columnNames.name}>{repo.name}</Td>
170 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
171 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
172 <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
173 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
174 </Tr>
175 {repo.details ? (
176 <Tr isExpanded={isRepoExpanded(repo)}>
177 {!childIsFullWidth ? <Td /> : null}
178 {repo.details.detail1 ? (
179 <Td dataLabel="Repo detail 1" noPadding={childHasNoPadding} colSpan={detail1Colspan}>
180 <ExpandableRowContent>{repo.details.detail1}</ExpandableRowContent>
181 </Td>
182 ) : null}
183 {repo.details.detail2 ? (
184 <Td dataLabel="Repo detail 2" noPadding={childHasNoPadding} colSpan={detail2Colspan}>
185 <ExpandableRowContent>{repo.details.detail2}</ExpandableRowContent>
186 </Td>
187 ) : null}
188 {repo.details.detail3 ? (
189 <Td dataLabel="Repo detail 3" noPadding={childHasNoPadding} colSpan={detail3Colspan}>
190 <ExpandableRowContent>{repo.details.detail3}</ExpandableRowContent>
191 </Td>
192 ) : null}
193 </Tr>
194 ) : null}
195 </Tbody>
196 );
197 })}
198 </Table>
199 </Fragment>
200 );
201 };