|
| 1 | +import React, { Component, PropTypes, Children } from 'react' |
| 2 | + |
| 3 | +export default class Link extends Component { |
| 4 | + static contextTypes = { |
| 5 | + router: PropTypes.object |
| 6 | + } |
| 7 | + |
| 8 | + constructor (props) { |
| 9 | + super(props) |
| 10 | + this.linkClicked = this.linkClicked.bind(this) |
| 11 | + } |
| 12 | + |
| 13 | + linkClicked (e) { |
| 14 | + if ('A' === e.target.nodeName && |
| 15 | + (e.metaKey || e.ctrlKey || e.shiftKey || 2 === e.nativeEvent.which)) { |
| 16 | + // ignore click for new tab / new window behavior |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + const { href, scroll } = this.props |
| 21 | + |
| 22 | + if (!isLocal(href)) { |
| 23 | + // ignore click if it's outside our scope |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + e.preventDefault() |
| 28 | + |
| 29 | + // straight up redirect |
| 30 | + this.context.router.goTo(href, (err) => { |
| 31 | + if (err) { |
| 32 | + if (this.props.onError) this.props.onError(err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if (false !== scroll) { |
| 37 | + window.scrollTo(0, 0) |
| 38 | + } |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + render () { |
| 43 | + const children = Children.map(this.props.children, (child) => { |
| 44 | + const props = { |
| 45 | + onClick: this.linkClicked |
| 46 | + } |
| 47 | + |
| 48 | + const isChildAnchor = child && 'a' === child.type |
| 49 | + |
| 50 | + // if child does not specify a href, specify it |
| 51 | + // so that repetition is not needed by the user |
| 52 | + if (!isChildAnchor || !('href' in child.props)) { |
| 53 | + props.href = this.props.href |
| 54 | + } |
| 55 | + |
| 56 | + if (isChildAnchor) { |
| 57 | + return React.cloneElement(child, props) |
| 58 | + } else { |
| 59 | + return <a {...props}>{child}</a> |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + return children[0] |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function isLocal (href) { |
| 68 | + const origin = location.origin |
| 69 | + return !/^https?:\/\//.test(href) || |
| 70 | + origin === href.substr(0, origin.length) |
| 71 | +} |
0 commit comments