Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing 'as' prop in Link and fixing 'href' prop #45

Merged
merged 5 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"prop-types": "^15.6.2",
"react": "^16.6.3",
"react-i18next": "^8.3.8",
"react-tree-walker": "^4.3.0"
"react-tree-walker": "^4.3.0",
"url-parse": "^1.4.4"
}
}
18 changes: 13 additions & 5 deletions src/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import NextLink from 'next/link'
import Url from 'url-parse'

export default function () {

Expand All @@ -27,27 +28,34 @@ export default function () {
class Link extends React.Component {
render() {
const { defaultLanguage, localeSubpaths } = config
const { children, href } = this.props
const { as, children, href } = this.props
let lng = null
if (Array.isArray(i18n.languages) && i18n.languages.length > 0) {
[lng] = i18n.languages
}
if (localeSubpaths && lng && lng !== defaultLanguage) {
const url = new Url(href, true)
url.set('query', { ...url.query, lng })

return (
<NextLink href={`${href}?lng=${lng}`} as={`/${lng}${href}`}>
{children}
</NextLink>
<NextLink href={url.href} as={`/${lng}${as || href}`}>{children}</NextLink>
)
}
return <NextLink href={href}>{children}</NextLink>

return <NextLink href={href} as={as}>{children}</NextLink>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to pass null into as?

Copy link
Contributor Author

@capellini capellini Dec 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had left off the defaultProps entirely, which would have left the 'as' prop as undefined, but that triggered the react/require-default-props linting rule. Perhaps I can disable that rule for this file, as I think it's valid to leave 'as' without a default prop. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a genuine question. If it's alright to pass null into as, I have no problems here. I suspect it is.

Otherwise, just put undefined as the default prop. I don't want to disable the react/require-default-props rule - it's important.

}
}

Link.propTypes = {
as: PropTypes.string,
children: PropTypes.node.isRequired,
href: PropTypes.string.isRequired,
}

Link.defaultProps = {
as: undefined,
}

/*
Usage of `withNamespaces` here is just to
force `Link` to rerender on language change
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,11 @@ punycode@^2.1.0:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

querystringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.1.0.tgz#7ded8dfbf7879dcc60d0a644ac6754b283ad17ef"
integrity sha512-sluvZZ1YiTLD5jsqZcDmFyV2EwToyXZBfpoVOmktMmW+VEnhgakFHnasVph65fOjGPTWN0Nw3+XQaSeMayr0kg==

rc@^1.2.7:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
Expand Down Expand Up @@ -3330,6 +3335,11 @@ require-uncached@^1.0.3:
caller-path "^0.1.0"
resolve-from "^1.0.0"

requires-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=

reselect@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/reselect/-/reselect-3.0.1.tgz#efdaa98ea7451324d092b2b2163a6a1d7a9a2147"
Expand Down Expand Up @@ -3825,6 +3835,14 @@ urix@^0.1.0:
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=

url-parse@^1.4.4:
version "1.4.4"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.4.tgz#cac1556e95faa0303691fec5cf9d5a1bc34648f8"
integrity sha512-/92DTTorg4JjktLNLe6GPS2/RvAd/RGr6LuktmWSMLEOa6rjnlrFXNgSbSmkNvCoL2T028A0a1JaJLzRMlFoHg==
dependencies:
querystringify "^2.0.0"
requires-port "^1.0.0"

use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
Expand Down