File size: 4,280 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import React, { useState } from "react";
import {
Link,
LinkField,
Text,
TextField,
useSitecoreContext,
} from "@sitecore-jss/sitecore-jss-nextjs";
interface Fields {
Id: string;
DisplayName: string;
Title: TextField;
NavigationTitle: TextField;
Href: string;
Querystring: string;
Children: Array<Fields>;
Styles: string[];
}
type NavigationProps = {
params?: { [key: string]: string };
fields: Fields;
handleClick: (event?: React.MouseEvent<HTMLElement>) => void;
relativeLevel: number;
};
const getNavigationText = function (
props: NavigationProps,
): JSX.Element | string {
let text;
if (props.fields.NavigationTitle) {
text = <Text field={props.fields.NavigationTitle} />;
} else if (props.fields.Title) {
text = <Text field={props.fields.Title} />;
} else {
text = props.fields.DisplayName;
}
return text;
};
const getLinkTitle = (props: NavigationProps): string | undefined => {
let title;
if (props.fields.NavigationTitle?.value) {
title = props.fields.NavigationTitle.value.toString();
} else if (props.fields.Title?.value) {
title = props.fields.Title.value.toString();
} else {
title = props.fields.DisplayName;
}
return title;
};
const getLinkField = (props: NavigationProps): LinkField => ({
value: {
href: props.fields.Href,
title: getLinkTitle(props),
querystring: props.fields.Querystring,
},
});
const NavigationList = (props: NavigationProps) => {
const { sitecoreContext } = useSitecoreContext();
let children: JSX.Element[] = [];
if (props.fields.Children && props.fields.Children.length) {
children = props.fields.Children.map((element: Fields, index: number) => (
<NavigationList
key={`${index}${element.Id}`}
fields={element}
handleClick={props.handleClick}
relativeLevel={props.relativeLevel + 1}
/>
));
}
return (
<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)}
editable={sitecoreContext.pageEditing}
onClick={props.handleClick}
>
{getNavigationText(props)}
</Link>
</div>
{children.length > 0 ? <ul className="clearfix">{children}</ul> : null}
</li>
);
};
export const Default = (props: NavigationProps): JSX.Element => {
const [isOpenMenu, openMenu] = useState(false);
const { sitecoreContext } = useSitecoreContext();
const styles =
props.params != null
? `${props.params.GridParameters ?? ""} ${
props.params.Styles ?? ""
}`.trimEnd()
: "";
const id = props.params != null ? props.params.RenderingIdentifier : null;
if (!Object.values(props.fields).length) {
return (
<div
className={`component navigation ${styles}`}
id={id ? id : undefined}
>
<div className="component-content">[Navigation]</div>
</div>
);
}
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={(event: React.MouseEvent<HTMLElement>) =>
handleToggleMenu(event, false)
}
relativeLevel={1}
/>
));
return (
<div className={`component navigation ${styles}`} id={id ? id : undefined}>
<label className="menu-mobile-navigate-wrapper">
<input
type="checkbox"
className="menu-mobile-navigate"
checked={isOpenMenu}
onChange={() => handleToggleMenu()}
/>
<div className="menu-humburger" />
<div className="component-content">
<nav>
<ul className="clearfix">{list}</ul>
</nav>
</div>
</label>
</div>
);
};
|