link.js 1.6 KB
Newer Older
N
nkzawa 已提交
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
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) {
    if ('A' === e.target.nodeName &&
      (e.metaKey || e.ctrlKey || e.shiftKey || 2 === e.nativeEvent.which)) {
      // 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 33 34 35 36
    this.context.router.push(null, href)
    .then((success) => {
      if (!success) return
      if (false !== scroll) window.scrollTo(0, 0)
    })
    .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
      }

N
nkzawa 已提交
46
      const isAnchor = child && 'a' === child.type
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 66 67 68 69
        return React.cloneElement(child, props)
      } else {
        return <a {...props}>{child}</a>
      }
    })

    return children[0]
  }
}

function isLocal (href) {
  const origin = location.origin
  return !/^https?:\/\//.test(href) ||
    origin === href.substr(0, origin.length)
}