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