Skip to content

Commit 766d508

Browse files
Make customData available to interactive atoms (#16612)
* Make `customData` available to interactive atoms * Add tests * Add web test case
1 parent 3053e08 commit 766d508

7 files changed

Lines changed: 79 additions & 0 deletions

File tree

dotcom-rendering/src/components/InteractiveAtom.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type InteractiveAtomType = {
3434
elementCss?: string;
3535
isMainMedia?: boolean;
3636
title: string;
37+
customData?: string;
3738
};
3839

3940
export const InteractiveAtom = ({
@@ -43,6 +44,7 @@ export const InteractiveAtom = ({
4344
elementCss,
4445
isMainMedia,
4546
title,
47+
customData,
4648
}: InteractiveAtomType) => {
4749
const { renderingTarget } = useConfig();
4850

@@ -51,6 +53,7 @@ export const InteractiveAtom = ({
5153
css={[containerStyles, !!isMainMedia && fullHeightStyles]}
5254
data-atom-id={id}
5355
data-atom-type="interactive"
56+
{...(customData ? { 'data-atom-custom-data': customData } : {})}
5457
>
5558
<Island
5659
priority="feature"

dotcom-rendering/src/components/InteractiveLayoutAtom.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ export type InteractiveLayoutAtomType = {
1010
elementHtml?: string;
1111
elementJs?: string;
1212
elementCss?: string;
13+
customData?: string;
1314
};
1415

1516
export const InteractiveLayoutAtom = ({
1617
id,
1718
elementHtml,
1819
elementJs,
1920
elementCss,
21+
customData,
2022
}: InteractiveLayoutAtomType) => (
2123
<div
2224
className="interactive interactive-atom"
2325
css={containerStyles}
2426
data-atom-id={id}
2527
data-atom-type="interactive-layout"
28+
{...(customData ? { 'data-atom-custom-data': customData } : {})}
2629
>
2730
{!!elementCss && (
2831
<style

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,9 @@
25222522
"thumbnail"
25232523
],
25242524
"type": "string"
2525+
},
2526+
"customData": {
2527+
"type": "string"
25252528
}
25262529
},
25272530
"required": [
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { render } from '@testing-library/react';
2+
import { ConfigProvider } from '../components/ConfigContext';
3+
import { InteractiveAtom } from '../components/InteractiveAtom';
4+
import { InteractiveLayoutAtom } from '../components/InteractiveLayoutAtom';
5+
6+
const mockCustomData = JSON.stringify({ key: 'value' });
7+
8+
describe('Interactive atom custom data attributes', () => {
9+
it('should add custom data attribute to InteractiveAtom on web', () => {
10+
const { container } = render(
11+
<ConfigProvider
12+
value={{
13+
renderingTarget: 'Web',
14+
darkModeAvailable: true,
15+
assetOrigin: '/',
16+
editionId: 'UK',
17+
}}
18+
>
19+
<InteractiveAtom
20+
id="test-id"
21+
title="Test Title"
22+
customData={mockCustomData}
23+
/>
24+
</ConfigProvider>,
25+
);
26+
const el = container.querySelector(
27+
`[data-atom-custom-data='${mockCustomData}']`,
28+
);
29+
expect(el).toBeInTheDocument();
30+
});
31+
32+
it('should add custom data attribute to InteractiveAtom on apps', () => {
33+
const { container } = render(
34+
<ConfigProvider
35+
value={{
36+
renderingTarget: 'Apps',
37+
darkModeAvailable: true,
38+
assetOrigin: '/',
39+
editionId: 'UK',
40+
}}
41+
>
42+
<InteractiveAtom
43+
id="test-id"
44+
title="Test Title"
45+
customData={mockCustomData}
46+
/>
47+
</ConfigProvider>,
48+
);
49+
const el = container.querySelector(
50+
`[data-atom-custom-data='${mockCustomData}']`,
51+
);
52+
expect(el).toBeInTheDocument();
53+
});
54+
55+
it('should add custom data attribute to InteractiveLayoutAtom', () => {
56+
const { container } = render(
57+
<InteractiveLayoutAtom id="test-id" customData={mockCustomData} />,
58+
);
59+
const el = container.querySelector(
60+
`[data-atom-custom-data='${mockCustomData}']`,
61+
);
62+
expect(el).toBeInTheDocument();
63+
});
64+
});

dotcom-rendering/src/lib/renderElement.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ export const renderElement = ({
436436
elementHtml={element.html}
437437
elementJs={element.js}
438438
elementCss={element.css}
439+
customData={element.customData}
439440
/>
440441
);
441442
}
@@ -447,6 +448,7 @@ export const renderElement = ({
447448
elementJs={element.js}
448449
elementCss={element.css}
449450
title={element.title}
451+
customData={element.customData}
450452
/>
451453
);
452454
case 'model.dotcomrendering.pageElements.InteractiveBlockElement': {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,9 @@
19991999
"thumbnail"
20002000
],
20012001
"type": "string"
2002+
},
2003+
"customData": {
2004+
"type": "string"
20022005
}
20032006
},
20042007
"required": [

dotcom-rendering/src/types/content.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ export interface InteractiveAtomBlockElement {
347347
css?: string;
348348
placeholderUrl?: string;
349349
role?: RoleType | 'fullWidth';
350+
customData?: string;
350351
}
351352

352353
// Can't guarantee anything in interactiveBlockElement :shrug:

0 commit comments

Comments
 (0)