Skip to content

Commit c45ac11

Browse files
committed
[templates/nextjs-sxa] SXA components has been reworked with supporting metadata approach
1 parent cdb899a commit c45ac11

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

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/Navigation.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React, { useState } from 'react';
21
import {
32
Link,
43
LinkField,
54
Text,
65
TextField,
76
useSitecoreContext,
87
} from '@sitecore-jss/sitecore-jss-nextjs';
8+
import React, { useState } from 'react';
99

1010
interface Fields {
1111
Id: string;
@@ -108,13 +108,14 @@ export const Default = (props: NavigationProps): JSX.Element => {
108108
};
109109

110110
const NavigationList = (props: NavigationProps) => {
111+
let children: JSX.Element[] = [];
111112
const { sitecoreContext } = useSitecoreContext();
112113
const [active, setActive] = useState(false);
113114
const classNameList = `${props.fields.Styles.concat('rel-level' + props.relativeLevel).join(
114115
' '
115116
)}`;
117+
const isMetadataMode = sitecoreContext?.editMode === 'metadata';
116118

117-
let children: JSX.Element[] = [];
118119
if (props.fields.Children && props.fields.Children.length) {
119120
children = props.fields.Children.map((element: Fields, index: number) => (
120121
<NavigationList
@@ -134,8 +135,8 @@ const NavigationList = (props: NavigationProps) => {
134135
>
135136
<Link
136137
field={getLinkField(props)}
137-
editable={sitecoreContext.pageEditing}
138138
onClick={props.handleClick}
139+
{...(!isMetadataMode ? { editable: sitecoreContext.pageEditing } : '')}
139140
>
140141
{getNavigationText(props)}
141142
</Link>

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

+13-7
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,7 @@ interface Fields {
1717
field: {
1818
jsonValue: {
1919
value: string;
20-
editable: string;
20+
editable?: string;
2121
};
2222
};
2323
};
@@ -29,7 +29,8 @@ interface Fields {
2929
field: {
3030
jsonValue: {
3131
value: string;
32-
editable: string;
32+
editable?: string;
33+
metadata?: object;
3334
};
3435
};
3536
};
@@ -61,16 +62,21 @@ const ComponentContent = (props: ComponentContentProps) => {
6162
export const Default = (props: TitleProps): JSX.Element => {
6263
const datasource = props.fields?.data?.datasource || props.fields?.data?.contextItem;
6364
const { sitecoreContext } = useSitecoreContext();
65+
const isMetadataMode = sitecoreContext?.editMode === 'metadata';
6466

6567
const text: TextField = {
6668
value: datasource?.field?.jsonValue?.value,
67-
editable: datasource?.field?.jsonValue?.editable,
69+
...(isMetadataMode
70+
? { metadata: props?.fields?.data?.contextItem?.field?.jsonValue?.metadata }
71+
: { editable: datasource?.field?.jsonValue?.editable }),
6872
};
6973
const link: LinkField = {
7074
value: {
7175
href: datasource?.url?.path,
7276
title: datasource?.field?.jsonValue?.value,
73-
editable: true,
77+
...(isMetadataMode
78+
? { metadata: props?.fields?.data?.contextItem?.field?.jsonValue?.metadata }
79+
: { editable: true }),
7480
},
7581
};
7682
if (sitecoreContext.pageState !== 'normal') {

0 commit comments

Comments
 (0)