Skip to content

Commit 69ada92

Browse files
committed
feat(): slot方式自定义按钮
1 parent c444914 commit 69ada92

File tree

5 files changed

+84
-71
lines changed

5 files changed

+84
-71
lines changed

packages/style/index.styl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,24 @@
6666
box-shadow: 0 1px 5px rgba(0, 0, 0, .15);
6767
}
6868
}
69-
.org-tree-node-btn {
69+
.org-tree-node-icon {
7070
position: absolute;
7171
top: 100%;
7272
left: 50%;
73-
width: 20px;
74-
height: 20px;
75-
z-index: 10;
7673
margin-left: -11px;
7774
margin-top: 9px;
7875
background-color: #fff;
79-
border: 1px solid #ccc;
80-
border-radius: 50%;
81-
box-shadow: 0 0 2px rgba(0, 0, 0, .15);
76+
z-index: 10;
8277
cursor: pointer;
8378
transition: all .35s ease;
84-
79+
}
80+
.org-tree-node-btn {
81+
@extend .org-tree-node-icon
82+
box-shadow: 0 0 2px rgba(0, 0, 0, .15);
83+
border: 1px solid #ccc;
84+
border-radius: 50%;
85+
width: 20px;
86+
height: 20px;
8587
&:hover {
8688
background-color: #e7e8e9;
8789
transform: scale(1.15);

packages/vueDragTree/model/util.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export function hasClass(el, cls) {
3838

3939
export function addClass(el, cls) {
4040
if (!el) return
41-
console.log(el)
4241
var curClass = el.className
4342
var classes = (cls || '').split(' ')
4443

packages/vueDragTree/src/node-content.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ export default {
1212
const tree = parent.tree
1313
const node = this.node
1414
const { data, store } = node
15+
// console.log(tree.$scopedSlots.default({ node, data }))
1516
return (
1617
parent.renderContent
17-
? parent.renderContent.call(parent._renderProxy, h, { _self: tree.$vnode.context, node, data, store })
18+
? parent.renderContent(h, { _self: tree.$vnode.context, node, data, store })
1819
: tree.$scopedSlots.default
1920
? tree.$scopedSlots.default({ node, data })
2021
: h('span', { class: 'org-tree-node-label-inner' }, [node.label])
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script>
2+
export default {
3+
name: 'NodeIcon',
4+
props: {
5+
node: {
6+
type: Object,
7+
required: true
8+
}
9+
},
10+
mounted () {
11+
},
12+
methods: {
13+
handleExpandIconClick() {
14+
if (this.node.isLeaf) return
15+
if (this.node.expanded) {
16+
this.$emit('node-collapse', this.node.data, this.node, this)
17+
this.node.collapse()
18+
} else {
19+
this.node.expand()
20+
this.$emit('node-expand', this.node.data, this.node, this)
21+
}
22+
}
23+
},
24+
render(h) {
25+
const parent = this.$parent
26+
const tree = parent.tree
27+
const node = this.node
28+
const { data } = node
29+
return (
30+
tree.$scopedSlots['node-icon']
31+
? h('div', {
32+
class: 'org-tree-node-icon',
33+
on: {
34+
click: () => this.handleExpandIconClick()
35+
}
36+
},
37+
[
38+
tree.$scopedSlots['node-icon']({ node, data })
39+
])
40+
: h('span', {
41+
class: ['org-tree-node-btn', { 'is-leaf': node.isLeaf, expanded: !node.isLeaf && node.expanded }],
42+
on: {
43+
click: () => this.handleExpandIconClick()
44+
}
45+
}
46+
)
47+
)
48+
}
49+
}
50+
</script>

packages/vueDragTree/src/tree-node.vue

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,30 @@
1818
@dragend.stop="handleDragEnd"
1919
@drop.stop="handleDrop"
2020
>
21-
<div class="org-tree-node-label" ref="nodelabel" @click="handleClick">
22-
<!-- <span @click.prevent.stop="toggle" :class="['org-tree-node-btn', { 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded }]" v-if="node.children && node.children.length > 0"></span> -->
23-
<span
21+
<div class="org-tree-node-label" ref="nodelabel">
22+
<node-icon
2423
v-if="node.childNodes && node.childNodes.length > 0"
25-
:class="[
26-
'org-tree-node-btn',
27-
{ 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded },
28-
'org-tree-node__expand-icon'
29-
]"
30-
@click.stop="handleExpandIconClick"
24+
:node="node"
3125
/>
32-
<!-- <el-checkbox
33-
v-if="showCheckbox"
34-
v-model="node.checked"
35-
:indeterminate="node.indeterminate"
36-
:disabled="!!node.disabled"
37-
@click.native.stop
38-
@change="handleCheckChange"
39-
>
40-
</el-checkbox> -->
41-
<!-- <span
42-
v-if="node.loading"
43-
class="org-tree-node__loading-icon el-icon-loading"
44-
/> -->
4526
<node-content :node="node" />
4627
</div>
47-
<!-- <el-collapse-transition> -->
48-
<div
49-
v-if="(!renderAfterExpand || childNodeRendered) && node.childNodes.length > 0"
50-
v-show="expanded"
51-
class="org-tree-node-children"
52-
role="group"
53-
:aria-expanded="expanded"
54-
>
55-
<tree-node
56-
v-for="child in node.childNodes"
57-
:key="getNodeKey(child)"
58-
:render-content="renderContent"
59-
:render-after-expand="renderAfterExpand"
60-
:node="child"
61-
@node-expand="handleChildNodeExpand"
62-
/>
28+
<div
29+
v-if="(!renderAfterExpand || childNodeRendered) && node.childNodes.length > 0"
30+
v-show="expanded"
31+
class="org-tree-node-children"
32+
role="group"
33+
:aria-expanded="expanded"
34+
>
35+
<tree-node
36+
v-for="child in node.childNodes"
37+
:key="getNodeKey(child)"
38+
:render-content="renderContent"
39+
:render-after-expand="renderAfterExpand"
40+
:node="child"
41+
@node-expand="handleChildNodeExpand"
42+
/>
43+
</div>
6344
</div>
64-
<!-- </el-collapse-transition> -->
65-
</div>
6645
</template>
6746

6847
<script>
@@ -71,6 +50,7 @@
7150
import emitter from '../model/emiter'
7251
import { getNodeKey } from '../model/util'
7352
import NodeContent from './node-content'
53+
import NodeIcon from './node-icon'
7454
export default {
7555
name: 'TreeNode',
7656
@@ -79,7 +59,8 @@ export default {
7959
components: {
8060
// ElCollapseTransition,
8161
// ElCheckbox,
82-
NodeContent
62+
NodeContent,
63+
NodeIcon
8364
},
8465
8566
mixins: [emitter],
@@ -114,27 +95,10 @@ export default {
11495
console.log(this.node.expanded, this.node, val)
11596
this.$nextTick(() => {
11697
this.expanded = val
117-
// this.expanded =
118-
// if (val) {
119-
// this.expanded = false
120-
// } else {
121-
// this.expanded = true
122-
// }
123-
// if (val && this.node.childNodes.length > 0) {
124-
// this.expanded = false
125-
// } else {
126-
// console.log(this.node.childNodes)
127-
// this.expanded = true
128-
// }
12998
})
13099
if (val) {
131100
this.childNodeRendered = true
132101
}
133-
// if (this.node.childNodes && this.node.childNodes.length > 0) {
134-
// this.leaf = false
135-
// } else {
136-
// this.leaf = true
137-
// }
138102
}
139103
},
140104
@@ -159,7 +123,6 @@ export default {
159123
this.node.updateChildren()
160124
})
161125
if (this.node.expanded) {
162-
console.log('extendsextends')
163126
this.expanded = true
164127
this.childNodeRendered = true
165128
}
@@ -214,12 +177,10 @@ export default {
214177
handleExpandIconClick() {
215178
if (this.node.isLeaf) return
216179
if (this.expanded) {
217-
console.log(1233)
218180
this.tree.$emit('node-collapse', this.node.data, this.node, this)
219181
this.node.collapse()
220182
} else {
221183
this.node.expand()
222-
console.log(789, this.node.data)
223184
this.$emit('node-expand', this.node.data, this.node, this)
224185
}
225186
},

0 commit comments

Comments
 (0)