Skip to content

Commit 3281ed8

Browse files
Anna Rankintimdorr
authored andcommitted
Add Support for "Blank" Link components. (#3803)
* adds test for rendering Links that do not specify "to" prop * Modifies Link to return an empty <a> tag when no "to" prop is given. * Adds example of empty links * Adds spec for passing down of additional props * Updates documentation for Link. * Adds a word to a sentence in Link documentation, cleans up whitespace. * Removes unecessary example page. * Removes default "to" prop on Link component. * Compares "to" to null * Fixes typo in Link-test * Whitespace nit
1 parent 28f693a commit 3281ed8

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

docs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ A [location descriptor](https://github.com/reactjs/react-router/blob/master/docs
113113
* `query`: An object of key:value pairs to be stringified.
114114
* `hash`: A hash to put in the URL, e.g. `#a-hash`.
115115
* `state`: State to persist to the `location`.
116+
* If it is not specified, an anchor tag without an `href` attribute will be rendered.
116117

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

modules/Link.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const Link = React.createClass({
5555
},
5656

5757
propTypes: {
58-
to: oneOfType([ string, object ]).isRequired,
58+
to: oneOfType([ string, object ]),
5959
query: object,
6060
hash: string,
6161
state: object,
@@ -112,6 +112,9 @@ const Link = React.createClass({
112112
const { router } = this.context
113113

114114
if (router) {
115+
// If user does not specify a `to` prop, return an empty anchor tag.
116+
if (to == null) { return <a {...props} /> }
117+
115118
const location = createLocationDescriptor(to, { query, hash, state })
116119
props.href = router.createHref(location)
117120

modules/__tests__/Link-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,45 @@ describe('A <Link>', function () {
455455
})
456456
})
457457

458+
describe('when the "to" prop is unspecified', function () {
459+
class App extends Component {
460+
render() {
461+
return (
462+
<div>
463+
<Link>Blank Link</Link>
464+
<Link/>
465+
<Link className="kitten-link">Kittens</Link>
466+
</div>
467+
)
468+
}
469+
}
470+
471+
it('returns an anchor tag without an href', function (done) {
472+
render((
473+
<Router history={createHistory('/')}>
474+
<Route path="/" component={App} />
475+
</Router>
476+
), node, function () {
477+
const link1 = node.querySelectorAll('a')[0]
478+
const link2 = node.querySelectorAll('a')[1]
479+
const link3 = node.querySelectorAll('a')[2]
480+
expect(link1.href).toEqual('')
481+
expect(link2.href).toEqual('')
482+
expect(link3.href).toEqual('')
483+
done()
484+
})
485+
})
486+
487+
it('passes down other props', function (done) {
488+
render((
489+
<Router history={createHistory('/')}>
490+
<Route path="/" component={App} />
491+
</Router>
492+
), node, function () {
493+
const link3 = node.querySelectorAll('a')[2]
494+
expect(link3.className).toEqual('kitten-link')
495+
done()
496+
})
497+
})
498+
})
458499
})

0 commit comments

Comments
 (0)