From 04139044345e9c521dc8f7fb1ae3776de240e426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E6=96=87=E6=9D=B0?= Date: Sun, 19 May 2019 21:03:27 +0800 Subject: [PATCH 1/4] Tree: fix #15538 by adding more detect logic. --- packages/tree/src/model/node.js | 7 +++++-- src/utils/util.js | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/tree/src/model/node.js b/packages/tree/src/model/node.js index 454cfcabde0..67d19d1ab63 100644 --- a/packages/tree/src/model/node.js +++ b/packages/tree/src/model/node.js @@ -1,5 +1,6 @@ import objectAssign from 'element-ui/src/utils/merge'; import { markNodeData, NODE_KEY } from './util'; +import { arrayContains } from 'element-ui/src/utils/util'; export const getChildState = node => { let all = true; @@ -435,8 +436,10 @@ export default class Node { const newNodes = []; newData.forEach((item, index) => { - if (item[NODE_KEY]) { - newDataMap[item[NODE_KEY]] = { index, data: item }; + const key = item[NODE_KEY]; + const isNodeExists = !!key && arrayContains(oldData, data => data[NODE_KEY] === key); + if (isNodeExists) { + newDataMap[key] = { index, data: item }; } else { newNodes.push({ index, data: item }); } diff --git a/src/utils/util.js b/src/utils/util.js index 16043af5fa0..db926f54ff6 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -97,6 +97,10 @@ export const arrayFindIndex = function(arr, pred) { return -1; }; +export const arrayContains = function(arr, pred) { + return arrayFindIndex(arr, pred) >= 0; +}; + export const arrayFind = function(arr, pred) { const idx = arrayFindIndex(arr, pred); return idx !== -1 ? arr[idx] : undefined; From 94cdb793dc6cd84392cdb080e083728dc4a1ed7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E6=96=87=E6=9D=B0?= Date: Sun, 19 May 2019 21:23:53 +0800 Subject: [PATCH 2/4] Tree: fix #15538 and add test case. --- test/unit/specs/tree.spec.js | 61 +++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/test/unit/specs/tree.spec.js b/test/unit/specs/tree.spec.js index 7642fa5b74d..18ede2fc70e 100644 --- a/test/unit/specs/tree.spec.js +++ b/test/unit/specs/tree.spec.js @@ -1,4 +1,4 @@ -import { createVue, destroyVM, waitImmediate } from '../util'; +import { createVue, destroyVM, waitImmediate, wait } from '../util'; const DELAY = 10; @@ -831,4 +831,63 @@ describe('Tree', () => { done(); }); }); + + it('update multi tree data', async () => { + const vm = createVue({ + template: ` +
+ + +
+ `, + + data() { + return { + data: [{ + id: 1, + label: '一级 1', + children: [{ + id: 11, + label: '二级 1-1', + children: [{ + id: 111, + label: '三级 1-1' + }] + }] + }, { + id: 2, + label: '一级 2', + children: [{ + id: 21, + label: '二级 2-1' + }, { + id: 22, + label: '二级 2-2' + }] + }, { + id: 3, + label: '一级 3', + children: [{ + id: 31, + label: '二级 3-1' + }, { + id: 32, + label: '二级 3-2' + }] + }], + defaultProps: { + children: 'children', + label: 'label' + } + }; + } + }, true); + const nodeData = { label: '新增 1', id: 4 }; + vm.data.push(nodeData); + await wait(); + const tree1 = vm.$refs.tree1; + expect(tree1.getNode(4).data).to.equal(nodeData); + const tree2 = vm.$refs.tree2; + expect(tree2.getNode(4).data).to.equal(nodeData); + }); }); From 74191d06646eba5b5cecaed365a9b872f9324538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E6=96=87=E6=9D=B0?= Date: Sun, 19 May 2019 23:13:51 +0800 Subject: [PATCH 3/4] Tree: fix eslint error --- test/unit/specs/tree.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/specs/tree.spec.js b/test/unit/specs/tree.spec.js index 18ede2fc70e..403d51c061c 100644 --- a/test/unit/specs/tree.spec.js +++ b/test/unit/specs/tree.spec.js @@ -832,7 +832,7 @@ describe('Tree', () => { }); }); - it('update multi tree data', async () => { + it('update multi tree data', async() => { const vm = createVue({ template: `
From 2a58f4d58a054d8079ffd2ef007a13378c9eb9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?tecfan=28=E8=8C=83=E6=96=87=E6=9D=B0=29?= Date: Mon, 10 Jun 2019 22:09:39 +0800 Subject: [PATCH 4/4] Tree: remove arrayContains function --- packages/tree/src/model/node.js | 4 ++-- src/utils/util.js | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/tree/src/model/node.js b/packages/tree/src/model/node.js index 67d19d1ab63..657b3b82be9 100644 --- a/packages/tree/src/model/node.js +++ b/packages/tree/src/model/node.js @@ -1,6 +1,6 @@ import objectAssign from 'element-ui/src/utils/merge'; import { markNodeData, NODE_KEY } from './util'; -import { arrayContains } from 'element-ui/src/utils/util'; +import { arrayFindIndex } from 'element-ui/src/utils/util'; export const getChildState = node => { let all = true; @@ -437,7 +437,7 @@ export default class Node { newData.forEach((item, index) => { const key = item[NODE_KEY]; - const isNodeExists = !!key && arrayContains(oldData, data => data[NODE_KEY] === key); + const isNodeExists = !!key && arrayFindIndex(oldData, data => data[NODE_KEY] === key) >= 0; if (isNodeExists) { newDataMap[key] = { index, data: item }; } else { diff --git a/src/utils/util.js b/src/utils/util.js index db926f54ff6..16043af5fa0 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -97,10 +97,6 @@ export const arrayFindIndex = function(arr, pred) { return -1; }; -export const arrayContains = function(arr, pred) { - return arrayFindIndex(arr, pred) >= 0; -}; - export const arrayFind = function(arr, pred) { const idx = arrayFindIndex(arr, pred); return idx !== -1 ? arr[idx] : undefined;