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

Add support for URL objects in Link and Router #1345

Merged
merged 19 commits into from
Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
10 changes: 8 additions & 2 deletions lib/link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'url'
import { resolve, format } from 'url'
import React, { Component, Children, PropTypes } from 'react'
import Router from './router'
import { warn, execOnce, getLocationOrigin } from './utils'
Expand Down Expand Up @@ -39,6 +39,11 @@ export default class Link extends Component {
return
}

if (typeof href === 'object') {
Copy link
Member

Choose a reason for hiding this comment

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

Since typeof null === 'object' results in true and require('url').format(null) throws an exception, should we do if(href && typeof href === 'object')?

// format href object to generate url
href = format(href)
}

const { pathname } = window.location
href = resolve(pathname, href)
as = as ? resolve(pathname, as) : href
Expand Down Expand Up @@ -97,7 +102,8 @@ export default class Link extends Component {

// If child is an <a> tag and doesn't have a href attribute we specify it so that repetition is not needed by the user
if (child.type === 'a' && !('href' in child.props)) {
props.href = this.props.as || this.props.href
props.href = this.props.as ||
(typeof this.props.href === 'object' ? format(this.props.href) : this.props.href)
}

return React.cloneElement(child, props)
Expand Down
6 changes: 4 additions & 2 deletions lib/router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ export default class Router extends EventEmitter {
}

push (url, as = url) {
return this.change('pushState', url, as)
const formattedUrl = typeof url === 'object' ? format(url) : url
return this.change('pushState', formattedUrl, as === url ? formattedUrl : as)
}

replace (url, as = url) {
return this.change('replaceState', url, as)
const formattedUrl = typeof url === 'object' ? format(url) : url
return this.change('replaceState', formattedUrl, as === url ? formattedUrl : as)
}

async change (method, url, as) {
Expand Down