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] Fix double hash in anchor links #1375

Merged
merged 2 commits into from
Mar 13, 2023
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
16 changes: 16 additions & 0 deletions packages/sitecore-jss-nextjs/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ describe('<Link />', () => {
expect(c.find(ReactLink).length).to.equal(0);
});

it('should not add extra hash when linktype is anchor', () => {
const field = {
linktype: 'anchor',
href: '#anchor',
text: 'anchor link',
anchor: 'anchor',
};
const rendered = mount(
<Page>
<Link field={field} />
</Page>
).find('a');
expect(rendered.html()).to.contain(`href="${field.href}"`);
expect(rendered.text()).to.equal(field.text);
});

it('should render NextLink using internalLinkMatcher', () => {
const field = {
value: {
Expand Down
13 changes: 13 additions & 0 deletions packages/sitecore-jss-react/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mount } from 'enzyme';

import { Link, LinkField } from './Link';
import { generalLinkField as eeLinkData } from '../test-data/ee-data';
import { it } from 'node:test';

describe('<Link />', () => {
it('should render nothing with missing field', () => {
Expand Down Expand Up @@ -51,6 +52,18 @@ describe('<Link />', () => {
expect(rendered.html()).to.contain(field.text);
});

it('should not add extra hash when linktype is anchor', () => {
const field = {
linktype: 'anchor',
href: '#anchor',
text: 'anchor link',
anchor: 'anchor',
};
const rendered = mount(<Link field={field} />).find('a');
expect(rendered.html()).to.contain(`href="${field.href}"`);
expect(rendered.text()).to.equal(field.text);
});

it('should render ee HTML', () => {
const field = {
editableFirstPart: eeLinkData,
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 @@ -12,6 +12,7 @@ export interface LinkFieldValue {
text?: string;
anchor?: string;
querystring?: string;
linktype?: string;
}

export interface LinkField {
Expand Down Expand Up @@ -95,7 +96,7 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
return null;
}

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

const anchorAttrs: { [attr: string]: unknown } = {
Expand Down