Skip to content

Commit f849a9b

Browse files
RmatkovskyRuslan Matkovskyi
authored and
Ruslan Matkovskyi
committed
[templates/nextjs-sxa] SXA components has been reworked with supporting metadata approach
1 parent cdb899a commit f849a9b

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Our versioning strategy is as follows:
1212
## Unreleased
1313

1414
### 🎉 New Features & Improvements
15-
* `[sitecore-jss-react]` `[sitecore-jss-nextjs]` Introduce FieldMetadata component and functionality to render it when metadata field property is provided in the field's layout data. In such case the field component is wrapped with metadata markup to enable chromes hydration when editing in pages. Ability to render metadata has been added to the field rendering components for react and nextjs. ([#1773](https://github.com/Sitecore/jss/pull/1773))
15+
* `[sitecore-jss-react]` `[sitecore-jss-nextjs]` Introduce FieldMetadata component and functionality to render it when metadata field property is provided in the field's layout data. In such case the field component is wrapped with metadata markup to enable chromes hydration when editing in pages. Ability to render metadata has been added to the field rendering components for react and nextjs. ([#1773](https://github.com/Sitecore/jss/pull/1773))
16+
* `[templates/nextjs-sxa]` A new approach `metadata` has been added in SXA components. ([#1788](https://github.com/Sitecore/jss/pull/1788))
1617

1718
### 🛠 Breaking Changes
1819

packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/Image.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '@sitecore-jss/sitecore-jss-nextjs';
1111

1212
interface Fields {
13-
Image: ImageField;
13+
Image: ImageField & { metadata?: { [key: string]: unknown } };
1414
ImageCaption: Field<string>;
1515
TargetUrl: LinkField;
1616
}
@@ -31,19 +31,22 @@ const ImageDefault = (props: ImageProps): JSX.Element => (
3131
export const Banner = (props: ImageProps): JSX.Element => {
3232
const { sitecoreContext } = useSitecoreContext();
3333
const isPageEditing = sitecoreContext.pageEditing;
34+
const isMetadataMode = sitecoreContext?.editMode === 'metadata';
3435
const classHeroBannerEmpty =
3536
isPageEditing && props.fields?.Image?.value?.class === 'scEmptyImage'
3637
? 'hero-banner-empty'
3738
: '';
3839
const backgroundStyle = (props?.fields?.Image?.value?.src && {
3940
backgroundImage: `url('${props.fields.Image.value.src}')`,
4041
}) as CSSProperties;
41-
const modifyImageProps = {
42-
...props.fields.Image,
43-
editable: props?.fields?.Image?.editable
44-
?.replace(`width="${props?.fields?.Image?.value?.width}"`, 'width="100%"')
45-
.replace(`height="${props?.fields?.Image?.value?.height}"`, 'height="100%"'),
46-
};
42+
const modifyImageProps = !isMetadataMode
43+
? {
44+
...props.fields.Image,
45+
editable: props?.fields?.Image?.editable
46+
?.replace(`width="${props?.fields?.Image?.value?.width}"`, 'width="100%"')
47+
.replace(`height="${props?.fields?.Image?.value?.height}"`, 'height="100%"'),
48+
}
49+
: { ...props.fields.Image };
4750
const id = props.params.RenderingIdentifier;
4851

4952
return (

packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/Title.tsx

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React from 'react';
21
import {
32
Link,
4-
Text,
5-
useSitecoreContext,
63
LinkField,
4+
Text,
75
TextField,
6+
useSitecoreContext,
87
} from '@sitecore-jss/sitecore-jss-nextjs';
8+
import React from 'react';
99

1010
interface Fields {
1111
data: {
@@ -17,7 +17,8 @@ interface Fields {
1717
field: {
1818
jsonValue: {
1919
value: string;
20-
editable: string;
20+
editable?: string;
21+
metadata?: object;
2122
};
2223
};
2324
};
@@ -29,7 +30,8 @@ interface Fields {
2930
field: {
3031
jsonValue: {
3132
value: string;
32-
editable: string;
33+
editable?: string;
34+
metadata?: object;
3335
};
3436
};
3537
};
@@ -61,17 +63,22 @@ const ComponentContent = (props: ComponentContentProps) => {
6163
export const Default = (props: TitleProps): JSX.Element => {
6264
const datasource = props.fields?.data?.datasource || props.fields?.data?.contextItem;
6365
const { sitecoreContext } = useSitecoreContext();
66+
const isMetadataMode = sitecoreContext?.editMode === 'metadata';
6467

6568
const text: TextField = {
6669
value: datasource?.field?.jsonValue?.value,
67-
editable: datasource?.field?.jsonValue?.editable,
70+
...(isMetadataMode
71+
? { metadata: datasource?.field?.jsonValue?.metadata }
72+
: { editable: datasource?.field?.jsonValue?.editable }),
6873
};
6974
const link: LinkField = {
7075
value: {
7176
href: datasource?.url?.path,
7277
title: datasource?.field?.jsonValue?.value,
73-
editable: true,
7478
},
79+
...(isMetadataMode
80+
? { metadata: datasource?.field?.jsonValue?.metadata }
81+
: { editable: datasource?.field?.jsonValue?.editable }),
7582
};
7683
if (sitecoreContext.pageState !== 'normal') {
7784
link.value.querystring = `sc_site=${datasource?.url?.siteName}`;
@@ -84,10 +91,10 @@ export const Default = (props: TitleProps): JSX.Element => {
8491
return (
8592
<ComponentContent styles={props.params.styles} id={props.params.RenderingIdentifier}>
8693
<>
87-
{sitecoreContext.pageState === 'edit' ? (
94+
{sitecoreContext.pageEditing ? (
8895
<Text field={text} />
8996
) : (
90-
<Link field={link}>
97+
<Link field={link} editable={true}>
9198
<Text field={text} />
9299
</Link>
93100
)}

0 commit comments

Comments
 (0)