Skip to content

Commit

Permalink
#539620: [SXA] fixed navigation link in editing mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sc-ruslanmatkovskyi committed Jul 20, 2022
1 parent 838bc31 commit 0260b6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@ const Layout = ({ layoutData }: LayoutProps): JSX.Element => {
{/* root placeholder for the app, which we add components to using route data */}
<div id="wrapper" className={mainClassPageEditing}>
<header>
<div id="header">
{route && <Placeholder name="headless-header" rendering={route} />}
</div>
<div id="header">{route && <Placeholder name="headless-header" rendering={route} />}</div>
</header>
<main>
<div id="content">
{route && <Placeholder name="headless-main" rendering={route} />}
</div>
<div id="content">{route && <Placeholder name="headless-main" rendering={route} />}</div>
</main>
<footer>
<div id="footer">
{route && <Placeholder name="headless-footer" rendering={route} />}
</div>
<div id="footer">{route && <Placeholder name="headless-footer" rendering={route} />}</div>
</footer>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React, { useState } from 'react';
import { Link, LinkField, Text, TextField } from '@sitecore-jss/sitecore-jss-nextjs';
import {
Link,
LinkField,
Text,
TextField,
useSitecoreContext,
} from '@sitecore-jss/sitecore-jss-nextjs';

interface Fields {
Id: string;
Expand All @@ -15,7 +21,7 @@ interface Fields {
type NavigationProps = {
params?: { [key: string]: string };
fields: Fields;
handleClick: () => void;
handleClick: (event?: React.MouseEvent<HTMLElement>) => void;
relativeLevel: number;
};

Expand All @@ -36,13 +42,14 @@ const getNavigationText = function (props: NavigationProps): JSX.Element | strin
const getLinkField = (props: NavigationProps): LinkField => ({
value: {
href: props.fields.Href,
title: props.fields.DisplayName,
title: getLinkTitle(props),
querystring: props.fields.Querystring,
},
});

export const Default = (props: NavigationProps): JSX.Element => {
const [isOpenMenu, openMenu] = useState(false);
const { sitecoreContext } = useSitecoreContext();

if (!Object.values(props.fields).length) {
return (
Expand All @@ -52,20 +59,25 @@ export const Default = (props: NavigationProps): JSX.Element => {
);
}

const handleToggleMenu = (flag?: boolean): void => {
const handleToggleMenu = (event?: React.MouseEvent<HTMLElement>, flag?: boolean): void => {
if (event && sitecoreContext?.pageEditing) {
event.preventDefault();
}

if (flag !== undefined) {
return openMenu(flag);
}

openMenu(!isOpenMenu);
};

const list = Object.values(props.fields)
.filter((element) => element)
.map((element: Fields, key: number) => (
<NavigationList
key={`${key}${element.Id}`}
fields={element}
handleClick={() => handleToggleMenu(false)}
handleClick={(event: React.MouseEvent<HTMLElement>) => handleToggleMenu(event, false)}
relativeLevel={1}
/>
));
Expand Down Expand Up @@ -93,14 +105,7 @@ export const Default = (props: NavigationProps): JSX.Element => {
};

const NavigationList = (props: NavigationProps) => {
let title;
if (props.fields.NavigationTitle) {
title = props.fields.NavigationTitle.value?.toString();
} else if (props.fields.Title) {
title = props.fields.Title.value?.toString();
} else {
title = props.fields.DisplayName;
}
const { sitecoreContext } = useSitecoreContext();

let children: JSX.Element[] = [];
if (props.fields.Children && props.fields.Children.length) {
Expand All @@ -115,9 +120,17 @@ const NavigationList = (props: NavigationProps) => {
}

return (
<li className={props.fields.Styles.concat('rel-level' + props.relativeLevel).join(' ')} key={props.fields.Id}>
<li
className={props.fields.Styles.concat('rel-level' + props.relativeLevel).join(' ')}
key={props.fields.Id}
tabIndex={0}
>
<div className="navigation-title">
<Link field={getLinkField(props)} title={title} onClick={props.handleClick}>
<Link
field={getLinkField(props)}
editable={sitecoreContext.pageEditing}
onClick={props.handleClick}
>
{getNavigationText(props)}
</Link>
</div>
Expand All @@ -126,3 +139,15 @@ const NavigationList = (props: NavigationProps) => {
);
};

const getLinkTitle = (props: NavigationProps): string | undefined => {
let title;
if (props.fields.NavigationTitle) {
title = props.fields.NavigationTitle.value?.toString();
} else if (props.fields.Title) {
title = props.fields.Title.value?.toString();
} else {
title = props.fields.DisplayName;
}

return title;
};

0 comments on commit 0260b6b

Please sign in to comment.