-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Link.ts
68 lines (56 loc) · 2.06 KB
/
Link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createVNode, Inferno, InfernoMouseEvent, linkEvent, VNode } from 'inferno';
import { ChildFlags, VNodeFlags } from 'inferno-vnode-flags';
import { invariant } from './utils';
import { combineFrom, isString } from 'inferno-shared';
import type { Location } from 'history';
import { parsePath } from 'history';
import { splitLocation, normalizeToLocation } from "./locationUtils";
const isModifiedEvent = (event: InfernoMouseEvent<any>): boolean => Boolean(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
export interface ILinkProps {
children?: any;
onClick?: any;
target?: string;
className?: string;
replace?: boolean;
to?: string | Location;
innerRef?: any;
}
function handleClick({ props, context }, event: InfernoMouseEvent<any>) {
if (props.onClick) {
props.onClick(event);
}
if (
!event.defaultPrevented && // onClick prevented default
event.button === 0 && // ignore everything but left clicks
!props.target && // let browser handle "target=_blank" etc.
!isModifiedEvent(event) // ignore clicks with modifier keys
) {
event.preventDefault();
const { history } = context.router;
const { replace = false, to: toPropIn } = props;
const { to, state } = splitLocation(normalizeToLocation(toPropIn));
if (replace) {
history.replace(to, state);
} else {
history.push(to, state);
}
}
}
/**
* The public API for rendering a history-aware <a>.
*/
export function Link(props: ILinkProps & Inferno.LinkHTMLAttributes<HTMLLinkElement>, context): VNode {
const { replace, children, className, to = '', innerRef, ...rest } = props;
invariant(context.router, 'You should not use <Link> outside a <Router>');
const href = context.router.history.createHref(isString(to) ? parsePath(to) : to);
const newProps: any = combineFrom(rest, null);
newProps.href = href;
newProps.onClick = linkEvent(
{
context,
props
},
handleClick
);
return createVNode(VNodeFlags.HtmlElement, 'a', className, children, ChildFlags.UnknownChildren, newProps, null, innerRef);
}