Skip to content

Commit 68308fa

Browse files
committed
fix if active branch reuse
1 parent 06cab90 commit 68308fa

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/directives/if.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const _if = (el: Element, exp: string, ctx: Context) => {
4444
parent.removeChild(el)
4545

4646
let block: Block | undefined
47+
let activeBranchIndex: number = -1
4748

4849
const removeActiveBlock = () => {
4950
if (block) {
@@ -54,15 +55,20 @@ export const _if = (el: Element, exp: string, ctx: Context) => {
5455
}
5556

5657
ctx.effect(() => {
57-
for (const { exp, el } of branches) {
58+
for (let i = 0; i < branches.length; i++) {
59+
const { exp, el } = branches[i]
5860
if (!exp || evaluate(ctx.scope, exp)) {
59-
removeActiveBlock()
60-
block = new Block(el, ctx)
61-
block.insert(parent, anchor)
62-
parent.removeChild(anchor)
61+
if (i !== activeBranchIndex) {
62+
removeActiveBlock()
63+
block = new Block(el, ctx)
64+
block.insert(parent, anchor)
65+
parent.removeChild(anchor)
66+
activeBranchIndex = i
67+
}
6368
return
6469
}
6570
}
71+
// no matched branch.
6672
removeActiveBlock()
6773
})
6874

0 commit comments

Comments
 (0)