Skip to content

Commit 5b9f68e

Browse files
authored
feat(Table): allow extra content to be appended to th (#12017)
* feat(Table): allow extra content to be appended to th * hide prop * add note for internal use * more prop desc update
1 parent dd5671c commit 5b9f68e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/react-table/src/components/Table/Th.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export interface ThProps
6565
* content, such as a "select all" checkbox or "expand all" toggle.
6666
*/
6767
'aria-label'?: string;
68+
/** @hide Beta prop, internal use only. Additional content to render in the column header, appended to the end of the header content. */
69+
additionalContent?: React.ReactNode;
6870
}
6971

7072
const ThBase: React.FunctionComponent<ThProps> = ({
@@ -94,6 +96,7 @@ const ThBase: React.FunctionComponent<ThProps> = ({
9496
isSubheader = false,
9597
screenReaderText,
9698
'aria-label': ariaLabel,
99+
additionalContent,
97100
...props
98101
}: ThProps) => {
99102
const { variant } = useContext(TableContext);
@@ -241,6 +244,7 @@ const ThBase: React.FunctionComponent<ThProps> = ({
241244
>
242245
{transformedChildren ||
243246
(screenReaderText && <span className={accessibilityStyles.screenReader}>{screenReaderText}</span>)}
247+
{additionalContent && additionalContent}
244248
</MergedComponent>
245249
);
246250

packages/react-table/src/components/Table/__tests__/Th.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,29 @@ test('Renders with screen reader text when screenReaderText is passed in', () =>
2828

2929
expect(screen.getByRole('columnheader')).toHaveTextContent('Test');
3030
});
31+
32+
test('Does not render with additional content by default', () => {
33+
render(<Th />);
34+
35+
expect(screen.getByRole('columnheader')).toBeEmptyDOMElement();
36+
});
37+
38+
test('Render with additional content when additionalContent is passed in', () => {
39+
render(<Th additionalContent={<div>Extra</div>}>Test</Th>);
40+
41+
expect(screen.getByRole('columnheader')).toHaveTextContent('Extra');
42+
});
43+
44+
test('Additional content renders after children when additionalContent is passed in', () => {
45+
render(
46+
<Th additionalContent={<div>Extra</div>}>
47+
<div>Test</div>
48+
</Th>
49+
);
50+
51+
const th = screen.getByRole('columnheader');
52+
const thChildren = th.children;
53+
54+
expect(thChildren.item(0)?.textContent).toEqual('Test');
55+
expect(thChildren.item(1)?.textContent).toEqual('Extra');
56+
});

0 commit comments

Comments
 (0)