Debian Codegraph @

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

raw
1 import { useLayoutEffect, useRef, useState } from 'react';
2 import { Table, Thead, Tr, Th, Tbody, Td, InnerScrollContainer } 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 const useIsStuckFromScrollParent = ({
18 shouldTrack,
19 scrollParentRef
20 }: {
21 /** Indicates whether to track the scroll top position of the scroll parent element */
22 shouldTrack: boolean;
23 /** Reference to the scroll parent element */
24 scrollParentRef: React.RefObject<any>;
25 }): boolean => {
26 const [isStuck, setIsStuck] = useState(false);
27
28 useLayoutEffect(() => {
29 if (!shouldTrack) {
30 setIsStuck(false);
31 return;
32 }
33
34 const scrollElement = scrollParentRef.current;
35 if (!scrollElement) {
36 setIsStuck(false);
37 return;
38 }
39
40 const syncFromScroll = () => {
41 setIsStuck(scrollElement.scrollTop > 0);
42 };
43 syncFromScroll();
44 scrollElement.addEventListener('scroll', syncFromScroll, { passive: true });
45 return () => scrollElement.removeEventListener('scroll', syncFromScroll);
46 }, [shouldTrack, scrollParentRef]);
47
48 return isStuck;
49 };
50
51 export const TableStickyHeaderDynamic: React.FunctionComponent = () => {
52 const scrollContainerRef = useRef<HTMLDivElement>(null);
53 const isStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef: scrollContainerRef });
54
55 // In real usage, this data would come from some external source like an API via props.
56 const facts: Fact[] = Array.from({ length: 9 }, (_, index) => ({
57 name: `Fact ${index + 1}`,
58 state: `State ${index + 1}`,
59 detail1: `Test cell ${index + 1}-3`,
60 detail2: `Test cell ${index + 1}-4`,
61 detail3: `Test cell ${index + 1}-5`,
62 detail4: `Test cell ${index + 1}-6`,
63 detail5: `Test cell ${index + 1}-7`,
64 detail6: `Test cell ${index + 1}-8`,
65 detail7: `Test cell ${index + 1}-9`
66 }));
67
68 const columnNames = {
69 name: 'Fact',
70 state: 'State',
71 header3: 'Header 3',
72 header4: 'Header 4',
73 header5: 'Header 5',
74 header6: 'Header 6',
75 header7: 'Header 7',
76 header8: 'Header 8',
77 header9: 'Header 9'
78 };
79
80 return (
81 <div style={{ height: '400px' }}>
82 <InnerScrollContainer ref={scrollContainerRef}>
83 <Table
84 aria-label="Dynamic sticky header table"
85 gridBreakPoint=""
86 isStickyHeaderBase
87 isStickyHeaderStuck={isStuck}
88 >
89 <Thead>
90 <Tr>
91 <Th modifier="truncate">{columnNames.name}</Th>
92 <Th modifier="truncate">{columnNames.state}</Th>
93 <Th modifier="truncate">{columnNames.header3}</Th>
94 <Th modifier="truncate">{columnNames.header4}</Th>
95 <Th modifier="truncate">{columnNames.header5}</Th>
96 <Th modifier="truncate">{columnNames.header6}</Th>
97 <Th modifier="truncate">{columnNames.header7}</Th>
98 <Th modifier="truncate">{columnNames.header8}</Th>
99 <Th modifier="truncate">{columnNames.header9}</Th>
100 </Tr>
101 </Thead>
102 <Tbody>
103 {facts.map((fact) => (
104 <Tr key={fact.name}>
105 <Td modifier="nowrap" dataLabel={columnNames.name}>
106 {fact.name}
107 </Td>
108 <Td modifier="nowrap" dataLabel={columnNames.state}>
109 <RhUiBlueprintIcon />
110 {` ${fact.state}`}
111 </Td>
112 <Td modifier="nowrap" dataLabel={columnNames.header3}>
113 {fact.detail1}
114 </Td>
115 <Td modifier="nowrap" dataLabel={columnNames.header4}>
116 {fact.detail2}
117 </Td>
118 <Td modifier="nowrap" dataLabel={columnNames.header5}>
119 {fact.detail3}
120 </Td>
121 <Td modifier="nowrap" dataLabel={columnNames.header6}>
122 {fact.detail4}
123 </Td>
124 <Td modifier="nowrap" dataLabel={columnNames.header7}>
125 {fact.detail5}
126 </Td>
127 <Td modifier="nowrap" dataLabel={columnNames.header8}>
128 {fact.detail6}
129 </Td>
130 <Td modifier="nowrap" dataLabel={columnNames.header9}>
131 {fact.detail7}
132 </Td>
133 </Tr>
134 ))}
135 </Tbody>
136 </Table>
137 </InnerScrollContainer>
138 </div>
139 );
140 };