Skip to content

Commit 425310e

Browse files
committed
fix(transition/v-show): ensure transition is in persisted mode when used with v-show
fix #4845 close #4852
1 parent 2bab639 commit 425310e

File tree

8 files changed

+319
-166
lines changed

8 files changed

+319
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import { compile } from '../../src'
2+
3+
describe('Transition multi children warnings', () => {
4+
function checkWarning(
5+
template: string,
6+
shouldWarn: boolean,
7+
message = `<Transition> expects exactly one child element or component.`
8+
) {
9+
const spy = jest.fn()
10+
compile(template.trim(), {
11+
hoistStatic: true,
12+
transformHoist: null,
13+
onError: err => {
14+
spy(err.message)
15+
}
16+
})
17+
18+
if (shouldWarn) expect(spy).toHaveBeenCalledWith(message)
19+
else expect(spy).not.toHaveBeenCalled()
20+
}
21+
22+
test('warns if multiple children', () => {
23+
checkWarning(
24+
`
25+
<transition>
26+
<div>hey</div>
27+
<div>hey</div>
28+
</transition>
29+
`,
30+
true
31+
)
32+
})
33+
34+
test('warns with v-for', () => {
35+
checkWarning(
36+
`
37+
<transition>
38+
<div v-for="i in items">hey</div>
39+
</transition>
40+
`,
41+
true
42+
)
43+
})
44+
45+
test('warns with multiple v-if + v-for', () => {
46+
checkWarning(
47+
`
48+
<transition>
49+
<div v-if="a" v-for="i in items">hey</div>
50+
<div v-else v-for="i in items">hey</div>
51+
</transition>
52+
`,
53+
true
54+
)
55+
})
56+
57+
test('warns with template v-if', () => {
58+
checkWarning(
59+
`
60+
<transition>
61+
<template v-if="ok"></template>
62+
</transition>
63+
`,
64+
true
65+
)
66+
})
67+
68+
test('warns with multiple templates', () => {
69+
checkWarning(
70+
`
71+
<transition>
72+
<template v-if="a"></template>
73+
<template v-else></template>
74+
</transition>
75+
`,
76+
true
77+
)
78+
})
79+
80+
test('warns if multiple children with v-if', () => {
81+
checkWarning(
82+
`
83+
<transition>
84+
<div v-if="one">hey</div>
85+
<div v-if="other">hey</div>
86+
</transition>
87+
`,
88+
true
89+
)
90+
})
91+
92+
test('does not warn with regular element', () => {
93+
checkWarning(
94+
`
95+
<transition>
96+
<div>hey</div>
97+
</transition>
98+
`,
99+
false
100+
)
101+
})
102+
103+
test('does not warn with one single v-if', () => {
104+
checkWarning(
105+
`
106+
<transition>
107+
<div v-if="a">hey</div>
108+
</transition>
109+
`,
110+
false
111+
)
112+
})
113+
114+
test('does not warn with v-if v-else-if v-else', () => {
115+
checkWarning(
116+
`
117+
<transition>
118+
<div v-if="a">hey</div>
119+
<div v-else-if="b">hey</div>
120+
<div v-else>hey</div>
121+
</transition>
122+
`,
123+
false
124+
)
125+
})
126+
127+
test('does not warn with v-if v-else', () => {
128+
checkWarning(
129+
`
130+
<transition>
131+
<div v-if="a">hey</div>
132+
<div v-else>hey</div>
133+
</transition>
134+
`,
135+
false
136+
)
137+
})
138+
})
139+
140+
test('inject persisted when child has v-show', () => {
141+
expect(
142+
compile(`
143+
<transition>
144+
<div v-show="ok" />
145+
</transition>
146+
`).code
147+
).toMatchSnapshot()
148+
})
149+
150+
test('the v-if/else-if/else branches in Transition should ignore comments', () => {
151+
expect(
152+
compile(`
153+
<transition>
154+
<div v-if="a">hey</div>
155+
<!-- this should be ignored -->
156+
<div v-else-if="b">hey</div>
157+
<!-- this should be ignored -->
158+
<div v-else>
159+
<p v-if="c"/>
160+
<!-- this should not be ignored -->
161+
<p v-else/>
162+
</div>
163+
</transition>
164+
`).code
165+
).toMatchSnapshot()
166+
})
+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`inject persisted when child has v-show 1`] = `
4+
"const _Vue = Vue
5+
6+
return function render(_ctx, _cache) {
7+
with (_ctx) {
8+
const { vShow: _vShow, createElementVNode: _createElementVNode, withDirectives: _withDirectives, Transition: _Transition, withCtx: _withCtx, openBlock: _openBlock, createBlock: _createBlock } = _Vue
9+
10+
return (_openBlock(), _createBlock(_Transition, { persisted: \\"\\" }, {
11+
default: _withCtx(() => [
12+
_withDirectives(_createElementVNode(\\"div\\", null, null, 512 /* NEED_PATCH */), [
13+
[_vShow, ok]
14+
])
15+
]),
16+
_: 1 /* STABLE */
17+
}))
18+
}
19+
}"
20+
`;
21+
322
exports[`the v-if/else-if/else branches in Transition should ignore comments 1`] = `
423
"const _Vue = Vue
524

packages/compiler-dom/__tests__/transforms/warnTransitionChildren.spec.ts

-158
This file was deleted.

packages/compiler-dom/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { transformVText } from './transforms/vText'
1616
import { transformModel } from './transforms/vModel'
1717
import { transformOn } from './transforms/vOn'
1818
import { transformShow } from './transforms/vShow'
19-
import { warnTransitionChildren } from './transforms/warnTransitionChildren'
19+
import { transformTransition } from './transforms/Transition'
2020
import { stringifyStatic } from './transforms/stringifyStatic'
2121
import { ignoreSideEffectTags } from './transforms/ignoreSideEffectTags'
2222
import { extend } from '@vue/shared'
@@ -25,11 +25,11 @@ export { parserOptions }
2525

2626
export const DOMNodeTransforms: NodeTransform[] = [
2727
transformStyle,
28-
...(__DEV__ ? [warnTransitionChildren] : [])
28+
...(__DEV__ ? [transformTransition] : [])
2929
]
3030

3131
export const DOMDirectiveTransforms: Record<string, DirectiveTransform> = {
32-
cloak: noopDirectiveTransform,
32+
cloak: noopDirectiveTransform,
3333
html: transformVHtml,
3434
text: transformVText,
3535
model: transformModel, // override compiler-core

0 commit comments

Comments
 (0)