Debian Codegraph @

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

raw
1 import { useState } from 'react';
2 import { Table, Thead, Tr, Th, Tbody, Td, InnerScrollContainer } from '@patternfly/react-table';
3 import { PageSection } from '@patternfly/react-core';
4 import { DashboardWrapper } from '@patternfly/react-table/dist/esm/demos/DashboardWrapper';
5
6 type Direction = 'asc' | 'desc' | undefined;
7
8 interface Fact {
9 name: string;
10 state: string;
11 detail1: string;
12 detail2: string;
13 detail3: string;
14 detail4: string;
15 detail5: string;
16 detail6: string;
17 detail7: string;
18 detail8: string;
19 detail9: string;
20 detail10: string;
21 detail11: string;
22 detail12: string;
23 detail13: string;
24 detail14: string;
25 }
26
27 export const TableStickyFirstColumn = () => {
28 const facts = Array.from(
29 {
30 length: 9
31 },
32 (_, index) => ({
33 name: `Fact ${index + 1}`,
34 state: `State ${index + 1}`,
35 detail1: `Test cell ${index + 1}-3`,
36 detail2: `Test cell ${index + 1}-4`,
37 detail3: `Test cell ${index + 1}-5`,
38 detail4: `Test cell ${index + 1}-6`,
39 detail5: `Test cell ${index + 1}-7`,
40 detail6: `Test cell ${index + 1}-8`,
41 detail7: `Test cell ${index + 1}-9`,
42 detail8: `Test cell ${index + 1}-10`,
43 detail9: `Test cell ${index + 1}-11`,
44 detail10: `Test cell ${index + 1}-12`,
45 detail11: `Test cell ${index + 1}-13`,
46 detail12: `Test cell ${index + 1}-14`,
47 detail13: `Test cell ${index + 1}-15`,
48 detail14: `Test cell ${index + 1}-16`
49 })
50 );
51 const columnNames = {
52 name: 'Fact',
53 state: 'State',
54 header3: 'Header 3',
55 header4: 'Header 4',
56 header5: 'Header 5',
57 header6: 'Header 6',
58 header7: 'Header 7',
59 header8: 'Header 8',
60 header9: 'Header 9',
61 header10: 'Header 10',
62 header11: 'Header 11',
63 header12: 'Header 12',
64 header13: 'Header 13',
65 header14: 'Header 14',
66 header15: 'Header 15',
67 header16: 'Header 16'
68 };
69
70 const [activeSortIndex, setActiveSortIndex] = useState(-1);
71 const [activeSortDirection, setActiveSortDirection] = useState<Direction>('asc');
72 const getSortableRowValues = (fact: Fact) => {
73 const {
74 name,
75 state,
76 detail1,
77 detail2,
78 detail3,
79 detail4,
80 detail5,
81 detail6,
82 detail7,
83 detail8,
84 detail9,
85 detail10,
86 detail11,
87 detail12,
88 detail13,
89 detail14
90 } = fact;
91 return [
92 name,
93 state,
94 detail1,
95 detail2,
96 detail3,
97 detail4,
98 detail5,
99 detail6,
100 detail7,
101 detail8,
102 detail9,
103 detail10,
104 detail11,
105 detail12,
106 detail13,
107 detail14
108 ];
109 };
110 let sortedFacts = facts;
111 if (activeSortIndex > -1) {
112 sortedFacts = facts.sort((a, b) => {
113 const aValue = getSortableRowValues(a)[activeSortIndex];
114 const bValue = getSortableRowValues(b)[activeSortIndex];
115 if (aValue === bValue) {
116 return 0;
117 }
118 if (activeSortDirection === 'asc') {
119 return aValue > bValue ? 1 : -1;
120 } else {
121 return bValue > aValue ? 1 : -1;
122 }
123 });
124 }
125 const getSortParams = (columnIndex: number) => ({
126 sortBy: {
127 index: activeSortIndex,
128 direction: activeSortDirection
129 },
130 onSort: (_event: React.MouseEvent, index: number, direction: Direction) => {
131 setActiveSortIndex(index);
132 setActiveSortDirection(direction);
133 },
134 columnIndex
135 });
136 return (
137 <DashboardWrapper hasPageTemplateTitle>
138 <PageSection
139 isWidthLimited
140 padding={{ default: 'noPadding', xl: 'padding' }}
141 aria-label="Sticky column table data"
142 >
143 <InnerScrollContainer>
144 <Table aria-label="Sticky column table" gridBreakPoint="">
145 <Thead>
146 <Tr>
147 <Th isStickyColumn stickyMinWidth="100px" hasRightBorder modifier="fitContent" sort={getSortParams(0)}>
148 {columnNames.name}
149 </Th>
150 <Th modifier="fitContent" sort={getSortParams(1)}>
151 {columnNames.state}
152 </Th>
153 <Th>{columnNames.header3}</Th>
154 <Th>{columnNames.header4}</Th>
155 <Th>{columnNames.header5}</Th>
156 <Th>{columnNames.header6}</Th>
157 <Th>{columnNames.header7}</Th>
158 <Th>{columnNames.header8}</Th>
159 <Th>{columnNames.header9}</Th>
160 <Th>{columnNames.header10}</Th>
161 <Th>{columnNames.header11}</Th>
162 <Th>{columnNames.header12}</Th>
163 <Th>{columnNames.header13}</Th>
164 <Th>{columnNames.header14}</Th>
165 <Th>{columnNames.header15}</Th>
166 <Th>{columnNames.header16}</Th>
167 </Tr>
168 </Thead>
169 <Tbody>
170 {sortedFacts.map((fact) => (
171 <Tr key={fact.name}>
172 <Th isStickyColumn stickyMinWidth="100px" hasRightBorder modifier="truncate">
173 {fact.name}
174 </Th>
175 <Td modifier="nowrap" dataLabel={columnNames.state}>
176 {fact.state}
177 </Td>
178 <Td modifier="nowrap" dataLabel={columnNames.header3}>
179 {fact.detail1}
180 </Td>
181 <Td modifier="nowrap" dataLabel={columnNames.header4}>
182 {fact.detail2}
183 </Td>
184 <Td modifier="nowrap" dataLabel={columnNames.header5}>
185 {fact.detail3}
186 </Td>
187 <Td modifier="nowrap" dataLabel={columnNames.header6}>
188 {fact.detail4}
189 </Td>
190 <Td modifier="nowrap" dataLabel={columnNames.header7}>
191 {fact.detail5}
192 </Td>
193 <Td modifier="nowrap" dataLabel={columnNames.header8}>
194 {fact.detail6}
195 </Td>
196 <Td modifier="nowrap" dataLabel={columnNames.header9}>
197 {fact.detail7}
198 </Td>
199 <Td modifier="nowrap" dataLabel={columnNames.header10}>
200 {fact.detail8}
201 </Td>
202 <Td modifier="nowrap" dataLabel={columnNames.header11}>
203 {fact.detail9}
204 </Td>
205 <Td modifier="nowrap" dataLabel={columnNames.header12}>
206 {fact.detail10}
207 </Td>
208 <Td modifier="nowrap" dataLabel={columnNames.header13}>
209 {fact.detail11}
210 </Td>
211 <Td modifier="nowrap" dataLabel={columnNames.header14}>
212 {fact.detail12}
213 </Td>
214 <Td modifier="nowrap" dataLabel={columnNames.header15}>
215 {fact.detail13}
216 </Td>
217 <Td modifier="nowrap" dataLabel={columnNames.header16}>
218 {fact.detail14}
219 </Td>
220 </Tr>
221 ))}
222 </Tbody>
223 </Table>
224 </InnerScrollContainer>
225 </PageSection>
226 </DashboardWrapper>
227 );
228 };