Skip to content

Commit 4b1f761

Browse files
simplify subheading in the product element. This also fixes a react warning about unique keys in the subheadings in the productElement.
1 parent f4af771 commit 4b1f761

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

dotcom-rendering/src/components/ProductElement.tsx

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import { css } from '@emotion/react';
22
import { from } from '@guardian/source/foundations';
33
import type { ReactNode } from 'react';
44
import type { ArticleFormat } from '../lib/articleFormat';
5-
import { parseHtml } from '../lib/domUtils';
65
import type { NestedArticleElement } from '../lib/renderElement';
76
import type { ProductBlockElement } from '../types/content';
87
import { ProductCardInline } from './ProductCardInline';
98
import { ProductCardLeftCol } from './ProductCardLeftCol';
10-
import { buildElementTree } from './SubheadingBlockComponent';
9+
import { Subheading } from './Subheading';
1110

1211
const contentContainer = css`
1312
position: relative;
@@ -86,23 +85,10 @@ const Content = ({
8685
const showLeftCol =
8786
product.displayType === 'InlineWithProductCard' &&
8887
shouldShowLeftColCard;
89-
const subheadingHtml = parseHtml(
90-
`<h2 id="${product.h2Id ?? product.elementId}">${
91-
product.primaryHeadingHtml
92-
? `${product.primaryHeadingHtml}<br />`
93-
: ''
94-
} ${product.secondaryHeadingHtml || ''}</h2>`,
95-
);
9688

97-
const isSubheading = subheadingHtml.textContent
98-
? subheadingHtml.textContent.trim().length > 0
99-
: false;
10089
return (
10190
<div>
102-
{isSubheading &&
103-
Array.from(subheadingHtml.childNodes).map(
104-
buildElementTree(format),
105-
)}
91+
<ProductSubheading product={product} format={format} />
10692
<div css={contentContainer} data-spacefinder-role="nested">
10793
{showLeftCol && (
10894
<LeftColProductCardContainer>
@@ -131,3 +117,32 @@ const Content = ({
131117
</div>
132118
);
133119
};
120+
121+
const ProductSubheading = ({
122+
product,
123+
format,
124+
}: {
125+
product: ProductBlockElement;
126+
format: ArticleFormat;
127+
}) => {
128+
const subheadingHtml = `${
129+
product.primaryHeadingHtml ? `${product.primaryHeadingHtml}<br />` : ''
130+
} ${product.secondaryHeadingHtml || ''}`;
131+
132+
const isSubheading =
133+
!!product.primaryHeadingText && !!product.secondaryHeadingText;
134+
135+
if (!isSubheading) {
136+
return null;
137+
}
138+
139+
return (
140+
<Subheading
141+
id={`${product.h2Id ?? product.elementId}`}
142+
format={format}
143+
topPadding={true}
144+
>
145+
<span dangerouslySetInnerHTML={{ __html: subheadingHtml }}></span>
146+
</Subheading>
147+
);
148+
};

dotcom-rendering/src/frontend/schemas/feArticle.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,6 +4477,9 @@
44774477
"secondaryHeadingHtml": {
44784478
"type": "string"
44794479
},
4480+
"secondaryHeadingText": {
4481+
"type": "string"
4482+
},
44804483
"primaryHeadingHtml": {
44814484
"type": "string"
44824485
},

dotcom-rendering/src/model/block-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,6 +3965,9 @@
39653965
"secondaryHeadingHtml": {
39663966
"type": "string"
39673967
},
3968+
"secondaryHeadingText": {
3969+
"type": "string"
3970+
},
39683971
"primaryHeadingHtml": {
39693972
"type": "string"
39703973
},

dotcom-rendering/src/model/enhanceProductElement.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const enhanceProductBlockElement = (
1515
content: elementsEnhancer(element.content),
1616
lowestPrice: getLowestPrice(element.productCtas),
1717
primaryHeadingText: extractHeadingText(element.primaryHeadingHtml),
18+
secondaryHeadingText: extractHeadingText(element.secondaryHeadingHtml),
1819
});
1920

2021
/**

dotcom-rendering/src/types/content.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ export interface ProductBlockElement {
479479
productName: string;
480480
image?: ProductImage;
481481
secondaryHeadingHtml: string;
482+
secondaryHeadingText?: string;
482483
primaryHeadingHtml: string;
483484
primaryHeadingText?: string;
484485
customAttributes: ProductCustomAttribute[];

0 commit comments

Comments
 (0)