|
1 | 1 | /* @flow */
|
2 | 2 |
|
| 3 | +import { remove } from 'shared/util' |
| 4 | + |
3 | 5 | export default {
|
4 |
| - create: registerRef, |
5 |
| - update: registerRef |
| 6 | + create (_: any, vnode: VNodeWithData) { |
| 7 | + registerRef(vnode) |
| 8 | + }, |
| 9 | + update (oldVnode: VNodeWithData, vnode: VNodeWithData) { |
| 10 | + if (oldVnode.data.ref !== vnode.data.ref) { |
| 11 | + registerRef(oldVnode, true) |
| 12 | + registerRef(vnode) |
| 13 | + } |
| 14 | + }, |
| 15 | + destroy (vnode: VNodeWithData) { |
| 16 | + registerRef(vnode, true) |
| 17 | + } |
6 | 18 | }
|
7 | 19 |
|
8 |
| -export function registerRef (_: any, vnode: VNodeWithData) { |
| 20 | +export function registerRef (vnode: VNodeWithData, isRemoval: ?boolean) { |
9 | 21 | const key = vnode.data.ref
|
10 | 22 | if (!key) return
|
11 | 23 |
|
| 24 | + const vm = vnode.context |
12 | 25 | const ref = vnode.componentInstance || vnode.elm
|
13 |
| - const refs = vnode.context.$refs |
14 |
| - |
15 |
| - if (typeof key === 'function') { |
16 |
| - key(ref) |
17 |
| - } else if (vnode.data.refInFor) { |
18 |
| - const refArray = refs[key] |
19 |
| - if (Array.isArray(refArray)) { |
20 |
| - if (refArray.indexOf(ref) < 0) { |
21 |
| - refArray.push(ref) |
22 |
| - } |
23 |
| - } else { |
24 |
| - refs[key] = [ref] |
| 26 | + const refs = vm.$refs |
| 27 | + if (isRemoval) { |
| 28 | + if (Array.isArray(refs[key])) { |
| 29 | + remove(refs[key], ref) |
| 30 | + } else if (refs[key] === ref) { |
| 31 | + refs[key] = undefined |
25 | 32 | }
|
26 | 33 | } else {
|
27 |
| - refs[key] = ref |
28 |
| - } |
29 |
| -} |
30 |
| - |
31 |
| -export function resetRefs (refs: Refs): Refs { |
32 |
| - const res = {} |
33 |
| - // keep existing v-for ref arrays even if empty |
34 |
| - for (const key in refs) { |
35 |
| - if (Array.isArray(refs[key])) { |
36 |
| - res[key] = [] |
| 34 | + if (vnode.data.refInFor) { |
| 35 | + if (Array.isArray(refs[key]) && refs[key].indexOf(ref) < 0) { |
| 36 | + refs[key].push(ref) |
| 37 | + } else { |
| 38 | + refs[key] = [ref] |
| 39 | + } |
| 40 | + } else { |
| 41 | + refs[key] = ref |
37 | 42 | }
|
38 | 43 | }
|
39 |
| - return res |
40 | 44 | }
|
0 commit comments