@patternfly/react-table 4.103.0 → 4.103.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,219 @@
1
+ import React from 'react';
2
+ import { Card, Label, PageSection, Table, TableHeader, TableBody, expandable } from '@patternfly/react-table';
3
+ import DashboardWrapper from '@patternfly/react-core/src/demos/examples/DashboardWrapper';
4
+ import { TextVariants } from '@patternfly/react-core';
5
+
6
+ const columns = [
7
+ {
8
+ title: 'Servers',
9
+ cellFormatters: [expandable]
10
+ },
11
+ 'Threads',
12
+ 'Applications',
13
+ 'Workspaces',
14
+ 'Status'
15
+ ];
16
+
17
+ const serverData = [
18
+ {
19
+ name: 'US-Node 1',
20
+ threads: 18,
21
+ applications: 42,
22
+ workspaces: 7,
23
+ status: { title: <Label color="orange">Stopped</Label> },
24
+ details: (
25
+ <TextContent>
26
+ <Text component={TextVariants.p}>
27
+ Location<Text component={TextVariants.small}>Boston</Text>
28
+ </Text>
29
+ <Text component={TextVariants.p}>
30
+ Last Modified<Text component={TextVariants.small}>2 hours ago</Text>
31
+ </Text>
32
+ <Text component={TextVariants.p}>
33
+ URL<Text component={TextVariants.small}>http://www.redhat.com/en/office-locations/US-node1</Text>
34
+ </Text>
35
+ </TextContent>
36
+ )
37
+ },
38
+ {
39
+ name: 'US-Node 2',
40
+ threads: 9,
41
+ applications: 24,
42
+ workspaces: 17,
43
+ status: { title: <Label color="red">Down</Label> },
44
+ details: (
45
+ <TextContent>
46
+ <Text component={TextVariants.p}>
47
+ Location<Text component={TextVariants.small}>Atlanta</Text>
48
+ </Text>
49
+ <Text component={TextVariants.p}>
50
+ Last Modified<Text component={TextVariants.small}>5 hours ago</Text>
51
+ </Text>
52
+ <Text component={TextVariants.p}>
53
+ URL<Text component={TextVariants.small}>http://www.redhat.com/en/office-locations/US-node2</Text>
54
+ </Text>
55
+ </TextContent>
56
+ )
57
+ },
58
+ {
59
+ name: 'US-Node 3',
60
+ threads: 8,
61
+ applications: 47,
62
+ workspaces: 3,
63
+ status: { title: <Label color="green">Running</Label> },
64
+ details: (
65
+ <TextContent>
66
+ <Text component={TextVariants.p}>
67
+ Location<Text component={TextVariants.small}>San Francisco</Text>
68
+ </Text>
69
+ <Text component={TextVariants.p}>
70
+ Last Modified<Text component={TextVariants.small}>20 minutes ago</Text>
71
+ </Text>
72
+ <Text component={TextVariants.p}>
73
+ URL<Text component={TextVariants.small}>http://www.redhat.com/en/office-locations/US-node3</Text>
74
+ </Text>
75
+ </TextContent>
76
+ )
77
+ },
78
+ {
79
+ name: 'US-Node 4',
80
+ threads: 6,
81
+ applications: 4,
82
+ workspaces: 15,
83
+ status: { title: <Label color="blue">Needs Maintenance</Label> },
84
+ details: (
85
+ <TextContent>
86
+ <Text component={TextVariants.p}>
87
+ Location<Text component={TextVariants.small}>Raleigh</Text>
88
+ </Text>
89
+ <Text component={TextVariants.p}>
90
+ Last Modified<Text component={TextVariants.small}>10 minutes ago</Text>
91
+ </Text>
92
+ <Text component={TextVariants.p}>
93
+ URL<Text component={TextVariants.small}>http://www.redhat.com/en/office-locations/US-node4</Text>
94
+ </Text>
95
+ </TextContent>
96
+ )
97
+ },
98
+ {
99
+ name: 'US-Node 5',
100
+ threads: 9,
101
+ applications: 24,
102
+ workspaces: 17,
103
+ status: { title: <Label color="orange">Stopped</Label> },
104
+ details: (
105
+ <TextContent>
106
+ <Text component={TextVariants.p}>
107
+ Location<Text component={TextVariants.small}>Atlanta</Text>
108
+ </Text>
109
+ <Text component={TextVariants.p}>
110
+ Last Modified<Text component={TextVariants.small}>15 minutes ago</Text>
111
+ </Text>
112
+ <Text component={TextVariants.p}>
113
+ URL<Text component={TextVariants.small}>http://www.redhat.com/en/office-locations/US-node5</Text>
114
+ </Text>
115
+ </TextContent>
116
+ )
117
+ }
118
+ ];
119
+
120
+ const initialExpandedServerNames = ['US-Node 2']; // Default to expanded
121
+
122
+ class ExpandCollapseAllTableDemo extends React.Component {
123
+ constructor(props) {
124
+ super(props);
125
+ this.state = {
126
+ isChecked: false,
127
+ selectedRows: 0,
128
+ expandedRows: 0,
129
+ expandCollapseToggle: 'expand',
130
+ collapseAllAriaLabel: 'Expand all',
131
+ expandedServerNames: initialExpandedServerNames
132
+ };
133
+ }
134
+
135
+ setServerExpanded = (server, isExpanding) => {
136
+ this.setState(() => {
137
+ const otherExpandedServerNames = this.state.expandedServerNames.filter(r => r !== server.name);
138
+ return {
139
+ expandedServerNames: isExpanding ? [...otherExpandedServerNames, server.name] : otherExpandedServerNames
140
+ };
141
+ });
142
+ };
143
+
144
+ render() {
145
+ const { collapseAllAriaLabel } = this.state;
146
+
147
+ const isServerExpanded = server => this.state.expandedServerNames.includes(server.name);
148
+ // We want to be able to reference the original data object based on row index. But because an expanded
149
+ // row takes up two row indexes, servers[rowIndex] will not be accurate like it would in a normal table.
150
+ // One solution to this is to create an array of data objects indexed by the displayed row index.
151
+ const serversByRowIndex = [];
152
+
153
+ const rows = [];
154
+ serverData.forEach(server => {
155
+ rows.push({
156
+ ...{ isOpen: isServerExpanded(server) },
157
+ cells: [server.name, server.threads, server.applications, server.workspaces, server.status]
158
+ });
159
+ serversByRowIndex.push(server);
160
+ if (server.details) {
161
+ const cells = [];
162
+
163
+ cells.push({
164
+ title: server.details
165
+ });
166
+
167
+ rows.push({
168
+ parent: rows.length - 1,
169
+ cells
170
+ });
171
+ serversByRowIndex.push(null);
172
+ }
173
+ });
174
+
175
+ const onCollapse = (_e, rowIndex, isOpen) => {
176
+ const collapseAll = rowIndex === undefined;
177
+
178
+ if (collapseAll) {
179
+ this.setState(() => {
180
+ return {
181
+ expandedServerNames: isOpen ? [...serverData.map((server) => server.name)] : []
182
+ };
183
+ });
184
+
185
+ }
186
+ else if (serversByRowIndex[rowIndex]) {
187
+ this.setServerExpanded(serversByRowIndex[rowIndex], isOpen);
188
+ }
189
+ }
190
+
191
+ return (
192
+ <React.Fragment>
193
+ <DashboardWrapper hasPageTemplateTitle>
194
+ <PageSection
195
+ padding={{
196
+ default: 'noPadding',
197
+ xl: 'padding'
198
+ }}
199
+ >
200
+ <Card component="div">
201
+ <Table
202
+ aria-label="Collapsible table"
203
+ onCollapse={onCollapse}
204
+ rows={rows}
205
+ cells={columns}
206
+ canCollapseAll={true}
207
+ collapseAllAriaLabel={collapseAllAriaLabel}
208
+ expandId="expand-collapse-all-demo"
209
+ >
210
+ <TableHeader />
211
+ <TableBody />
212
+ </Table>
213
+ </Card>
214
+ </PageSection>
215
+ </DashboardWrapper>
216
+ </React.Fragment>
217
+ );
218
+ }
219
+ }