Skip to content

Commit a63fb57

Browse files
authored
feat: change header counter type to ReactNode (slot) to allow arbitrary content. (#4018)
1 parent 6012a00 commit a63fb57

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

pages/header/permutations.page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
import React from 'react';
44

5-
import { Button, Link } from '~components';
5+
import { Button, Link, Spinner } from '~components';
66
import Header, { HeaderProps } from '~components/header';
77

88
import createPermutations from '../utils/permutations';
@@ -22,7 +22,7 @@ const permutations = createPermutations<HeaderProps>([
2222
'A description text with some content to simulate a text shown below the title of this header.',
2323
null,
2424
],
25-
counter: ['(1/10)', undefined],
25+
counter: ['(1/10)', <Spinner key="spinner" />, undefined],
2626
actions: [
2727
<Button key="button" variant="primary">
2828
Create Resource
@@ -36,7 +36,7 @@ export default function HeaderPermutations() {
3636
return (
3737
<article>
3838
<h1>Header - Permutations</h1>
39-
<ScreenshotArea>
39+
<ScreenshotArea disableAnimations={true}>
4040
<PermutationsView permutations={permutations} render={props => <Header {...props}>{props.variant}</Header>} />
4141
</ScreenshotArea>
4242
</article>

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13127,13 +13127,6 @@ exports[`Components definition for header matches the snapshot: header 1`] = `
1312713127
"optional": true,
1312813128
"type": "string",
1312913129
},
13130-
{
13131-
"description": "Specifies secondary text that's displayed to the right of the heading title. This is commonly used
13132-
to display resource counters in table and cards components.",
13133-
"name": "counter",
13134-
"optional": true,
13135-
"type": "string",
13136-
},
1313713130
{
1313813131
"description": "Overrides the default [HTML heading tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements)
1313913132
provided by the variant. Using this property does not change the visual appearance of the component.",
@@ -13197,6 +13190,12 @@ HTML heading tag based on the specified \`variant\` or \`headingTagOverride\`.",
1319713190
"isDefault": true,
1319813191
"name": "children",
1319913192
},
13193+
{
13194+
"description": "Specifies secondary content that's displayed to the right of the heading title. This is commonly used
13195+
to display resource counters in table and cards components.",
13196+
"isDefault": false,
13197+
"name": "counter",
13198+
},
1320013199
{
1320113200
"description": "Supplementary text below the heading.",
1320213201
"isDefault": false,

src/header/__tests__/header.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Container from '../../../lib/components/container';
77
import ExpandableSection from '../../../lib/components/expandable-section';
88
import Header from '../../../lib/components/header';
99
import { DATA_ATTR_FUNNEL_KEY, FUNNEL_KEY_SUBSTEP_NAME } from '../../../lib/components/internal/analytics/selectors';
10+
import Spinner from '../../../lib/components/spinner';
1011
import createWrapper from '../../../lib/components/test-utils/dom';
1112

1213
import styles from '../../../lib/components/header/styles.css.js';
@@ -41,6 +42,12 @@ test('renders everything provided', () => {
4142
expect(wrapper.findActions()!.find('button')!.getElement()).toHaveTextContent('Click me');
4243
});
4344

45+
test('renders arbitrary counter content', () => {
46+
const { wrapper } = renderHeader(<Header counter={<Spinner />} />);
47+
expect(wrapper.findCounter()).not.toBeNull();
48+
expect(wrapper.findCounter()!.findSpinner()).not.toBeNull();
49+
});
50+
4451
test('renders h2 tag and variant by default', () => {
4552
const { wrapper } = renderHeader(<Header>title</Header>);
4653
expect(wrapper.find('h2')!.getElement()).toHaveTextContent('title');

src/header/analytics/use-table-integration.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { useEffect, useMemo } from 'react';
3+
import React, { useEffect, useMemo } from 'react';
44

55
import { parseCountValue } from '../../internal/analytics/utils/parse-count-text';
66
import { useTableComponentsContext } from '../../internal/context/table-component-context';
@@ -11,9 +11,15 @@ import { useTableComponentsContext } from '../../internal/context/table-componen
1111
* The extracted count value is automatically synchronized with the table header
1212
* component through the table context, updating the countText property.
1313
*/
14-
export const useTableIntegration = (countText: string | undefined) => {
14+
export const useTableIntegration = (countText: React.ReactNode | undefined) => {
1515
const tableComponentContext = useTableComponentsContext();
16-
const countValue = useMemo(() => parseCountValue(countText), [countText]);
16+
const countValue = useMemo(() => {
17+
if (typeof countText === 'string') {
18+
return parseCountValue(countText);
19+
} else {
20+
return undefined;
21+
}
22+
}, [countText]);
1723

1824
useEffect(() => {
1925
if (tableComponentContext?.headerRef?.current && countValue !== undefined) {

src/header/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export interface HeaderProps extends BaseComponentProps {
3434
*/
3535
actions?: React.ReactNode;
3636
/**
37-
* Specifies secondary text that's displayed to the right of the heading title. This is commonly used
37+
* Specifies secondary content that's displayed to the right of the heading title. This is commonly used
3838
* to display resource counters in table and cards components.
3939
*/
40-
counter?: string;
40+
counter?: React.ReactNode;
4141
/**
4242
* Area next to the heading to display an Info link.
4343
*/

0 commit comments

Comments
 (0)