forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableExpandCollapseAll.tsx
More file actions
212 lines (200 loc) · 6.54 KB
/
TableExpandCollapseAll.tsx
File metadata and controls
212 lines (200 loc) · 6.54 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Fragment, ReactNode, useEffect, useState } from 'react';
import { Card, Content, Label, PageSection } from '@patternfly/react-core';
import { Table, Thead, Tbody, Tr, Th, Td, ExpandableRowContent } from '@patternfly/react-table';
import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper';
const expandableColumns = ['Servers', 'Threads', 'Applications', 'Workspaces', 'Status'];
interface Server {
name: string;
threads: number;
applications: number;
workspaces: number;
status: { title: ReactNode };
details: ReactNode;
}
const serverData: Server[] = [
{
name: 'US-Node 1',
threads: 18,
applications: 42,
workspaces: 7,
status: { title: <Label color="green">Running</Label> },
details: (
<Content>
<p>
Location<small>Boston</small>
</p>
<p>
Last Modified<small>2 hours ago</small>
</p>
<p>
URL<small>http://www.redhat.com/en/office-locations/US-node1</small>
</p>
</Content>
)
},
{
name: 'US-Node 2',
threads: 9,
applications: 24,
workspaces: 17,
status: { title: <Label color="red">Down</Label> },
details: (
<Content>
<p>
Location<small>Atlanta</small>
</p>
<p>
Last Modified<small>5 hours ago</small>
</p>
<p>
URL<small>http://www.redhat.com/en/office-locations/US-node2</small>
</p>
</Content>
)
},
{
name: 'US-Node 3',
threads: 8,
applications: 47,
workspaces: 3,
status: { title: <Label color="green">Running</Label> },
details: (
<Content>
<p>
Location<small>San Francisco</small>
</p>
<p>
Last Modified<small>20 minutes ago</small>
</p>
<p>
URL<small>http://www.redhat.com/en/office-locations/US-node3</small>
</p>
</Content>
)
},
{
name: 'US-Node 4',
threads: 6,
applications: 4,
workspaces: 15,
status: { title: <Label color="blue">Needs Maintenance</Label> },
details: (
<Content>
<p>
Location<small>Raleigh</small>
</p>
<p>
Last Modified<small>10 minutes ago</small>
</p>
<p>
URL<small>http://www.redhat.com/en/office-locations/US-node4</small>
</p>
</Content>
)
},
{
name: 'US-Node 5',
threads: 9,
applications: 24,
workspaces: 17,
status: { title: <Label color="orange">Stopped</Label> },
details: (
<Content>
<p>
Location<small>Atlanta</small>
</p>
<p>
Last Modified<small>15 minutes ago</small>
</p>
<p>
URL<small>http://www.redhat.com/en/office-locations/US-node5</small>
</p>
</Content>
)
}
];
const initialExpandedServerNames = ['US-Node 2']; // Default to expanded
export const TableExpandCollapseAll: React.FunctionComponent = () => {
const [areAllExpanded, setAreAllExpanded] = useState(false);
const [collapseAllAriaLabel, setCollapseAllAriaLabel] = useState('Expand all');
const [expandedServerNames, setExpandedServerNames] = useState(initialExpandedServerNames);
useEffect(() => {
const allExpanded = expandedServerNames.length === serverData.length;
setAreAllExpanded(allExpanded);
setCollapseAllAriaLabel(allExpanded ? 'Collapse all' : 'Expand all');
}, [expandedServerNames]);
const setServerExpanded = (server: Server, isExpanding: boolean) => {
const otherExpandedServerNames = expandedServerNames.filter((r) => r !== server.name);
setExpandedServerNames(isExpanding ? [...otherExpandedServerNames, server.name] : otherExpandedServerNames);
};
const isServerExpanded = (server: Server) => expandedServerNames.includes(server.name);
// We want to be able to reference the original data object based on row index. But because an expanded
// row takes up two row indexes, servers[rowIndex] will not be accurate like it would in a normal table.
// One solution to this is to create an array of data objects indexed by the displayed row index.
const onCollapseAll = (_event: any, _rowIndex: number, isOpen: boolean) => {
setExpandedServerNames(isOpen ? [...serverData.map((server) => server.name)] : []);
};
return (
<Fragment>
<DashboardWrapper hasPageTemplateTitle>
<PageSection
padding={{
default: 'noPadding',
xl: 'padding'
}}
aria-label="Collapsible table data"
>
<Card component="div">
<Table isExpandable aria-label="Collapsible table">
<Thead>
<Tr>
<Th
expand={{
areAllExpanded: !areAllExpanded,
collapseAllAriaLabel,
onToggle: onCollapseAll
}}
aria-label="Row expansion"
/>
{expandableColumns.map((column) => (
<Th key={column}>{column}</Th>
))}
</Tr>
</Thead>
{serverData.map((server, serverIndex) => (
<Tbody key={server.name} isExpanded={isServerExpanded(server)}>
<Tr isExpanded={isServerExpanded(server)}>
<Td
expand={
server.details
? {
rowIndex: serverIndex,
isExpanded: isServerExpanded(server),
onToggle: () => setServerExpanded(server, !isServerExpanded(server))
}
: undefined
}
>
<ExpandableRowContent>{server.details}</ExpandableRowContent>
</Td>
<Td>{server?.name}</Td>
<Td>{server?.threads}</Td>
<Td>{server?.applications}</Td>
<Td>{server?.workspaces}</Td>
<Td>{server?.status?.title}</Td>
</Tr>
<Tr isExpandable isExpanded={isServerExpanded(server)}>
<Td></Td>
<Td colSpan={expandableColumns.length}>
<ExpandableRowContent>{server?.details}</ExpandableRowContent>
</Td>
</Tr>
</Tbody>
))}
</Table>
</Card>
</PageSection>
</DashboardWrapper>
</Fragment>
);
};