-
Notifications
You must be signed in to change notification settings - Fork 381
feat(Table): support dynamic sticky styling #12348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { useLayoutEffect, useRef, useState } from 'react'; | ||
| import { Table, Thead, Tr, Th, Tbody, Td, InnerScrollContainer } from '@patternfly/react-table'; | ||
| import BlueprintIcon from '@patternfly/react-icons/dist/esm/icons/blueprint-icon'; | ||
|
|
||
| interface Fact { | ||
| name: string; | ||
| state: string; | ||
| detail1: string; | ||
| detail2: string; | ||
| detail3: string; | ||
| detail4: string; | ||
| detail5: string; | ||
| detail6: string; | ||
| detail7: string; | ||
| } | ||
|
|
||
| const useIsStuckFromScrollParent = ({ | ||
| shouldTrack, | ||
| scrollParentRef | ||
| }: { | ||
| /** Indicates whether to track the scroll top position of the scroll parent element */ | ||
| shouldTrack: boolean; | ||
| /** Reference to the scroll parent element */ | ||
| scrollParentRef: React.RefObject<any>; | ||
| }): boolean => { | ||
|
Comment on lines
+17
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify the ref type exposed/expected by InnerScrollContainer and align this example type.
fd -i "InnerScrollContainer.tsx"
rg -n -C3 "forwardRef|InnerScrollContainer|RefObject|HTMLDivElement|HTMLElement" packages/react-table/src/components/Table/InnerScrollContainer.tsx
rg -n -C2 "useIsStuckFromScrollParent|scrollParentRef" packages/react-table/src/components/Table/examples/TableStickyHeaderDynamic.tsxRepository: patternfly/patternfly-react Length of output: 2319 Replace Line 24 uses Proposed diff- scrollParentRef: React.RefObject<any>;
+ scrollParentRef: React.RefObject<HTMLDivElement>;🤖 Prompt for AI Agents |
||
| const [isStuck, setIsStuck] = useState(false); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (!shouldTrack) { | ||
| setIsStuck(false); | ||
| return; | ||
| } | ||
|
|
||
| const scrollElement = scrollParentRef.current; | ||
| if (!scrollElement) { | ||
| setIsStuck(false); | ||
| return; | ||
| } | ||
|
|
||
| const syncFromScroll = () => { | ||
| setIsStuck(scrollElement.scrollTop > 0); | ||
| }; | ||
| syncFromScroll(); | ||
| scrollElement.addEventListener('scroll', syncFromScroll, { passive: true }); | ||
| return () => scrollElement.removeEventListener('scroll', syncFromScroll); | ||
| }, [shouldTrack, scrollParentRef]); | ||
|
|
||
| return isStuck; | ||
| }; | ||
|
|
||
| export const TableStickyHeaderDynamic: React.FunctionComponent = () => { | ||
| const scrollContainerRef = useRef<HTMLDivElement>(null); | ||
| const isStuck = useIsStuckFromScrollParent({ shouldTrack: true, scrollParentRef: scrollContainerRef }); | ||
|
|
||
| // In real usage, this data would come from some external source like an API via props. | ||
| const facts: Fact[] = Array.from({ length: 9 }, (_, index) => ({ | ||
| name: `Fact ${index + 1}`, | ||
| state: `State ${index + 1}`, | ||
| detail1: `Test cell ${index + 1}-3`, | ||
| detail2: `Test cell ${index + 1}-4`, | ||
| detail3: `Test cell ${index + 1}-5`, | ||
| detail4: `Test cell ${index + 1}-6`, | ||
| detail5: `Test cell ${index + 1}-7`, | ||
| detail6: `Test cell ${index + 1}-8`, | ||
| detail7: `Test cell ${index + 1}-9` | ||
| })); | ||
|
|
||
| const columnNames = { | ||
| name: 'Fact', | ||
| state: 'State', | ||
| header3: 'Header 3', | ||
| header4: 'Header 4', | ||
| header5: 'Header 5', | ||
| header6: 'Header 6', | ||
| header7: 'Header 7', | ||
| header8: 'Header 8', | ||
| header9: 'Header 9' | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={{ height: '400px' }}> | ||
| <InnerScrollContainer ref={scrollContainerRef}> | ||
| <Table | ||
| aria-label="Dynamic sticky header table" | ||
| gridBreakPoint="" | ||
| isStickyHeaderBase | ||
| isStickyHeaderStuck={isStuck} | ||
| > | ||
| <Thead> | ||
| <Tr> | ||
| <Th modifier="truncate">{columnNames.name}</Th> | ||
| <Th modifier="truncate">{columnNames.state}</Th> | ||
| <Th modifier="truncate">{columnNames.header3}</Th> | ||
| <Th modifier="truncate">{columnNames.header4}</Th> | ||
| <Th modifier="truncate">{columnNames.header5}</Th> | ||
| <Th modifier="truncate">{columnNames.header6}</Th> | ||
| <Th modifier="truncate">{columnNames.header7}</Th> | ||
| <Th modifier="truncate">{columnNames.header8}</Th> | ||
| <Th modifier="truncate">{columnNames.header9}</Th> | ||
| </Tr> | ||
| </Thead> | ||
| <Tbody> | ||
| {facts.map((fact) => ( | ||
| <Tr key={fact.name}> | ||
| <Td modifier="nowrap" dataLabel={columnNames.name}> | ||
| {fact.name} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.state}> | ||
| <BlueprintIcon /> | ||
| {` ${fact.state}`} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header3}> | ||
| {fact.detail1} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header4}> | ||
| {fact.detail2} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header5}> | ||
| {fact.detail3} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header6}> | ||
| {fact.detail4} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header7}> | ||
| {fact.detail5} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header8}> | ||
| {fact.detail6} | ||
| </Td> | ||
| <Td modifier="nowrap" dataLabel={columnNames.header9}> | ||
| {fact.detail7} | ||
| </Td> | ||
| </Tr> | ||
| ))} | ||
| </Tbody> | ||
| </Table> | ||
| </InnerScrollContainer> | ||
| </div> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@edonehoo could you give this a quick look?