Debian Codegraph @

node/@patternfly/react-table/src/components/Table/examples/TableNestedHeaders.tsx

raw
1 import { useState } from 'react';
2 import { Table, Thead, Tr, Th, Tbody, Td, InnerScrollContainer, ThProps } from '@patternfly/react-table';
3 import { Stack, StackItem, Timestamp } from '@patternfly/react-core';
4 interface PodConnection {
5 source: {
6 podName: string;
7 port: { num: number; protocol: string };
8 };
9 destination: {
10 podName: string;
11 port: { num: number; protocol: string };
12 };
13 timestamp: string;
14 protocol: string;
15 flowRate: string;
16 traffic: string;
17 packets: number;
18 }
19
20 export const TableNestedHeaders: React.FunctionComponent = () => {
21 // In real usage, this data would come from some external source like an API via props.
22 const connections: PodConnection[] = [
23 {
24 source: { podName: 'api-pod-source-name', port: { num: 443, protocol: 'HTTPS' } },
25 destination: { podName: 'api-pod-destination-name', port: { num: 24, protocol: 'SMTP' } },
26 timestamp: '2021-06-22T19:58:24.000Z',
27 protocol: 'TCP',
28 flowRate: '1.9 Kbps',
29 traffic: '2.1 KB',
30 packets: 3
31 },
32 {
33 source: { podName: 'api-pod-source2-name', port: { num: 80, protocol: 'HTTP' } },
34 destination: { podName: 'api-pod-destination2-name', port: { num: 24, protocol: 'SMTP' } },
35 timestamp: '2021-06-22T21:42:01.000Z',
36 protocol: 'UDP',
37 flowRate: '3.4 Kbps',
38 traffic: '6.1 KB',
39 packets: 7
40 }
41 ];
42
43 const columnNames = {
44 pods: 'Pods',
45 source: 'Source',
46 destination: 'Destination',
47 datetime: 'Date & Time',
48 ports: 'Ports',
49 protocol: 'Protocol',
50 flowRate: 'Flow rate',
51 traffic: 'Traffic',
52 packets: 'Packets'
53 };
54
55 // Index of the currently sorted column
56 // Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key
57 // as the identifier of the sorted column. See the "Compound expandable" example.
58 const [activeSortIndex, setActiveSortIndex] = useState<number | null>(null);
59
60 // Sort direction of the currently sorted column
61 const [activeSortDirection, setActiveSortDirection] = useState<'asc' | 'desc' | null>(null);
62
63 // Since OnSort specifies sorted columns by index, we need sortable values for our object by column index.
64 const getSortableRowValues = (connection: PodConnection): (string | number)[] => {
65 const { source, destination, timestamp, protocol, flowRate, traffic, packets } = connection;
66 return [
67 source.podName,
68 destination.podName,
69 timestamp,
70 source.port.num,
71 destination.port.num,
72 protocol,
73 flowRate,
74 traffic,
75 packets
76 ];
77 };
78
79 // Note that we perform the sort as part of the component's render logic and not in onSort.
80 // We shouldn't store the list of data in state because we don't want to have to sync that with props.
81 let sortedConnections = connections;
82 if (activeSortIndex !== null) {
83 sortedConnections = connections.sort((a, b) => {
84 const aValue = getSortableRowValues(a)[activeSortIndex];
85 const bValue = getSortableRowValues(b)[activeSortIndex];
86 if (typeof aValue === 'number') {
87 // Numeric sort
88 if (activeSortDirection === 'asc') {
89 return (aValue as number) - (bValue as number);
90 }
91 return (bValue as number) - (aValue as number);
92 } else {
93 // String sort
94 if (activeSortDirection === 'asc') {
95 return (aValue as string).localeCompare(bValue as string);
96 }
97 return (bValue as string).localeCompare(aValue as string);
98 }
99 });
100 }
101
102 const getSortParams = (columnIndex: number): ThProps['sort'] => ({
103 sortBy: {
104 index: activeSortIndex,
105 direction: activeSortDirection
106 },
107 onSort: (_event, index, direction) => {
108 setActiveSortIndex(index);
109 setActiveSortDirection(direction);
110 },
111 columnIndex
112 });
113
114 return (
115 <InnerScrollContainer>
116 <Table aria-label="Nested column headers table" gridBreakPoint="">
117 <Thead hasNestedHeader>
118 <Tr>
119 <Th hasRightBorder colSpan={3}>
120 {columnNames.pods}
121 </Th>
122 <Th hasRightBorder colSpan={2}>
123 {columnNames.ports}
124 </Th>
125 <Th modifier="fitContent" hasRightBorder rowSpan={2} sort={getSortParams(5)}>
126 {columnNames.protocol}
127 </Th>
128 <Th modifier="fitContent" hasRightBorder rowSpan={2} sort={getSortParams(6)}>
129 {columnNames.flowRate}
130 </Th>
131 <Th modifier="fitContent" hasRightBorder rowSpan={2} sort={getSortParams(7)}>
132 {columnNames.traffic}
133 </Th>
134 <Th modifier="fitContent" rowSpan={2} sort={getSortParams(8)}>
135 {columnNames.packets}
136 </Th>
137 </Tr>
138 <Tr>
139 <Th isSubheader sort={getSortParams(0)}>
140 {columnNames.source}
141 </Th>
142 <Th isSubheader sort={getSortParams(1)}>
143 {columnNames.destination}
144 </Th>
145 <Th isSubheader modifier="fitContent" hasRightBorder sort={getSortParams(2)}>
146 {columnNames.datetime}
147 </Th>
148 <Th isSubheader modifier="fitContent" sort={getSortParams(3)}>
149 {columnNames.source}
150 </Th>
151 <Th isSubheader modifier="fitContent" hasRightBorder sort={getSortParams(4)}>
152 {columnNames.destination}
153 </Th>
154 </Tr>
155 </Thead>
156 <Tbody>
157 {sortedConnections.map((connection) => (
158 <Tr key={connection.source.podName}>
159 <Td dataLabel={columnNames.source}>{connection.source.podName}</Td>
160 <Td dataLabel={columnNames.destination}>{connection.destination.podName}</Td>\
161 <Td dataLabel={columnNames.datetime}>
162 <div>
163 <Timestamp dateFormat="full" timeFormat="medium" date={new Date(connection.timestamp)} />
164 </div>
165 </Td>
166 <Td dataLabel={columnNames.source}>
167 <Stack>
168 <StackItem>
169 <span>{connection.source.port.num}</span>
170 </StackItem>
171 <StackItem>
172 <small>({connection.source.port.protocol})</small>
173 </StackItem>
174 </Stack>
175 </Td>
176 <Td dataLabel={columnNames.destination}>
177 <Stack>
178 <StackItem>
179 <span>{connection.destination.port.num}</span>
180 </StackItem>
181 <StackItem>
182 <small>({connection.destination.port.protocol})</small>
183 </StackItem>
184 </Stack>
185 </Td>
186 <Td dataLabel={columnNames.protocol}>{connection.protocol}</Td>
187 <Td dataLabel={columnNames.flowRate}>{connection.flowRate}</Td>
188 <Td dataLabel={columnNames.traffic}>{connection.traffic}</Td>
189 <Td dataLabel={columnNames.packets}>{connection.packets}</Td>
190 </Tr>
191 ))}
192 </Tbody>
193 </Table>
194 </InnerScrollContainer>
195 );
196 };