Skip to content

Commit 3a31f08

Browse files
committed
wip: auto generate key for vif branch if it wraps in transition
1 parent 75de3bb commit 3a31f08

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

packages/compiler-vapor/src/generators/block.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ export function genBlockContent(
5555
push(...genOperations(operation, context))
5656
push(...genEffects(effect, context))
5757

58+
if (dynamic.needsKey) {
59+
for (const child of dynamic.children) {
60+
push(NEWLINE, `n${child.id}.key = ${JSON.stringify(child.id)}`)
61+
}
62+
}
63+
5864
push(NEWLINE, `return `)
5965

6066
const returnNodes = returns.map(n => `n${n}`)

packages/compiler-vapor/src/ir/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export interface IRDynamicInfo {
259259
children: IRDynamicInfo[]
260260
template?: number
261261
hasDynamicChild?: boolean
262+
needsKey?: boolean
262263
}
263264

264265
export interface IREffect {

packages/compiler-vapor/src/transforms/vIf.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type ElementNode,
33
ErrorCodes,
4+
NodeTypes,
45
createCompilerError,
56
createSimpleExpression,
67
} from '@vue/compiler-dom'
@@ -123,5 +124,17 @@ export function createIfBranch(
123124
const branch: BlockIRNode = newBlock(node)
124125
const exitBlock = context.enterBlock(branch)
125126
context.reference()
127+
// generate key for branch result when it's in transition
128+
// the key will be used to cache node at runtime
129+
branch.dynamic.needsKey = isInTransition(context)
126130
return [branch, exitBlock]
127131
}
132+
133+
function isInTransition(context: TransformContext<ElementNode>): boolean {
134+
const parentNode = context.parent && context.parent.node
135+
return !!(
136+
parentNode &&
137+
parentNode.type === NodeTypes.ELEMENT &&
138+
(parentNode.tag === 'transition' || parentNode.tag === 'Transition')
139+
)
140+
}

0 commit comments

Comments
 (0)