@patternfly/react-table 5.0.0-alpha.117 → 5.0.0-alpha.119

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-table",
3
- "version": "5.0.0-alpha.117",
3
+ "version": "5.0.0-alpha.119",
4
4
  "description": "This library provides a set of React table components for use with the PatternFly 4",
5
5
  "main": "dist/js/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -40,7 +40,7 @@
40
40
  "subpaths": "node scripts/createSubpaths.js"
41
41
  },
42
42
  "dependencies": {
43
- "@patternfly/react-core": "^5.0.0-alpha.115",
43
+ "@patternfly/react-core": "^5.0.0-alpha.116",
44
44
  "@patternfly/react-icons": "^5.0.0-alpha.19",
45
45
  "@patternfly/react-styles": "^5.0.0-alpha.16",
46
46
  "@patternfly/react-tokens": "^5.0.0-alpha.14",
@@ -55,5 +55,5 @@
55
55
  "react": "^17 || ^18",
56
56
  "react-dom": "^17 || ^18"
57
57
  },
58
- "gitHead": "ff94267709508525b7fe8dd1da0b7ea6a33854aa"
58
+ "gitHead": "26ffe379333044d711d36be0066f889caca1ed89"
59
59
  }
@@ -1,20 +1,9 @@
1
1
  import React from 'react';
2
- import { Card, Label, PageSection } from '@patternfly/react-core';
3
- import { expandable } from '@patternfly/react-table';
4
- import { Table, TableHeader, TableBody } from '@patternfly/react-table/deprecated';
2
+ import { Card, Label, PageSection, TextVariants, Text, TextContent } from '@patternfly/react-core';
3
+ import { Table, Thead, Tbody, Tr, Th, Td, ExpandableRowContent } from '@patternfly/react-table';
5
4
  import DashboardWrapper from '@patternfly/react-core/src/demos/examples/DashboardWrapper';
6
- import { TextVariants } from '@patternfly/react-core';
7
5
 
8
- const expandableColumns = [
9
- {
10
- title: 'Servers',
11
- cellFormatters: [expandable]
12
- },
13
- 'Threads',
14
- 'Applications',
15
- 'Workspaces',
16
- 'Status'
17
- ];
6
+ const expandableColumns = ['Servers', 'Threads', 'Applications', 'Workspaces', 'Status'];
18
7
 
