Skip to content

Commit

Permalink
Tree: Fix issue #15538 caused by two Tree sharing the same data. (#15615
Browse files Browse the repository at this point in the history
)

* Tree: fix #15538 by adding more detect logic.

* Tree: fix #15538 and add test case.

* Tree: fix eslint error

* Tree: remove arrayContains function
  • Loading branch information
VanMess authored and island205 committed Jun 21, 2019
1 parent 69715b6 commit ca9a033
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/tree/src/model/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import objectAssign from 'element-ui/src/utils/merge';
import { markNodeData, NODE_KEY } from './util';
import { arrayFindIndex } from 'element-ui/src/utils/util';

export const getChildState = node => {
let all = true;
Expand Down Expand Up @@ -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 && arrayFindIndex(oldData, data => data[NODE_KEY] === key) >= 0;
if (isNodeExists) {
newDataMap[key] = { index, data: item };
} else {
newNodes.push({ index, data: item });
}
Expand Down
61 changes: 60 additions & 1 deletion test/unit/specs/tree.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createVue, destroyVM, waitImmediate } from '../util';
import { createVue, destroyVM, waitImmediate, wait } from '../util';

const DELAY = 10;

Expand Down Expand Up @@ -831,4 +831,63 @@ describe('Tree', () => {
done();
});
});

it('update multi tree data', async() => {
const vm = createVue({
template: `
<div>
<el-tree ref="tree1" :data="data" node-key="id" :props="defaultProps"></el-tree>
<el-tree ref="tree2" :data="data" node-key="id" :props="defaultProps"></el-tree>
</div>
`,

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);
});
});

0 comments on commit ca9a033

Please sign in to comment.