Skip to content
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
10 changes: 8 additions & 2 deletions src/routing/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface LinkProperties extends VNodeProperties {
const factory = create({ injector }).properties<LinkProperties>();

export const Link = factory(function Link({ middleware: { injector }, properties, children }) {
let { routerKey = 'router', to, isOutlet = true, target, params = {}, onClick, ...props } = properties();
let { routerKey = 'router', to, isOutlet = true, params = {}, onClick, ...props } = properties();
const router = injector.get<Router>(routerKey);
let href: string | undefined = to;

Expand All @@ -29,7 +29,13 @@ export const Link = factory(function Link({ middleware: { injector }, properties
const onclick = (event: MouseEvent) => {
onClick && onClick(event);

if (!event.defaultPrevented && event.button === 0 && !event.metaKey && !event.ctrlKey && !target) {
if (
!event.defaultPrevented &&
event.button === 0 &&
!event.metaKey &&
!event.ctrlKey &&
!linkProps.target
) {
if (!has('build-serve') || !has('build-time-rendered')) {
event.preventDefault();
href !== undefined && router.setPath(href);
Expand Down
13 changes: 12 additions & 1 deletion tests/routing/unit/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('Link', () => {
const r = renderer(() => w(Link, { to: 'foo', target: '_blank' }), {
middleware: [[getRegistry, mockGetRegistry]]
});
const template = assertion(() => v(WrappedAnchor.tag, { href: 'foo', onclick: noop }));
const template = assertion(() => v(WrappedAnchor.tag, { href: 'foo', onclick: noop, target: '_blank' }));
r.expect(template);
r.property(WrappedAnchor, 'onclick', createMockEvent());
r.expect(template);
Expand Down Expand Up @@ -190,4 +190,15 @@ describe('Link', () => {
// nothing to see here
}
});

it('does pass target through to element', () => {
const WrappedAnchor = wrap('a');
const r = renderer(() => w(Link, { to: '#foo/static', isOutlet: false, target: '_blank' }), {
middleware: [[getRegistry, mockGetRegistry]]
});
const template = assertion(() =>
v(WrappedAnchor.tag, { href: '#foo/static', onclick: noop, target: '_blank' })
);
r.expect(template);
});
});