Skip to content

Commit f855b4c

Browse files
sanniassinsanniassin
sanniassin
authored and
sanniassin
committed
Reimplement remix-run#3803 in v4
1 parent 6648a2f commit f855b4c

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

packages/react-router-dom/docs/api/Link.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Provides declarative, accessible navigation around your application.
66
import { Link } from 'react-router-dom'
77

88
<Link to="/about">About</Link>
9+
<Link>Disabled</Link> // renders <a> without a href attribute
910
```
1011

1112
## to: string

packages/react-router-dom/modules/Link.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Link extends React.Component {
1515
to: PropTypes.oneOfType([
1616
PropTypes.string,
1717
PropTypes.object
18-
]).isRequired
18+
])
1919
}
2020

2121
static defaultProps = {
@@ -58,6 +58,10 @@ class Link extends React.Component {
5858
render() {
5959
const { replace, to, ...props } = this.props // eslint-disable-line no-unused-vars
6060

61+
if (to == null) {
62+
return <a {...props} href={null} />
63+
}
64+
6165
const href = this.context.router.history.createHref(
6266
typeof to === 'string' ? { pathname: to } : to
6367
)

packages/react-router-dom/modules/__tests__/Link-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,50 @@ describe('A <Link>', () => {
2424

2525
expect(href).toEqual('/the/path?the=query#the-hash')
2626
})
27+
28+
describe('when the "to" prop is unspecified', () => {
29+
it('returns an anchor tag without a href', () => {
30+
const node = document.createElement('div')
31+
32+
ReactDOM.render((
33+
<MemoryRouter>
34+
<Link>link</Link>
35+
</MemoryRouter>
36+
), node)
37+
38+
const href = node.querySelector('a').getAttribute('href')
39+
40+
expect(href).toEqual(null)
41+
})
42+
43+
it('omits href prop', () => {
44+
const node = document.createElement('div')
45+
46+
ReactDOM.render((
47+
<MemoryRouter>
48+
<Link href="/path">link</Link>
49+
</MemoryRouter>
50+
), node)
51+
52+
const href = node.querySelector('a').getAttribute('href')
53+
54+
expect(href).toEqual(null)
55+
})
56+
57+
it('passes down other props', () => {
58+
const node = document.createElement('div')
59+
60+
ReactDOM.render((
61+
<MemoryRouter>
62+
<Link className="link-class">link</Link>
63+
</MemoryRouter>
64+
), node)
65+
66+
const className = node.querySelector('a').getAttribute('class')
67+
68+
expect(className).toEqual('link-class')
69+
})
70+
})
2771
})
2872

2973
describe('When a <Link> is clicked', () => {

0 commit comments

Comments
 (0)