Debian Codegraph @

node/@patternfly/react-table/src/demos/examples/TableExpandCollapseAll.tsx

raw
1 import { Fragment, ReactNode, useEffect, useState } from 'react';
2 import { Content, Label, PageSection } from '@patternfly/react-core';
3 import { Table, Thead, Tbody, Tr, Th, Td, ExpandableRowContent } from '@patternfly/react-table';
4 import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper';
5
6 const expandableColumns = ['Servers', 'Threads', 'Applications', 'Workspaces', 'Status'];
7
8 interface Server {
9 name: string;
10 threads: number;
11 applications: number;
12 workspaces: number;
13 status: { title: ReactNode };
14 details: ReactNode;
15 }
16
17 const serverData: Server[] = [
18 {
19 name: 'US-Node 1',
20 threads: 18,
21 applications: 42,
22 workspaces: 7,
23 status: { title: <Label color="green">Running</Label> },
24 details: (
25 <Content>
26 <p>
27 Location<small>Boston</small>
28 </p>
29 <p>
30 Last Modified<small>2 hours ago</small>
31 </p>
32 <p>
33 URL<small>http://www.redhat.com/en/office-locations/US-node1</small>
34 </p>
35 </Content>
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 <Content>
46 <p>
47 Location<small>Atlanta</small>
48 </p>
49 <p>
50 Last Modified<small>5 hours ago</small>
51 </p>
52 <p>
53 URL<small>http://www.redhat.com/en/office-locations/US-node2</small>
54 </p>
55 </Content>
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 <Content>
66 <p>
67 Location<small>San Francisco</small>
68 </p>
69 <p>
70 Last Modified<small>20 minutes ago</small>
71 </p>
72 <p>
73 URL<small>http://www.redhat.com/en/office-locations/US-node3</small>
74 </p>
75 </Content>
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 <Content>
86 <p>
87 Location<small>Raleigh</small>
88 </p>
89 <p>
90 Last Modified<small>10 minutes ago</small>
91 </p>
92 <p>
93 URL<small>http://www.redhat.com/en/office-locations/US-node4</small>
94 </p>
95 </Content>
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 <Content>
106 <p>
107 Location<small>Atlanta</small>
108 </p>
109 <p>
110 Last Modified<small>15 minutes ago</small>
111 </p>
112 <p>
113 URL<small>http://www.redhat.com/en/office-locations/US-node5</small>
114 </p>
115 </Content>
116 )
117 }
118 ];
119
120 const initialExpandedServerNames = ['US-Node 2']; // Default to expanded
121
122 export const TableExpandCollapseAll: React.FunctionComponent = () => {
123 const [areAllExpanded, setAreAllExpanded] = useState(false);
124 const [collapseAllAriaLabel, setCollapseAllAriaLabel] = useState('Expand all');
125 const [expandedServerNames, setExpandedServerNames] = useState(initialExpandedServerNames);
126
127 useEffect(() => {
128 const allExpanded = expandedServerNames.length === serverData.length;
129 setAreAllExpanded(allExpanded);
130 setCollapseAllAriaLabel(allExpanded ? 'Collapse all' : 'Expand all');
131 }, [expandedServerNames]);
132
133 const setServerExpanded = (server: Server, isExpanding: boolean) => {
134 const otherExpandedServerNames = expandedServerNames.filter((r) => r !== server.name);
135 setExpandedServerNames(isExpanding ? [...otherExpandedServerNames, server.name] : otherExpandedServerNames);
136 };
137
138 const isServerExpanded = (server: Server) => expandedServerNames.includes(server.name);
139
140 // We want to be able to reference the original data object based on row index. But because an expanded
141 // row takes up two row indexes, servers[rowIndex] will not be accurate like it would in a normal table.
142 // One solution to this is to create an array of data objects indexed by the displayed row index.
143
144 const onCollapseAll = (_event: any, _rowIndex: number, isOpen: boolean) => {
145 setExpandedServerNames(isOpen ? [...serverData.map((server) => server.name)] : []);
146 };
147
148 return (
149 <Fragment>
150 <DashboardWrapper hasPageTemplateTitle>
151 <PageSection
152 padding={{
153 default: 'noPadding',
154 xl: 'padding'
155 }}
156 aria-label="Collapsible table data"
157 >
158 <Table isExpandable hasAnimations aria-label="Collapsible table">
159 <Thead>
160 <Tr>
161 <Th
162 expand={{
163 areAllExpanded: !areAllExpanded,
164 collapseAllAriaLabel,
165 onToggle: onCollapseAll
166 }}
167 aria-label="Row expansion"
168 />
169 {expandableColumns.map((column) => (
170 <Th key={column}>{column}</Th>
171 ))}
172 </Tr>
173 </Thead>
174
175 {serverData.map((server, serverIndex) => (
176 <Tbody key={server.name} isExpanded={isServerExpanded(server)}>
177 <Tr isContentExpanded={isServerExpanded(server)}>
178 <Td
179 expand={
180 server.details
181 ? {
182 rowIndex: serverIndex,
183 isExpanded: isServerExpanded(server),
184 onToggle: () => setServerExpanded(server, !isServerExpanded(server))
185 }
186 : undefined
187 }
188 >
189 <ExpandableRowContent>{server.details}</ExpandableRowContent>
190 </Td>
191 <Td>{server?.name}</Td>
192 <Td>{server?.threads}</Td>
193 <Td>{server?.applications}</Td>
194 <Td>{server?.workspaces}</Td>
195 <Td>{server?.status?.title}</Td>
196 </Tr>
197 <Tr isExpanded={isServerExpanded(server)}>
198 <Td></Td>
199 <Td colSpan={expandableColumns.length}>
200 <ExpandableRowContent>{server?.details}</ExpandableRowContent>
201 </Td>
202 </Tr>
203 </Tbody>
204 ))}
205 </Table>
206 </PageSection>
207 </DashboardWrapper>
208 </Fragment>
209 );
210 };