Debian Codegraph @

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