Skip to content

Commit a7508b1

Browse files
committed
implement transition-group
1 parent bbd022e commit a7508b1

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
export default {}
1+
import { warn } from 'core/util/index'
2+
3+
// Because the vdom's children update algorithm is "unstable" - i.e.
4+
// it doesn't guarantee the relative positioning of removed elements,
5+
// we force transition-group to update its children into two passes:
6+
// in the first pass, we remove all nodes that need to be removed,
7+
// triggering their leaving transition; in the second pass, we insert/move
8+
// into the final disired state. This way in the second pass removed
9+
// nodes will remain where they should be.
10+
11+
export default {
12+
props: ['tag'],
13+
14+
beforeUpdate () {
15+
this.__patch__(this._vnode, this.kept)
16+
this._vnode = this.kept
17+
},
18+
19+
render (h) {
20+
const prevMap = this.prevChildrenMap
21+
const children = this.$slots.default || []
22+
const map = this.prevChildrenMap = {}
23+
const kept = []
24+
25+
for (let i = 0; i < children.length; i++) {
26+
const c = children[i]
27+
if (c.tag) {
28+
if (c.key == null) {
29+
process.env.NODE_ENV !== 'production' && warn(
30+
'transition-group children must be keyed.'
31+
)
32+
c.key = i
33+
}
34+
map[c.key] = c
35+
;(c.data || (c.data = {})).transition = { name: 'fade' }
36+
if (prevMap && prevMap[c.key]) {
37+
kept.push(c)
38+
}
39+
}
40+
}
41+
42+
const tag = this.tag || this.$vnode.data.tag || 'span'
43+
if (this._isMounted) {
44+
this.kept = h(tag, null, kept)
45+
}
46+
47+
return h(tag, null, children)
48+
}
49+
}

0 commit comments

Comments
 (0)