-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteleport.js
45 lines (37 loc) · 974 Bytes
/
teleport.js
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
import React, { Component, PropTypes } from 'react'
export default class Teleport extends Component {
onClick = e => {
const { context, props } = this
// ignore click for new tab / new window behavior
if (props.goTo || e.metaKey || e.ctrlKey || e.shiftKey || e.nativeEvent.which === 2) return
e.preventDefault()
const href = props.raw ? props.href : `${props.context || context.context}${props.href}`
return context.navigate(href, true)
}
render() {
const { children, href, goTo, style } = this.props
const props = {
href,
onClick: this.onClick,
style,
}
if (goTo) {
props.target = '_blank'
}
return (
<a {...props}>
{children}
</a>
)
}
}
Teleport.contextTypes = {
context: PropTypes.string,
navigate: PropTypes.func.isRequired,
}
Teleport.propTypes = {
context: PropTypes.string,
href: PropTypes.string,
goTo: PropTypes.bool,
raw: PropTypes.bool,
}