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

[Next.js][React] Link component does not add anchor to the internal links #1226

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion packages/sitecore-jss-nextjs/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('<Link />', () => {
title: 'My Link',
target: '_blank',
querystring: 'foo=bar',
anchor: 'foo',
},
};

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

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

expect(link.html()).to.contain(`href="${field.value.href}?${field.value.querystring}"`);
expect(link.html()).to.contain(
`href="${field.value.href}?${field.value.querystring}#${field.value.anchor}"`
);
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
8 changes: 6 additions & 2 deletions packages/sitecore-jss-nextjs/src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
const value = ((field as LinkFieldValue).href
? field
: (field as LinkField).value) as LinkFieldValue;
const { href, querystring } = value;
const { href, querystring, anchor } = value;
const isEditing = editable && (field as LinkFieldValue).editable;

if (href && !isEditing) {
Expand All @@ -40,7 +40,11 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
// determine if a link is a route or not.
if (internalLinkMatcher.test(href)) {
return (
<NextLink href={{ pathname: href, query: querystring }} key="link" locale={false}>
<NextLink
href={{ pathname: href, query: querystring, hash: anchor }}
key="link"
locale={false}
>
<a
title={value.title}
target={value.target}
Expand Down
5 changes: 4 additions & 1 deletion packages/sitecore-jss-react/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('<Link />', () => {
const field = {
value: {
href: '/lorem',
anchor: 'foo',
text: 'ipsum',
class: 'my-link',
title: 'My Link',
Expand All @@ -72,7 +73,9 @@ describe('<Link />', () => {
},
};
const rendered = mount(<Link field={field} />).find('a');
expect(rendered.html()).to.contain(`href="${field.value.href}?${field.value.querystring}"`);
expect(rendered.html()).to.contain(
`href="${field.value.href}?${field.value.querystring}#${field.value.anchor}"`
);
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
7 changes: 6 additions & 1 deletion packages/sitecore-jss-react/src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface LinkFieldValue {
title?: string;
target?: string;
text?: string;
anchor?: string;
querystring?: string;
}

Expand Down Expand Up @@ -94,8 +95,12 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
return null;
}

const anchor = link.anchor ? `#${link.anchor}` : '';

const anchorAttrs: { [attr: string]: unknown } = {
href: link.querystring ? `${link.href}?${link.querystring}` : link.href,
href: link.querystring
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
? `${link.href}?${link.querystring}${anchor}`
: `${link.href}${anchor}`,
className: link.class,
title: link.title,
target: link.target,
Expand Down