Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit d3a86cf

Browse files
authored
fix(runtime-vapor): fix v-show value (#70)
1 parent 5b3027f commit d3a86cf

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

packages/runtime-vapor/__tests__/vShow.spec.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@ beforeEach(() => {
1515
afterEach(() => {
1616
host.remove()
1717
})
18-
18+
const createDemo = (defaultValue: boolean) =>
19+
defineComponent({
20+
setup() {
21+
const visible = ref(defaultValue)
22+
function handleClick() {
23+
visible.value = !visible.value
24+
}
25+
const t0 = template('<button>toggle</button><h1>hello world</h1>')
26+
const n0 = t0()
27+
const {
28+
0: [n1],
29+
1: [n2],
30+
} = children(n0)
31+
withDirectives(n2, [[vShow, () => visible.value]])
32+
on(n1 as HTMLElement, 'click', handleClick)
33+
return n0
34+
},
35+
})
1936
describe('directive: v-show', () => {
2037
test('basic', async () => {
21-
const demo = defineComponent({
22-
setup() {
23-
const visible = ref(true)
24-
function handleClick() {
25-
visible.value = !visible.value
26-
}
27-
const t0 = template('<button>toggle</button><h1>hello world</h1>')
28-
const n0 = t0()
29-
const {
30-
0: [n1],
31-
1: [n2],
32-
} = children(n0)
33-
withDirectives(n2, [[vShow, () => visible.value]])
34-
on(n1 as HTMLElement, 'click', (...args) => handleClick(...args))
35-
return n0
36-
},
37-
})
38+
const demo = createDemo(true)
3839
render(demo as any, {}, '#host')
3940
const btn = host.querySelector('button')
4041
expect(host.innerHTML).toBe('<button>toggle</button><h1>hello world</h1>')
@@ -44,4 +45,14 @@ describe('directive: v-show', () => {
4445
'<button>toggle</button><h1 style="display: none;">hello world</h1>',
4546
)
4647
})
48+
test('should hide content when default value is false', async () => {
49+
const demo = createDemo(false)
50+
render(demo as any, {}, '#host')
51+
const btn = host.querySelector('button')
52+
const h1 = host.querySelector('h1')
53+
expect(h1?.style.display).toBe('none')
54+
btn?.click()
55+
await nextTick()
56+
expect(h1?.style.display).toBe('')
57+
})
4758
})

packages/runtime-vapor/src/directives/vShow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ObjectDirective } from '../directive'
33
const vShowMap = new WeakMap<HTMLElement, string>()
44

55
export const vShow: ObjectDirective<HTMLElement> = {
6-
beforeMount(node, { source: value }) {
6+
beforeMount(node, { value }) {
77
vShowMap.set(node, node.style.display === 'none' ? '' : node.style.display)
88
setDisplay(node, value)
99
},
@@ -13,7 +13,7 @@ export const vShow: ObjectDirective<HTMLElement> = {
1313
setDisplay(node, value)
1414
},
1515

16-
beforeUnmount(node, { source: value }) {
16+
beforeUnmount(node, { value }) {
1717
setDisplay(node, value)
1818
},
1919
}

0 commit comments

Comments
 (0)