Repository URL to install this package:
|
Version:
0.9.5 ▾
|
/* eslint brace-style: "OFF" */
import React from 'react'
import {
Link as ReactRouterLink,
NavLink as ReactRouterNavLink,
} from 'react-router-dom'
import { isNil, isTrue } from 'exotic'
import {
boolean,
string,
array,
obj,
func,
node,
any,
oneOfType,
} from '@skava/modules/___dist/view-container/types'
const IS_TEST = process.env.NODE_ENV === 'test'
const isValueExternal = to =>
to.includes('http') || to.includes('mailto') || to.includes('tel')
/**
* @see https://github.com/ReactTraining/react-router/issues/2051
*/
class Link extends React.PureComponent {
/**
* href, url, to, address
*/
static propTypes = {
/**
* @type {String || Object || Array || URL}
*/
to: any,
/**
* @type {String || Object}
*/
className: any,
/**
* @type {String || Object}
*/
activeClassName: any,
// === label
/**
* @alias text
*/
children: any,
/**
* @alias label
* @alias title
*/
text: string,
/**
* @optional
*/
isExternal: boolean,
/**
* @description can set the prefix for the url
*/
urlBase: string,
/**
* @description open in a new tab/window/same window
*/
target: string,
/**
* === synonymous, use oneOf, not multiple
*/
href: string,
link: string,
url: string,
onClick: func,
/**
* @type {Function | Object | String}
*/
styles: string,
/**
* @version 0 - was boolean
* @version 1 - is string or node
*/
missingLinkTag: oneOfType([string, node]),
/**
* @example <Link nofollow />
*/
nofollow: boolean,
/**
* @alias shouldNotBeFollowedByRobots
* @example <Link shouldNotFollow />
*/
shouldNotFollow: boolean,
}
// eslint-disable-next-line
render() {
let {
// actual target
to,
href,
url,
link,
onClick,
// label
text,
children,
//
className,
//
isExternal,
//
missingLinkTag,
//
target,
//
nofollow,
shouldNotFollow,
...remainingProps
} = this.props
to = to || href || url || link || onClick
// <a text="eh">
// <a children="eh">
// <a>{'eh'}</a>
// when passing text in via children
if (isNil(text)) {
// || to
text = children || null
}
if (to && missingLinkTag === true) {
const Tag = missingLinkTag
return <Tag>{to}</Tag>
}
if (process.env.NODE_ENV !== 'production') {
if (!to) {
throw new Error('/MISSING-LINK' + JSON.stringify(this.props))
}
}
// if (isNil(to)) {
// to = href || url
// }
// check if external
if (isValueExternal(to)) {
isExternal = true
}
const attributes = {
...remainingProps,
target,
className,
children: text,
}
if (nofollow === true || shouldNotFollow === true) {
attributes.rel = 'no-follow'
}
// if (isTrue(isExternal) || IS_TEST) {
/**
* As per James guidance,
* temparorly commenting the ReactRouterLink
*/
attributes.href = to
return <a {...attributes} />
// } else {
// attributes.to = {
// pathname: to,
// }
// }
// const view = <ReactRouterNavLink {...attributes} />
// return view
// <Link to={to}>{text}</Link>, <a href="">{text}</a>
// if (isRelative(url))
// if (isAbsolute(url))
// if (routes.includes(url))
}
}
export { Link }
export default Link