File tree Expand file tree Collapse file tree 3 files changed +50
-1
lines changed
packages/react-router-dom Expand file tree Collapse file tree 3 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ Provides declarative, accessible navigation around your application.
6
6
import { Link } from ' react-router-dom'
7
7
8
8
< Link to= " /about" > About< / Link>
9
+ < Link> Disabled< / Link> // renders <a> without a href attribute
9
10
```
10
11
11
12
## to: string
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class Link extends React.Component {
15
15
to : PropTypes . oneOfType ( [
16
16
PropTypes . string ,
17
17
PropTypes . object
18
- ] ) . isRequired
18
+ ] )
19
19
}
20
20
21
21
static defaultProps = {
@@ -58,6 +58,10 @@ class Link extends React.Component {
58
58
render ( ) {
59
59
const { replace, to, ...props } = this . props // eslint-disable-line no-unused-vars
60
60
61
+ if ( to == null ) {
62
+ return < a { ...props } href = { null } />
63
+ }
64
+
61
65
const href = this . context . router . history . createHref (
62
66
typeof to === 'string' ? { pathname : to } : to
63
67
)
Original file line number Diff line number Diff line change @@ -24,6 +24,50 @@ describe('A <Link>', () => {
24
24
25
25
expect ( href ) . toEqual ( '/the/path?the=query#the-hash' )
26
26
} )
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
+ } )
27
71
} )
28
72
29
73
describe ( 'When a <Link> is clicked' , ( ) => {
You can’t perform that action at this time.
0 commit comments