Skip to content

Commit

Permalink
[Next.js][React] Link component does not add anchor to the internal l…
Browse files Browse the repository at this point in the history
…inks (#1226)

* feat: add pass anchor field from sitecore to hash prop on nextlink

* chore: reference value in assertion

* chore: add anchor to LinkFieldValue interface

* Add anchor for sitecore-jss-react

* refactoring

Co-authored-by: Chris Manganaro <[email protected]>
  • Loading branch information
illiakovalenko and Chris Manganaro authored Nov 2, 2022
1 parent ece5908 commit 3dcf5a1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
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
6 changes: 5 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,11 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
return null;
}

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

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

0 comments on commit 3dcf5a1

Please sign in to comment.