link.js 1.6 KB
Newer Older
N
nkzawa 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
import React, { Component, PropTypes, Children } from 'react'

export default class Link extends Component {
  static contextTypes = {
    router: PropTypes.object
  }

  constructor (props) {
    super(props)
    this.linkClicked = this.linkClicked.bind(this)
  }

  linkClicked (e) {
D
Dan Zajdband 已提交
14
    if (e.target.nodeName === 'A' &&
15
      (e.metaKey || e.ctrlKey || e.shiftKey || (e.nativeEvent && e.nativeEvent.which === 2))) {
N
nkzawa 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29
      // ignore click for new tab / new window behavior
      return
    }

    const { href, scroll } = this.props

    if (!isLocal(href)) {
      // ignore click if it's outside our scope
      return
    }

    e.preventDefault()

    // straight up redirect
N
nkzawa 已提交
30 31 32
    this.context.router.push(null, href)
    .then((success) => {
      if (!success) return
D
Dan Zajdband 已提交
33
      if (scroll !== false) window.scrollTo(0, 0)
N
nkzawa 已提交
34 35 36
    })
    .catch((err) => {
      if (this.props.onError) this.props.onError(err)
N
nkzawa 已提交
37 38 39 40 41 42 43 44 45
    })
  }

  render () {
    const children = Children.map(this.props.children, (child) => {
      const props = {
        onClick: this.linkClicked
      }

D
Dan Zajdband 已提交
46
      const isAnchor = child && child.type === 'a'
N
nkzawa 已提交
47 48 49

      // if child does not specify a href, specify it
      // so that repetition is not needed by the user
N
nkzawa 已提交
50
      if (!isAnchor || !('href' in child.props)) {
N
nkzawa 已提交
51 52 53
        props.href = this.props.href
      }

N
nkzawa 已提交
54
      if (isAnchor) {
N
nkzawa 已提交
55 56 57 58 59 60 61 62 63 64 65
        return React.cloneElement(child, props)
      } else {
        return <a {...props}>{child}</a>
      }
    })

    return children[0]
  }
}

function isLocal (href) {
D
Dan Zajdband 已提交
66
  const origin = window.location.origin
N
nkzawa 已提交
67 68 69
  return !/^https?:\/\//.test(href) ||
    origin === href.substr(0, origin.length)
}