From a4f74f22c2588ecb0389ae11181776343a47264a Mon Sep 17 00:00:00 2001 From: Chris Manganaro Date: Fri, 28 Oct 2022 11:15:18 +0800 Subject: [PATCH 1/5] feat: add pass anchor field from sitecore to hash prop on nextlink --- packages/sitecore-jss-nextjs/src/components/Link.test.tsx | 3 ++- packages/sitecore-jss-nextjs/src/components/Link.tsx | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/sitecore-jss-nextjs/src/components/Link.test.tsx b/packages/sitecore-jss-nextjs/src/components/Link.test.tsx index 42017b3524..1072f88f1f 100644 --- a/packages/sitecore-jss-nextjs/src/components/Link.test.tsx +++ b/packages/sitecore-jss-nextjs/src/components/Link.test.tsx @@ -42,6 +42,7 @@ describe('', () => { title: 'My Link', target: '_blank', querystring: 'foo=bar', + anchor: 'foo', }, }; @@ -53,7 +54,7 @@ describe('', () => { 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}#foo"`); 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}"`); diff --git a/packages/sitecore-jss-nextjs/src/components/Link.tsx b/packages/sitecore-jss-nextjs/src/components/Link.tsx index 536ff34857..0d2e6b2abf 100644 --- a/packages/sitecore-jss-nextjs/src/components/Link.tsx +++ b/packages/sitecore-jss-nextjs/src/components/Link.tsx @@ -31,7 +31,7 @@ export const Link = forwardRef( 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) { @@ -40,7 +40,11 @@ export const Link = forwardRef( // determine if a link is a route or not. if (internalLinkMatcher.test(href)) { return ( - + Date: Fri, 28 Oct 2022 11:19:26 +0800 Subject: [PATCH 2/5] chore: reference value in assertion --- packages/sitecore-jss-nextjs/src/components/Link.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/sitecore-jss-nextjs/src/components/Link.test.tsx b/packages/sitecore-jss-nextjs/src/components/Link.test.tsx index 1072f88f1f..1383af08c1 100644 --- a/packages/sitecore-jss-nextjs/src/components/Link.test.tsx +++ b/packages/sitecore-jss-nextjs/src/components/Link.test.tsx @@ -54,7 +54,9 @@ describe('', () => { const link = c.find('a'); - expect(link.html()).to.contain(`href="${field.value.href}?${field.value.querystring}#foo"`); + 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}"`); From c7302528914dd60f0db8ce78dfd8f493df1c7e57 Mon Sep 17 00:00:00 2001 From: Chris Manganaro Date: Fri, 28 Oct 2022 11:39:12 +0800 Subject: [PATCH 3/5] chore: add anchor to LinkFieldValue interface --- packages/sitecore-jss-nextjs/src/components/Link.tsx | 2 +- packages/sitecore-jss-react/src/components/Link.tsx | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sitecore-jss-nextjs/src/components/Link.tsx b/packages/sitecore-jss-nextjs/src/components/Link.tsx index 0d2e6b2abf..4c79c31e6a 100644 --- a/packages/sitecore-jss-nextjs/src/components/Link.tsx +++ b/packages/sitecore-jss-nextjs/src/components/Link.tsx @@ -41,7 +41,7 @@ export const Link = forwardRef( if (internalLinkMatcher.test(href)) { return ( diff --git a/packages/sitecore-jss-react/src/components/Link.tsx b/packages/sitecore-jss-react/src/components/Link.tsx index 5d7fb24543..a2a9a1396f 100644 --- a/packages/sitecore-jss-react/src/components/Link.tsx +++ b/packages/sitecore-jss-react/src/components/Link.tsx @@ -10,6 +10,7 @@ export interface LinkFieldValue { title?: string; target?: string; text?: string; + anchor?: string; querystring?: string; } From d54ec8d7f4a456497197aed52715f637fc6b61bb Mon Sep 17 00:00:00 2001 From: illiakovalenko Date: Wed, 2 Nov 2022 10:07:09 +0200 Subject: [PATCH 4/5] Add anchor for sitecore-jss-react --- packages/sitecore-jss-react/src/components/Link.test.tsx | 5 ++++- packages/sitecore-jss-react/src/components/Link.tsx | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/sitecore-jss-react/src/components/Link.test.tsx b/packages/sitecore-jss-react/src/components/Link.test.tsx index cbe6a00e08..1468c7f22e 100644 --- a/packages/sitecore-jss-react/src/components/Link.test.tsx +++ b/packages/sitecore-jss-react/src/components/Link.test.tsx @@ -64,6 +64,7 @@ describe('', () => { const field = { value: { href: '/lorem', + anchor: 'foo', text: 'ipsum', class: 'my-link', title: 'My Link', @@ -72,7 +73,9 @@ describe('', () => { }, }; const rendered = mount().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}"`); diff --git a/packages/sitecore-jss-react/src/components/Link.tsx b/packages/sitecore-jss-react/src/components/Link.tsx index a2a9a1396f..9347ab2642 100644 --- a/packages/sitecore-jss-react/src/components/Link.tsx +++ b/packages/sitecore-jss-react/src/components/Link.tsx @@ -95,8 +95,12 @@ export const Link = forwardRef( 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 + ? `${link.href}?${link.querystring}${anchor}` + : `${link.href}${anchor}`, className: link.class, title: link.title, target: link.target, From b6cae0d980fb5253e019089e65a6b436f531161a Mon Sep 17 00:00:00 2001 From: illiakovalenko Date: Wed, 2 Nov 2022 16:08:41 +0200 Subject: [PATCH 5/5] refactoring --- packages/sitecore-jss-react/src/components/Link.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/sitecore-jss-react/src/components/Link.tsx b/packages/sitecore-jss-react/src/components/Link.tsx index 9347ab2642..a0927b1cbe 100644 --- a/packages/sitecore-jss-react/src/components/Link.tsx +++ b/packages/sitecore-jss-react/src/components/Link.tsx @@ -96,11 +96,10 @@ export const Link = forwardRef( } const anchor = link.anchor ? `#${link.anchor}` : ''; + const querystring = link.querystring ? `?${link.querystring}` : ''; const anchorAttrs: { [attr: string]: unknown } = { - href: link.querystring - ? `${link.href}?${link.querystring}${anchor}` - : `${link.href}${anchor}`, + href: `${link.href}${querystring}${anchor}`, className: link.class, title: link.title, target: link.target,