Debian Codegraph @

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

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