19
8
  const serverData = [
20
9
  {
@@ -22,7 +11,7 @@ const serverData = [
22
11
  threads: 18,
23
12
  applications: 42,
24
13
  workspaces: 7,
25
- status: { title: <Label color="orange">Stopped</Label> },
14
+ status: { title: <Label color="green">Running</Label> },
26
15
  details: (
27
16
  <TextContent>
28
17
  <Text component={TextVariants.p}>
@@ -121,101 +110,94 @@ const serverData = [
121
110
 
122
111
  const initialExpandedServerNames = ['US-Node 2']; // Default to expanded
123
112
 
124
- class ExpandCollapseAllTableDemo extends React.Component {
125
- constructor(props) {
126
- super(props);
127
- this.state = {
128
- isChecked: false,
129
- selectedRows: 0,
130
- expandedRows: 0,
131
- expandCollapseToggle: 'expand',
132
- collapseAllAriaLabel: 'Expand all',
133
- expandedServerNames: initialExpandedServerNames
134
- };
135
- }
136
-
137
- setServerExpanded = (server, isExpanding) => {
138
- this.setState(() => {
139
- const otherExpandedServerNames = this.state.expandedServerNames.filter(r => r !== server.name);
140
- return {
141
- expandedServerNames: isExpanding ? [...otherExpandedServerNames, server.name] : otherExpandedServerNames
142
- };
143
- });
144
- };
145
-
146
- render() {
147
- const { collapseAllAriaLabel } = this.state;
113
+ const ExpandCollapseAllTableDemo = () => {
114
+ const [areAllExpanded, setAreAllExpanded] = React.useState(false);
115
+ const [collapseAllAriaLabel, setCollapseAllAriaLabel] = React.useState('Expand all');
116
+ const [expandedServerNames, setExpandedServerNames] = React.useState(initialExpandedServerNames);
148
117
 
149
- const isServerExpanded = server => this.state.expandedServerNames.includes(server.name);
150
- // We want to be able to reference the original data object based on row index. But because an expanded
151
- // row takes up two row indexes, servers[rowIndex] will not be accurate like it would in a normal table.
152
- // One solution to this is to create an array of data objects indexed by the displayed row index.
153
- const serversByRowIndex = [];
118
+ React.useEffect(() => {
119
+ const allExpanded = expandedServerNames.length === serverData.length;
120
+ setAreAllExpanded(allExpanded);
121
+ setCollapseAllAriaLabel(allExpanded ? 'Collapse all' : 'Expand all');
122
+ }, [expandedServerNames]);
154
123
 
155
- const rows = [];
156
- serverData.forEach(server => {
157
- rows.push({
158
- ...{ isOpen: isServerExpanded(server) },
159
- cells: [server.name, server.threads, server.applications, server.workspaces, server.status]
160
- });
161
- serversByRowIndex.push(server);
162
- if (server.details) {
163
- const cells = [];
164
-
165
- cells.push({
166
- title: server.details
167
- });
124
+ const setServerExpanded = (server, isExpanding) => {
125
+ const otherExpandedServerNames = expandedServerNames.filter((r) => r !== server.name);
126
+ setExpandedServerNames(isExpanding ? [...otherExpandedServerNames, server.name] : otherExpandedServerNames);
127
+ };
168
128
 
169
- rows.push({
170
- parent: rows.length - 1,
171
- cells
172
- });
173
- serversByRowIndex.push(null);
174
- }
175
- });
129
+ const isServerExpanded = (server) => {
130
+ return expandedServerNames.includes(server.name);
131
+ };
176
132
 
177
- const onCollapse = (_e, rowIndex, isOpen) => {
178
- const collapseAll = rowIndex === undefined;
133
+ // We want to be able to reference the original data object based on row index. But because an expanded
134
+ // row takes up two row indexes, servers[rowIndex] will not be accurate like it would in a normal table.
135
+ // One solution to this is to create an array of data objects indexed by the displayed row index.
179
136
 
180
- if (collapseAll) {
181
- this.setState(() => {
182
- return {
183
- expandedServerNames: isOpen ? [...serverData.map((server) => server.name)] : []
184
- };
185
- });
137
+ const onCollapseAll = (_event, _rowIndex, isOpen) => {
138
+ setExpandedServerNames(isOpen ? [...serverData.map((server) => server.name)] : []);
139
+ };
186
140
 
187
- }
188
- else if (serversByRowIndex[rowIndex]) {
189
- this.setServerExpanded(serversByRowIndex[rowIndex], isOpen);
190
- }
191
- }
141
+ return (
142
+ <React.Fragment>
143
+ <DashboardWrapper hasPageTemplateTitle>
144
+ <PageSection
145
+ padding={{
146
+ default: 'noPadding',
147
+ xl: 'padding'
148
+ }}
149
+ >
150
+ <Card component="div">
151
+ <Table aria-label="Collapsible table">
152
+ <Thead>
153
+ <Tr>
154
+ <Th
155
+ expand={{
156
+ areAllExpanded: !areAllExpanded,
157
+ collapseAllAriaLabel: collapseAllAriaLabel,
158
+ onToggle: onCollapseAll
159
+ }}
160
+ ></Th>
161
+ {expandableColumns.map((column) => (
162
+ <Th key={column}>{column}</Th>
163
+ ))}
164
+ </Tr>
165
+ </Thead>
192
166
 
193
- return (
194
- <React.Fragment>
195
- <DashboardWrapper hasPageTemplateTitle>
196
- <PageSection
197
- padding={{
198
- default: 'noPadding',
199
- xl: 'padding'
200
- }}
201
- >
202
- <Card component="div">
203
- <Table
204
- aria-label="Collapsible table"
205
- onCollapse={onCollapse}
206
- rows={rows}
207
- cells={expandableColumns}
208
- canCollapseAll={true}
209
- collapseAllAriaLabel={collapseAllAriaLabel}
210
- expandId="expand-collapse-all-demo"
211
- >
212
- <TableHeader />
213
- <TableBody />
214
- </Table>
215
- </Card>
216
- </PageSection>
217
- </DashboardWrapper>
218
- </React.Fragment>
219
- );
220
- }
221
- }
167
+ {serverData.map((server, serverIndex) => (
168
+ <Tbody key={server.name} isExpanded={isServerExpanded(server)}>
169
+ <Tr>
170
+ <Td
171
+ expand={
172
+ server.details
173
+ ? {
174
+ rowIndex: serverIndex,
175
+ isExpanded: isServerExpanded(server),
176
+ onToggle: () => setServerExpanded(server, !isServerExpanded(server))
177
+ }
178
+ : undefined
179
+ }
180
+ >
181
+ <ExpandableRowContent>{server.details}</ExpandableRowContent>
182
+ </Td>
183
+ <Td>{server?.name}</Td>
184
+ <Td>{server?.threads}</Td>
185
+ <Td>{server?.applications}</Td>
186
+ <Td>{server?.workspaces}</Td>
187
+ <Td>{server?.status?.title}</Td>
188
+ </Tr>
189
+ <Tr isExpanded={isServerExpanded(server)}>
190
+ <Td></Td>
191
+ <Td colSpan={expandableColumns.length}>
192
+ <ExpandableRowContent>{server?.details}</ExpandableRowContent>
193
+ </Td>
194
+ </Tr>
195
+ </Tbody>
196
+ ))}
197
+ </Table>
198
+ </Card>
199
+ </PageSection>
200
+ </DashboardWrapper>
201
+ </React.Fragment>
202
+ );
203
+ };