Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { arrayContains } 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 && arrayContains(oldData, data => data[NODE_KEY] === key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add arrayContains, use arrayFindIndex directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, it has been updated.

if (isNodeExists) {
newDataMap[key] = { index, data: item };
} else {
newNodes.push({ index, data: item });
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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);
});
});