link.js 1.7 KB
Newer Older
1 2
import React, { Component, Children } from 'react'
import Router from './router'
N
nkzawa 已提交
3 4 5 6 7 8 9 10

export default class Link extends Component {
  constructor (props) {
    super(props)
    this.linkClicked = this.linkClicked.bind(this)
  }

  linkClicked (e) {
D
Dan Zajdband 已提交
11
    if (e.target.nodeName === 'A' &&
12
      (e.metaKey || e.ctrlKey || e.shiftKey || (e.nativeEvent && e.nativeEvent.which === 2))) {
N
nkzawa 已提交
13 14 15 16
      // ignore click for new tab / new window behavior
      return
    }

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

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

    e.preventDefault()

N
Naoyuki Kanezawa 已提交
26 27 28
    const route = as ? href : null
    const url = as || href

N
nkzawa 已提交
29
    // straight up redirect
30 31 32 33 34 35 36 37
    Router.push(route, url)
      .then((success) => {
        if (!success) return
        if (scroll !== false) window.scrollTo(0, 0)
      })
      .catch((err) => {
        if (this.props.onError) this.props.onError(err)
      })
N
nkzawa 已提交
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
Naoyuki Kanezawa 已提交
51
        props.href = this.props.as || this.props.href
N
nkzawa 已提交
52 53
      }

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

    return children[0]
  }
}

65
export 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)
}