-
Notifications
You must be signed in to change notification settings - Fork 392
feat(Table): add composable sticky footer #12645
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,23 @@ | ||
| import { forwardRef } from 'react'; | ||
| import { css } from '@patternfly/react-styles'; | ||
| import styles from '@patternfly/react-styles/css/components/Table/table'; | ||
|
|
||
| export interface TfootProps extends React.HTMLProps<HTMLTableSectionElement> { | ||
| /** Content rendered inside the <tfoot> row group */ | ||
| children?: React.ReactNode; | ||
| /** Additional classes added to the <tfoot> element */ | ||
| className?: string; | ||
| /** @hide Forwarded ref */ | ||
| innerRef?: React.Ref<any>; | ||
| } | ||
|
|
||
| const TfootBase: React.FunctionComponent<TfootProps> = ({ children, className, innerRef, ...props }: TfootProps) => ( | ||
| <tfoot className={css(styles.tableTfoot, className)} ref={innerRef} {...props}> | ||
| {children} | ||
| </tfoot> | ||
| ); | ||
|
|
||
| export const Tfoot = forwardRef((props: TfootProps, ref: React.Ref<HTMLTableSectionElement>) => ( | ||
| <TfootBase {...props} innerRef={ref} /> | ||
| )); | ||
| Tfoot.displayName = 'Tfoot'; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| import { render, screen } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||||||||||||
| import { Tfoot } from '../Tfoot'; | ||||||||||||||||||||||||||||||||||||||||||||
| import styles from '@patternfly/react-styles/css/components/Table/table'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('Renders a tfoot element with the table footer class', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||||||||||||
| <table> | ||||||||||||||||||||||||||||||||||||||||||||
| <Tfoot> | ||||||||||||||||||||||||||||||||||||||||||||
| <tr> | ||||||||||||||||||||||||||||||||||||||||||||
| <td>Footer</td> | ||||||||||||||||||||||||||||||||||||||||||||
| </tr> | ||||||||||||||||||||||||||||||||||||||||||||
| </Tfoot> | ||||||||||||||||||||||||||||||||||||||||||||
| </table> | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| expect(screen.getByText('Footer').closest('tfoot')).toHaveClass(styles.tableTfoot); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+16
Contributor
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. In addition to Rebeccas's comment above:
Suggested change
We want to try and use matchers that can more precisely get the element we want. Since we don't need to worry about any other rowgroup (thead or tbody) we should be okay to use that matcher here. Also just a minor tweak to the test name to match similar tests in other test files. |
||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| test('Forwards props, class names, and refs to the tfoot element', () => { | ||||||||||||||||||||||||||||||||||||||||||||
| const ref = { current: null } as React.RefObject<HTMLTableSectionElement>; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||||||||||||
| <table> | ||||||||||||||||||||||||||||||||||||||||||||
| <Tfoot ref={ref} className="custom-footer" data-testid="footer"> | ||||||||||||||||||||||||||||||||||||||||||||
| <tr /> | ||||||||||||||||||||||||||||||||||||||||||||
| </Tfoot> | ||||||||||||||||||||||||||||||||||||||||||||
| </table> | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| expect(screen.getByTestId('footer')).toHaveClass(styles.tableTfoot, 'custom-footer'); | ||||||||||||||||||||||||||||||||||||||||||||
| expect(ref.current).toBe(screen.getByTestId('footer')); | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+32
Contributor
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. Can we separate these out into 3 different tests? One for the ref, one for the custom class, and one for the props spreading? Also just using the getByRole matcher from above if we can. |
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||||
| import { Table, Thead, Tfoot, Tr, Th, Tbody, Td, InnerScrollContainer } from '@patternfly/react-table'; | ||||||
|
|
||||||
| export const TableStickyFooter: React.FunctionComponent = () => { | ||||||
| const rows = Array.from({ length: 12 }, (_, index) => index + 1); | ||||||
|
|
||||||
| return ( | ||||||
| <div style={{ height: '400px' }}> | ||||||
| <InnerScrollContainer> | ||||||
| <Table aria-label="Sticky footer table" gridBreakPoint="" isStickyFooter> | ||||||
|
Member
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.
Suggested change
|
||||||
| <Thead> | ||||||
| <Tr> | ||||||
| <Th>Item</Th> | ||||||
| <Th>Value</Th> | ||||||
| </Tr> | ||||||
| </Thead> | ||||||
| <Tbody> | ||||||
| {rows.map((row) => ( | ||||||
| <Tr key={row}> | ||||||
| <Td dataLabel="Item">Item {row}</Td> | ||||||
| <Td dataLabel="Value">Value {row}</Td> | ||||||
| </Tr> | ||||||
| ))} | ||||||
| </Tbody> | ||||||
| <Tfoot> | ||||||
| <Tr> | ||||||
| <Td colSpan={2}>Total: {rows.length} items</Td> | ||||||
| </Tr> | ||||||
| </Tfoot> | ||||||
| </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.