Debian Codegraph @

node/@patternfly/react-table/src/components/Table/examples/TableNestedStickyHeader.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 as number,
105 direction: activeSortDirection as 'asc' | 'desc'
106 },
107 onSort: (_event, index, direction) => {
108 setActiveSortIndex(index);
109 setActiveSortDirection(direction);
110 },
111 columnIndex
112 });
113
114 return (
115 <div style={{ height: '250px' }}>
116 <InnerScrollContainer>
117 <Table aria-label="Nested column headers table" gridBreakPoint="" isStickyHeader>
118 <Thead hasNestedHeader>
119 <Tr>
120 <Th hasRightBorder colSpan={3}>
121 {columnNames.pods}
122 </Th>
123 <Th hasRightBorder colSpan={2}>
124 {columnNames.ports}
125 </Th>
126 <Th modifier="fitContent" hasRightBorder rowSpan={2} sort={getSortParams(5)}>
127 {columnNames.protocol}
128 </Th>
129 <Th modifier="fitContent" hasRightBorder rowSpan={2} sort={getSortParams(6)}>
130 {columnNames.flowRate}
131 </Th>
132 <Th modifier="fitContent" hasRightBorder rowSpan={2} sort={getSortParams(7)}>
133 {columnNames.traffic}
134 </Th>
135 <Th modifier="fitContent" rowSpan={2} sort={getSortParams(8)}>
136 {columnNames.packets}
137 </Th>
138 </Tr>
139 <Tr>
140 <Th isSubheader sort={getSortParams(0)}>
141 {columnNames.source}
142 </Th>
143 <Th isSubheader sort={getSortParams(1)}>
144 {columnNames.destination}
145 </Th>
146 <Th isSubheader modifier="fitContent" hasRightBorder sort={getSortParams(2)}>
147 {columnNames.datetime}
148 </Th>
149 <Th isSubheader modifier="fitContent" sort={getSortParams(3)}>
150 {columnNames.source}
151 </Th>
152 <Th isSubheader modifier="fitContent" hasRightBorder sort={getSortParams(4)}>
153 {columnNames.destination}
154 </Th>
155 </Tr>
156 <Tr isBorderRow aria-hidden="true">
157 <Td colSpan={9}></Td>
158 </Tr>
159 </Thead>
160 <Tbody>
161 {sortedConnections.map((connection) => (
162 <Tr key={connection.source.podName}>
163 <Td dataLabel={columnNames.source}>{connection.source.podName}</Td>
164 <Td dataLabel={columnNames.destination}>{connection.destination.podName}</Td>
165 <Td dataLabel={columnNames.datetime}>
166 <div>
167 <Timestamp dateFormat="full" timeFormat="medium" date={new Date(connection.timestamp)} />
168 </div>
169 </Td>
170 <Td dataLabel={columnNames.source}>
171 <Stack>
172 <StackItem>
173 <span>{connection.source.port.num}</span>
174 </StackItem>
175 <StackItem>
176 <small>({connection.source.port.protocol})</small>
177 </StackItem>
178 </Stack>
179 </Td>
180 <Td dataLabel={columnNames.destination}>
181 <Stack>
182 <StackItem>
183 <span>{connection.destination.port.num}</span>
184 </StackItem>
185 <StackItem>
186 <small>({connection.destination.port.protocol})</small>
187 </StackItem>
188 </Stack>
189 </Td>
190 <Td dataLabel={columnNames.protocol}>{connection.protocol}</Td>
191 <Td dataLabel={columnNames.flowRate}>{connection.flowRate}</Td>
192 <Td dataLabel={columnNames.traffic}>{connection.traffic}</Td>
193 <Td dataLabel={columnNames.packets}>{connection.packets}</Td>
194 </Tr>
195 ))}
196 </Tbody>
197 </Table>
198 </InnerScrollContainer>
199 </div>
200 );
201 };