Skip to content

Commit 0b5f1dd

Browse files
committed
feat: 新增 slots(toggle, cound)
1 parent 9b81e1a commit 0b5f1dd

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

Diff for: src/json-tree/JsonTree.vue

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
<JsonTreeNode ref="jsonTreeNode"
44
:json-data="jsonData"
55
:show-line="showLine"
6-
:indent="indent"
7-
/>
6+
:indent="indent">
7+
<template v-for="(_, slot) of $scopedSlots"
8+
v-slot:[slot]="scope">
9+
<slot :name="slot" v-bind="scope"></slot>
10+
</template>
11+
</JsonTreeNode>
812
</div>
913
</template>
1014

@@ -56,3 +60,5 @@ export default {
5660
}
5761
}
5862
</script>
63+
64+
<style lang="scss" src="./json-tree.scss"></style>

Diff for: src/json-tree/JsonTreeNode.vue

+40-10
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
:data-json-tree-node-deep="deep"
44
:style="{ paddingLeft: nodePaddingLeft }">
55
<!-- 展开和收起两种状态 -->
6-
<i v-text="localCollapse ? '+' : '-'"
6+
<span
77
:class="{
88
'cursor-pointer': true,
99
'json-tree__toggle': true,
1010
'json-tree__toggle--hidden': shouldHideToggle
1111
}"
1212
@click.stop="onToggle">
13-
</i>
13+
<slot name="toggle" v-bind:collapse="localCollapse">
14+
<i v-text="localCollapse ? '+' : '-'"></i>
15+
</slot>
16+
</span>
1417
<div :class="{
1518
'json-tree__content': true,
1619
'json-tree__content--collapse': localCollapse
@@ -30,7 +33,13 @@
3033
:class="shouldHideToggle ? '' : 'cursor-pointer'"
3134
@click.stop="onToggle">
3235
</span>
33-
<span v-text="cntTip" class="json-tree__cnt-tips"></span>
36+
<span class="json-tree__cnt-tips">
37+
<slot name="count"
38+
v-bind:count="childCnt"
39+
v-bind:type="jsonDataType"
40+
v-text="cntTip">
41+
</slot>
42+
</span>
3443
</template>
3544

3645
<template v-else-if="jsonDataType === 'object'">
@@ -39,7 +48,13 @@
3948
:class="shouldHideToggle ? '' : 'cursor-pointer'"
4049
@click.stop="onToggle">
4150
</span>
42-
<span v-text="cntTip" class="json-tree__cnt-tips"></span>
51+
<span class="json-tree__cnt-tips">
52+
<slot name="count"
53+
v-bind:count="childCnt"
54+
v-bind:type="jsonDataType"
55+
v-text="cntTip">
56+
</slot>
57+
</span>
4358
</template>
4459
<span v-else v-text="displayedJsonVal"
4560
:class="[
@@ -51,11 +66,23 @@
5166
<template v-else>
5267
<span v-if="jsonDataType === 'array'">
5368
<span class="json-tree__bracket--left">[</span>
54-
<span v-text="cntTip" class="json-tree__cnt-tips"></span>
69+
<span class="json-tree__cnt-tips">
70+
<slot name="count"
71+
v-bind:count="childCnt"
72+
v-bind:type="jsonDataType"
73+
v-text="cntTip">
74+
</slot>
75+
</span>
5576
</span>
5677
<span v-else-if="jsonDataType === 'object'">
5778
<span v-text="'{'" class="json-tree__brace--left"></span>
58-
<span v-text="cntTip" class="json-tree__cnt-tips"></span>
79+
<span class="json-tree__cnt-tips">
80+
<slot name="count"
81+
v-bind:count="childCnt"
82+
v-bind:type="jsonDataType"
83+
v-text="cntTip">
84+
</slot>
85+
</span>
5986
</span>
6087
<span v-else v-text="displayedJsonVal"
6188
:class="[
@@ -73,9 +100,14 @@
73100
:json-key="i"
74101
:json-keys="[...jsonKeys, i]"
75102
:json-data="childData"
103+
:indent="indent"
76104
:deep="deep + 1"
77-
:show-line="showLine"
78-
/>
105+
:show-line="showLine">
106+
<template v-for="(_, slot) of $scopedSlots"
107+
v-slot:[slot]="scope">
108+
<slot :name="slot" v-bind="scope"></slot>
109+
</template>
110+
</JsonTreeNode>
79111
</template>
80112
<div v-if="!localCollapse && ['array', 'object'].includes(jsonDataType)"
81113
class="json-tree__content__footer">
@@ -94,5 +126,3 @@
94126
</template>
95127

96128
<script src="./json-tree-node.js"></script>
97-
98-
<style lang="scss" src="./json-tree.scss"></style>

Diff for: src/json-tree/json-tree-node.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,22 @@ export default {
9191
if (jsonDataType === 'string') return `"${jsonData}"`
9292
return jsonData // data 为 number 类型
9393
},
94-
cntTip () {
94+
childCnt () {
9595
const { jsonData, jsonDataType } = this
9696

97+
if (jsonDataType === 'array') return jsonData.length
98+
if (jsonDataType === 'object') return Object.keys(jsonData).length
99+
return 0
100+
},
101+
cntTip () {
102+
const { jsonDataType, childCnt } = this
103+
97104
if (jsonDataType === 'array') {
98-
return `// ${jsonData.length} items`
105+
return `// ${childCnt} items`
99106
}
100107

101108
if (jsonDataType === 'object') {
102-
return `// ${Object.keys(jsonData).length} keys`
109+
return `// ${childCnt} keys`
103110
}
104111

105112
return ''

Diff for: src/json-tree/json-tree.scss

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929
.json-tree__toggle {
3030
height: $row-height;
3131
line-height: $row-height;
32-
padding-right: 5px;
32+
padding-right: 4px;
3333
font-style: normal;
3434

35+
i {
36+
font-style: normal;
37+
}
38+
3539
&--hidden {
3640
visibility: hidden;
3741
opacity: 0;

0 commit comments

Comments
 (0)