Skip to content

Commit 1adb9f4

Browse files
authored
Merge pull request #97 from grapoza/fix-function-prop-defaults
Fixes assigning function props from defaults
2 parents 9d98ff2 + 4bc72ff commit 1adb9f4

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Yet another Vue treeview component.",
44
"author": "Gregg Rapoza <grapoza@gmail.com>",
55
"license": "MIT",
6-
"version": "0.10.1",
6+
"version": "0.10.2",
77
"browser": "index.js",
88
"repository": {
99
"url": "https://github.com/grapoza/vue-tree",

src/components/TreeViewNode.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,16 @@
361361
Object.assign(target, sourceCopy);
362362
363363
// Find object properties to deep assign them
364+
// and find function properties and assign if missing in target
364365
for (const propName of Object.keys(source)) {
365366
const propValue = source[propName];
367+
366368
if (this.$_treeViewNode_isProbablyObject(propValue)) {
367369
this.$_treeViewNode_assignDefaultProps(propValue, target[propName]);
368370
}
371+
else if (typeof propValue === 'function' && !target[propName]) {
372+
target[propName] = propValue;
373+
}
369374
}
370375
}
371376
},

tests/local/addRemove.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Add/Delete Usage</title>
6+
<script src="https://unpkg.com/vue"></script>
7+
<script src="../../dist/vue-tree.umd.js"></script>
8+
<link rel="stylesheet" href="demo.css">
9+
<link rel="stylesheet" href="../../dist/vue-tree.css">
10+
</head>
11+
<body>
12+
<div class="container">
13+
<h1>Basic Treeview Demo</h1>
14+
<div id="app">
15+
<tree id="customtree" :initial-model="model" :model-defaults="modelDefaults" ref="tree"></tree>
16+
</div>
17+
</div>
18+
19+
<script type='module'>
20+
import arModel from './addRemove.js';
21+
22+
new Vue({
23+
components: {
24+
tree: window['vue-tree']
25+
},
26+
data() {
27+
return {
28+
childCounter: 0,
29+
model: arModel,
30+
modelDefaults: {
31+
addChildCallback: this.addChildCallback
32+
}
33+
};
34+
},
35+
methods: {
36+
addChildCallback (parentModel) {
37+
this.childCounter++;
38+
return Promise.resolve({ id: `child-node${this.childCounter}`, label: `Added Child ${this.childCounter}`, deletable: true });
39+
}
40+
}
41+
}).$mount('#app');
42+
</script>
43+
</body>
44+
</html>

tests/local/addRemove.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default [
2+
{
3+
id: 'rootNode',
4+
label: 'Root Node'
5+
}
6+
];

tests/local/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h1>Test Pages index</h1>
1313
<li><a href='./basic.html'>Basic Checkboxes</a></li>
1414
<li><a href='./radioBasic.html'>Basic Radiobuttons</a></li>
1515
<li><a href='./slots.html'>Slotting</a></li>
16+
<li><a href='./addRemove.html'>Add/Remove Nodes</a></li>
1617
</ul>
1718
</div>
1819
</body>

tests/unit/TreeViewNode.spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('TreeViewNode.vue', () => {
199199
});
200200
});
201201

202-
describe('when there is an addChildCallback method', () => {
202+
describe('when there is an addChildCallback method in the model', () => {
203203

204204
let addChildButton = null;
205205

@@ -226,6 +226,33 @@ describe('TreeViewNode.vue', () => {
226226
});
227227
});
228228

229+
describe('when there is an addChildCallback method in the model defaults', () => {
230+
231+
let addChildButton = null;
232+
233+
beforeEach(() => {
234+
let radioState = {};
235+
let addChildCallback = () => {
236+
return Promise.resolve(null);
237+
};
238+
239+
wrapper = createWrapper({
240+
ariaKeyMap: {},
241+
initialModel: generateNodes(['esa'], radioState)[0],
242+
modelDefaults: { addChildCallback },
243+
depth: 0,
244+
treeId: 'tree',
245+
radioGroupValues: radioState
246+
});
247+
248+
addChildButton = wrapper.find('#' + wrapper.vm.nodeId + '-add-child');
249+
});
250+
251+
it('should include an add button', async () => {
252+
expect(addChildButton.exists()).to.be.true;
253+
});
254+
});
255+
229256
describe('when a node\'s model is disabled', () => {
230257

231258
beforeEach(() => {

0 commit comments

Comments
 (0)