Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
2451f52b
Unverified
Commit
2451f52b
authored
Apr 28, 2022
by
Tim Neutkens
Committed by
GitHub
Apr 28, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for passing ref to next/link with new behavior enabled (#36537)
parent
3aa8aaae
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
330 additions
and
291 deletions
+330
-291
link.tsx
packages/next/client/link.tsx
+294
-291
typescript.test.ts
test/e2e/new-link-behavior/typescript.test.ts
+8
-0
next.config.js
test/e2e/new-link-behavior/typescript/next.config.js
+6
-0
index.tsx
test/e2e/new-link-behavior/typescript/pages/index.tsx
+1
-0
ref.tsx
test/e2e/new-link-behavior/typescript/pages/ref.tsx
+21
-0
No files found.
packages/next/client/link.tsx
View file @
2451f52b
This diff is collapsed.
Click to expand it.
test/e2e/new-link-behavior/typescript.test.ts
View file @
2451f52b
...
@@ -3,6 +3,7 @@ import { NextInstance } from 'test/lib/next-modes/base'
...
@@ -3,6 +3,7 @@ import { NextInstance } from 'test/lib/next-modes/base'
import
{
renderViaHTTP
}
from
'next-test-utils'
import
{
renderViaHTTP
}
from
'next-test-utils'
import
cheerio
from
'cheerio'
import
cheerio
from
'cheerio'
import
path
from
'path'
import
path
from
'path'
import
webdriver
from
'next-webdriver'
const
appDir
=
path
.
join
(
__dirname
,
'typescript'
)
const
appDir
=
path
.
join
(
__dirname
,
'typescript'
)
...
@@ -14,6 +15,7 @@ describe('New Link Behavior', () => {
...
@@ -14,6 +15,7 @@ describe('New Link Behavior', () => {
files
:
{
files
:
{
pages
:
new
FileRef
(
path
.
join
(
appDir
,
'pages'
)),
pages
:
new
FileRef
(
path
.
join
(
appDir
,
'pages'
)),
'tsconfig.json'
:
new
FileRef
(
path
.
join
(
appDir
,
'tsconfig.json'
)),
'tsconfig.json'
:
new
FileRef
(
path
.
join
(
appDir
,
'tsconfig.json'
)),
'next.config.js'
:
new
FileRef
(
path
.
join
(
appDir
,
'next.config.js'
)),
},
},
dependencies
:
{
dependencies
:
{
typescript
:
'*'
,
typescript
:
'*'
,
...
@@ -31,4 +33,10 @@ describe('New Link Behavior', () => {
...
@@ -31,4 +33,10 @@ describe('New Link Behavior', () => {
expect
(
$a
.
text
()).
toBe
(
'Visit'
)
expect
(
$a
.
text
()).
toBe
(
'Visit'
)
expect
(
$a
.
attr
(
'href'
)).
toBe
(
'/test'
)
expect
(
$a
.
attr
(
'href'
)).
toBe
(
'/test'
)
})
})
it
(
'should apply ref on link'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
`/ref`
)
const
text
=
await
browser
.
elementByCss
(
'#anchor-text'
).
text
()
expect
(
text
).
toBe
(
'AnchorText: About'
)
})
})
})
test/e2e/new-link-behavior/typescript/next.config.js
0 → 100644
View file @
2451f52b
module
.
exports
=
{
reactStrictMode
:
true
,
experimental
:
{
newNextLinkBehavior
:
true
,
},
}
test/e2e/new-link-behavior/typescript/pages/index.tsx
View file @
2451f52b
...
@@ -73,6 +73,7 @@ export const ButtonLink: React.FC<ButtonLinkProps> = ({
...
@@ -73,6 +73,7 @@ export const ButtonLink: React.FC<ButtonLinkProps> = ({
shallow=
{
shallow
}
shallow=
{
shallow
}
// Always passHref to the <a> component if it's defined.
// Always passHref to the <a> component if it's defined.
passHref=
{
!!
href
}
passHref=
{
!!
href
}
legacyBehavior
>
>
<
a
>
{
children
}
</
a
>
<
a
>
{
children
}
</
a
>
</
NextLink
>
</
NextLink
>
...
...
test/e2e/new-link-behavior/typescript/pages/ref.tsx
0 → 100644
View file @
2451f52b
import
{
useRef
,
useEffect
,
useState
}
from
'react'
import
Link
from
'next/link'
export
default
function
Page
()
{
const
ref
=
useRef
<
HTMLAnchorElement
>
()
const
[
anchorText
,
setAnchorText
]
=
useState
(
null
)
useEffect
(()
=>
{
const
anchorElement
=
ref
.
current
setAnchorText
(
anchorElement
?.
textContent
)
},
[])
return
(
<>
{
anchorText
&&
<
h1
id=
"anchor-text"
>
AnchorText:
{
anchorText
}
</
h1
>
}
<
Link
href=
"/about"
ref=
{
ref
}
>
About
</
Link
>
</>
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment