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 "Blank" Link components. #3803

Merged
merged 11 commits into from
Sep 9, 2016
1 change: 1 addition & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ A [location descriptor](https://github.com/reactjs/react-router/blob/master/docs
* `query`: An object of key:value pairs to be stringified.
* `hash`: A hash to put in the URL, e.g. `#a-hash`.
* `state`: State to persist to the `location`.
* If it is not specified, an anchor tag without an `href` attribute will be rendered.

_Note: React Router currently does not manage scroll position, and will not scroll to the element corresponding to `hash`._

Expand Down
5 changes: 4 additions & 1 deletion modules/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Link = React.createClass({
},

propTypes: {
to: oneOfType([ string, object ]).isRequired,
to: oneOfType([ string, object ]),
query: object,
hash: string,
state: object,
Expand Down Expand Up @@ -112,6 +112,9 @@ const Link = React.createClass({
const { router } = this.context

if (router) {
// If user does not specify a `to` prop, return an empty anchor tag.
if (to == null) { return <a {...props} /> }

const location = createLocationDescriptor(to, { query, hash, state })
props.href = router.createHref(location)

Expand Down
41 changes: 41 additions & 0 deletions modules/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,45 @@ describe('A <Link>', function () {
})
})

describe('when the "to" prop is unspecified', function () {
class App extends Component {
render() {
return (
<div>
<Link>Blank Link</Link>
<Link/>
<Link className="kitten-link">Kittens</Link>
</div>
)
}
}

it('returns an anchor tag without an href', function (done) {
render((
<Router history={createHistory('/')}>
<Route path="/" component={App} />
</Router>
), node, function () {
const link1 = node.querySelectorAll('a')[0]
const link2 = node.querySelectorAll('a')[1]
const link3 = node.querySelectorAll('a')[2]
expect(link1.href).toEqual('')
expect(link2.href).toEqual('')
expect(link3.href).toEqual('')
done()
})
})

it('passes down other props', function (done) {
render((
<Router history={createHistory('/')}>
<Route path="/" component={App} />
</Router>
), node, function () {
const link3 = node.querySelectorAll('a')[2]
expect(link3.className).toEqual('kitten-link')
done()
})
})
})
})