Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React][NextJs] Handle Sitecore querystring property in Link component #929

Merged
merged 2 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/sitecore-jss-nextjs/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('<Link />', () => {
class: 'my-link',
title: 'My Link',
target: '_blank',
querystring: 'foo=bar',
},
};

Expand All @@ -52,7 +53,7 @@ describe('<Link />', () => {

const link = c.find('a');

expect(link.html()).to.contain(`href="${field.value.href}"`);
expect(link.html()).to.contain(`href="${field.value.href}?${field.value.querystring}"`);
expect(link.html()).to.contain(`class="${field.value.class}"`);
expect(link.html()).to.contain(`title="${field.value.title}"`);
expect(link.html()).to.contain(`target="${field.value.target}"`);
Expand Down
19 changes: 10 additions & 9 deletions packages/sitecore-jss-nextjs/src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,30 @@ export type LinkProps = ReactLinkProps & {

export const Link = (props: LinkProps): JSX.Element => {
const {
field,
editable,
children,
internalLinkMatcher = /^\//g,
showLinkTextWithChildrenPresent,
...htmlLinkProps
} = props;

const value = ((props.field as LinkFieldValue).href
? props.field
: (props.field as LinkField).value) as LinkFieldValue;
const { href } = value;
const isEditing = editable && (props.field as LinkFieldValue).editable;
const value = ((field as LinkFieldValue).href
? field
: (field as LinkField).value) as LinkFieldValue;
const { href, querystring } = value;
const isEditing = editable && (field as LinkFieldValue).editable;

if (href && !isEditing) {
const text =
showLinkTextWithChildrenPresent || !props.children ? value.text || value.href : null;
const text = showLinkTextWithChildrenPresent || !children ? value.text || value.href : null;

// determine if a link is a route or not.
if (internalLinkMatcher.test(href)) {
return (
<NextLink href={href} key="link" locale={false}>
<NextLink href={{ pathname: href, query: querystring }} key="link" locale={false}>
<a title={value.title} target={value.target} className={value.class} {...htmlLinkProps}>
{text}
{props.children}
{children}
</a>
</NextLink>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/sitecore-jss-react/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ describe('<Link />', () => {
class: 'my-link',
title: 'My Link',
target: '_blank',
querystring: 'foo=bar',
},
};
const rendered = mount(<Link field={field} />).find('a');
expect(rendered.html()).to.contain(`href="${field.value.href}"`);
expect(rendered.html()).to.contain(`href="${field.value.href}?${field.value.querystring}"`);
expect(rendered.html()).to.contain(`class="${field.value.class}"`);
expect(rendered.html()).to.contain(`title="${field.value.title}"`);
expect(rendered.html()).to.contain(`target="${field.value.target}"`);
Expand Down
3 changes: 2 additions & 1 deletion packages/sitecore-jss-react/src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface LinkFieldValue {
title?: string;
target?: string;
text?: string;
querystring?: string;
}

export interface LinkField {
Expand Down Expand Up @@ -94,7 +95,7 @@ export const Link: React.SFC<LinkProps> = ({
}

const anchorAttrs: { [attr: string]: unknown } = {
href: link.href,
href: link.querystring ? `${link.href}?${link.querystring}` : link.href,
className: link.class,
title: link.title,
target: link.target,
Expand Down