Debian Codegraph @

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

raw
1 /* eslint-disable no-console */
2 import { useState } from 'react';
3 import { Table, Thead, Tr, Th, Tbody, Td, ExpandableRowContent, ActionsColumn, IAction } from '@patternfly/react-table';
4
5 interface Repository {
6 name: string;
7 branches: string;
8 prs: string;
9 nestedComponent?: React.ReactNode;
10 link?: React.ReactNode;
11 noPadding?: boolean;
12 }
13 interface NestedRepository {
14 name: string;
15 branches: string | null;
16 prs: string | null;
17 workspaces: string | null;
18 lastCommit: string | null;
19 }
20
21 const NestedReposTable: React.FunctionComponent = () => {
22 // In real usage, this data would come from some external source like an API via props.
23 const prs: NestedRepository[] = [
24 { name: 'Repository 1', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' },
25 { name: 'Repository 2', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' },
26 { name: 'Repository 3', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' },
27 { name: 'Repository 4', branches: '25', prs: '25', workspaces: '5', lastCommit: '2 days ago' }
28 ];
29
30 const columnNames = {
31 name: 'Repositories',
32 branches: 'Branches',
33 prs: 'Pull requests',
34 workspaces: 'Workspaces',
35 lastCommit: 'Last commit'
36 };
37
38 return (
39 <Table aria-label="Simple table" variant="compact">
40 <Thead>
41 <Tr>
42 <Th>{columnNames.name}</Th>
43 <Th>{columnNames.branches}</Th>
44 <Th>{columnNames.prs}</Th>
45 <Th>{columnNames.workspaces}</Th>
46 <Th>{columnNames.lastCommit}</Th>
47 </Tr>
48 </Thead>
49 <Tbody>
50 {prs.map((repo) => (
51 <Tr key={repo.name}>
52 <Td dataLabel={columnNames.name}>{repo.name}</Td>
53 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
54 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
55 <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
56 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
57 </Tr>
58 ))}
59 </Tbody>
60 </Table>
61 );
62 };
63
64 export const TableExpandable: React.FunctionComponent = () => {
65 // In real usage, this data would come from some external source like an API via props.
66 const repositories: Repository[] = [
67 { name: 'Node 1', branches: '10', prs: '2', nestedComponent: <NestedReposTable />, link: <a>Link 1</a> },
68 { name: 'Node 2', branches: '3', prs: '4', link: <a>Link 2</a> },
69 {
70 name: 'Node 3',
71 branches: '11',
72 prs: '7',
73 nestedComponent: (
74 <p>
75 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
76 magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
77 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
78 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
79 est laborum.
80 </p>
81 ),
82 link: <a>Link 3</a>
83 },
84 {
85 name: 'Node 4',
86 branches: '11',
87 prs: '7',
88 nestedComponent: 'Expandable row content has no padding.',
89 link: <a>Link 4</a>,
90 noPadding: true
91 }
92 ];
93
94 const columnNames = {
95 name: 'Repositories',
96 branches: 'Branches',
97 prs: 'Pull requests',
98 link: 'Link',
99 action: 'Action'
100 };
101 // In this example, expanded rows are tracked by the repo names from each row. This could be any unique identifier.
102 // This is to prevent state from being based on row order index in case we later add sorting.
103 // Note that this behavior is very similar to selection state.
104 const initialExpandedRepoNames = repositories.filter((repo) => !!repo.nestedComponent).map((repo) => repo.name); // Default to all expanded
105 const [expandedRepoNames, setExpandedRepoNames] = useState<string[]>(initialExpandedRepoNames);
106 const setRepoExpanded = (repo: Repository, isExpanding = true) =>
107 setExpandedRepoNames((prevExpanded) => {
108 const otherExpandedRepoNames = prevExpanded.filter((r) => r !== repo.name);
109 return isExpanding ? [...otherExpandedRepoNames, repo.name] : otherExpandedRepoNames;
110 });
111 const isRepoExpanded = (repo: Repository) => expandedRepoNames.includes(repo.name);
112
113 const defaultActions = (repo: Repository): IAction[] => [
114 {
115 title: 'Some action',
116 onClick: () => console.log(`clicked on Some action, on row ${repo.name}`)
117 },
118 {
119 title: <a href="https://www.patternfly.org">Link action</a>
120 },
121 {
122 isSeparator: true
123 },
124 {
125 title: 'Third action',
126 onClick: () => console.log(`clicked on Third action, on row ${repo.name}`)
127 }
128 ];
129
130 return (
131 <Table isExpandable hasAnimations aria-label="Simple table">
132 <Thead>
133 <Tr>
134 <Th screenReaderText="Row expansion" />
135 <Th width={20}>{columnNames.name}</Th>
136 <Th>{columnNames.branches}</Th>
137 <Th>{columnNames.prs}</Th>
138 <Th>{columnNames.link}</Th>
139 <Th>{columnNames.action}</Th>
140 </Tr>
141 </Thead>
142 {repositories.map((repo, rowIndex) => (
143 <Tbody key={repo.name} isExpanded={isRepoExpanded(repo)}>
144 <Tr isContentExpanded={isRepoExpanded(repo)}>
145 <Td
146 expand={
147 repo.nestedComponent
148 ? {
149 rowIndex,
150 isExpanded: isRepoExpanded(repo),
151 onToggle: () => setRepoExpanded(repo, !isRepoExpanded(repo)),
152 expandId: 'composable-nested-table-expandable-example'
153 }
154 : undefined
155 }
156 />
157 <Td dataLabel={columnNames.name}>{repo.name}</Td>
158 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
159 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
160 <Td dataLabel={columnNames.link}>{repo.link}</Td>
161 <Td dataLabel={columnNames.action}>
162 <ActionsColumn items={defaultActions(repo)} />
163 </Td>
164 </Tr>
165 {repo.nestedComponent ? (
166 <Tr isExpanded={isRepoExpanded(repo)}>
167 <Td
168 noPadding={repo.noPadding}
169 dataLabel={`${columnNames.name} expended`}
170 colSpan={Object.keys(columnNames).length + 1}
171 >
172 <ExpandableRowContent>{repo.nestedComponent}</ExpandableRowContent>
173 </Td>
174 </Tr>
175 ) : null}
176 </Tbody>
177 ))}
178 </Table>
179 );
180 };