Debian Codegraph @

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

raw
1 import { useState } from 'react';
2 import { Table, Thead, Tr, Th, Tbody, Td, InnerScrollContainer, ThProps } from '@patternfly/react-table';
3 import RhUiBlueprintIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-blueprint-icon';
4
5 interface Fact {
6 name: string;
7 state: string;
8 detail1: string;
9 detail2: string;
10 detail3: string;
11 detail4: string;
12 detail5: string;
13 detail6: string;
14 detail7: string;
15 }
16
17 export const TableMultipleStickyColumns: React.FunctionComponent = () => {
18 // In real usage, this data would come from some external source like an API via props.
19 const facts: Fact[] = Array.from({ length: 9 }, (_, index) => ({
20 name: `Fact ${index + 1}`,
21 state: `State ${index + 1}`,
22 detail1: `Test cell ${index + 1}-3`,
23 detail2: `Test cell ${index + 1}-4`,
24 detail3: `Test cell ${index + 1}-5`,
25 detail4: `Test cell ${index + 1}-6`,
26 detail5: `Test cell ${index + 1}-7`,
27 detail6: `Test cell ${index + 1}-8`,
28 detail7: `Test cell ${index + 1}-9`
29 }));
30
31 const columnNames = {
32 name: 'Fact',
33 state: 'State',
34 header3: 'Header 3',
35 header4: 'Header 4',
36 header5: 'Header 5',
37 header6: 'Header 6',
38 header7: 'Header 7',
39 header8: 'Header 8',
40 header9: 'Header 9'
41 };
42
43 // Index of the currently sorted column
44 // Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key
45 // as the identifier of the sorted column. See the "Compound expandable" example.
46 const [activeSortIndex, setActiveSortIndex] = useState<number | null>(null);
47
48 // Sort direction of the currently sorted column
49 const [activeSortDirection, setActiveSortDirection] = useState<'asc' | 'desc' | null>(null);
50
51 // Since OnSort specifies sorted columns by index, we need sortable values for our object by column index.
52 // This example is trivial since our data objects just contain strings, but if the data was more complex
53 // this would be a place to return simplified string or number versions of each column to sort by.
54 const getSortableRowValues = (fact: Fact): (string | number)[] => {
55 const { name, state, detail1, detail2, detail3, detail4, detail5, detail6, detail7 } = fact;
56 return [name, state, detail1, detail2, detail3, detail4, detail5, detail6, detail7];
57 };
58
59 // Note that we perform the sort as part of the component's render logic and not in onSort.
60 // We shouldn't store the list of data in state because we don't want to have to sync that with props.
61 let sortedFacts = facts;
62 if (activeSortIndex !== null) {
63 sortedFacts = facts.sort((a, b) => {
64 const aValue = getSortableRowValues(a)[activeSortIndex];
65 const bValue = getSortableRowValues(b)[activeSortIndex];
66 if (aValue === bValue) {
67 return 0;
68 }
69 if (activeSortDirection === 'asc') {
70 return aValue > bValue ? 1 : -1;
71 } else {
72 return bValue > aValue ? 1 : -1;
73 }
74 });
75 }
76
77 const getSortParams = (columnIndex: number): ThProps['sort'] => ({
78 sortBy: {
79 index: activeSortIndex,
80 direction: activeSortDirection
81 },
82 onSort: (_event, index, direction) => {
83 setActiveSortIndex(index);
84 setActiveSortDirection(direction);
85 },
86 columnIndex
87 });
88
89 return (
90 <InnerScrollContainer>
91 <Table aria-label="Multiple sticky column table" gridBreakPoint="">
92 <Thead>
93 <Tr>
94 <Th isStickyColumn modifier="truncate" sort={getSortParams(0)}>
95 {columnNames.name}
96 </Th>
97 <Th
98 isStickyColumn
99 stickyMinWidth="120px"
100 stickyLeftOffset="120px"
101 hasRightBorder
102 modifier="truncate"
103 sort={getSortParams(1)}
104 >
105 {columnNames.state}
106 </Th>
107 <Th modifier="truncate">{columnNames.header3}</Th>
108 <Th modifier="truncate">{columnNames.header4}</Th>
109 <Th modifier="truncate">{columnNames.header5}</Th>
110 <Th modifier="truncate">{columnNames.header6}</Th>
111 <Th modifier="truncate">{columnNames.header7}</Th>
112 <Th modifier="truncate">{columnNames.header8}</Th>
113 <Th modifier="truncate">{columnNames.header9}</Th>
114 </Tr>
115 </Thead>
116 <Tbody>
117 {sortedFacts.map((fact) => (
118 <Tr key={fact.name}>
119 <Th isStickyColumn modifier="truncate">
120 {fact.name}
121 </Th>
122 <Th isStickyColumn stickyMinWidth="120px" stickyLeftOffset="120px" modifier="truncate" hasRightBorder>
123 <RhUiBlueprintIcon />
124 {` ${fact.state}`}
125 </Th>
126 <Td modifier="nowrap" dataLabel={columnNames.header3}>
127 {fact.detail1}
128 </Td>
129 <Td modifier="nowrap" dataLabel={columnNames.header4}>
130 {fact.detail2}
131 </Td>
132 <Td modifier="nowrap" dataLabel={columnNames.header5}>
133 {fact.detail3}
134 </Td>
135 <Td modifier="nowrap" dataLabel={columnNames.header6}>
136 {fact.detail4}
137 </Td>
138 <Td modifier="nowrap" dataLabel={columnNames.header7}>
139 {fact.detail5}
140 </Td>
141 <Td modifier="nowrap" dataLabel={columnNames.header8}>
142 {fact.detail6}
143 </Td>
144 <Td modifier="nowrap" dataLabel={columnNames.header9}>
145 {fact.detail7}
146 </Td>
147 </Tr>
148 ))}
149 </Tbody>
150 </Table>
151 </InnerScrollContainer>
152 );
153 };