link.js 1.7 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
      // ignore click for new tab / new window behavior
      return
    }

N
Naoyuki Kanezawa 已提交
20
    const { href, scroll, as } = this.props
N
nkzawa 已提交
21 22 23 24 25 26 27 28

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

    e.preventDefault()

N
Naoyuki Kanezawa 已提交
29 30 31
    const route = as ? href : null
    const url = as || href

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

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

D
Dan Zajdband 已提交
49
      const isAnchor = child && child.type === 'a'
N
nkzawa 已提交
50 51 52

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

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

    return children[0]
  }
}

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