Skip to content

Commit c502600

Browse files
authored
[MCTB-2008] Add support for clickable fields (#1)
1 parent de3eeec commit c502600

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

lib/json-box.vue

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,22 @@ export default {
3333
type: String,
3434
default: '$',
3535
},
36+
clickable: {
37+
type: Boolean,
38+
required: true,
39+
},
3640
},
3741
data() {
3842
return {
3943
expand: true,
4044
forceExpandMe: this.forceExpand,
4145
}
4246
},
47+
computed: {
48+
thisClickable() {
49+
return this.clickable && ['string', 'number', 'boolean'].includes(typeof this.value) || this.value === null;
50+
},
51+
},
4352
mounted() {
4453
this.expand = this.previewMode || (this.depth >= this.expandDepth ? false : true) || this.forceExpandMe
4554
},
@@ -149,6 +158,7 @@ export default {
149158
showArrayIndex: this.showArrayIndex,
150159
showDoubleQuotes: this.showDoubleQuotes,
151160
path: this.path,
161+
clickable: this.clickable,
152162
},
153163
on: {
154164
'update:expand': value => {
@@ -157,17 +167,28 @@ export default {
157167
'update:expandAll': value => {
158168
this.expand = value
159169
this.forceExpandMe = this.expand
160-
}
170+
},
171+
fieldClick: data => {
172+
this.$emit('fieldClick', data);
173+
},
161174
}
162175
}))
163176
177+
const clickHandler = {
178+
click: () => {
179+
this.$emit("fieldClick", {path: this.path, value: this.value});
180+
},
181+
};
182+
const spanClass = {class: {flex: typeof this.value === 'string'}};
164183
return h('div', {
165184
class: {
166185
'jv-node': true,
167186
'jv-key-node': Boolean(this.keyName) && !complex,
168-
'toggle': !this.previewMode && complex
169-
}
170-
}, elements)
187+
'toggle': !this.previewMode && complex,
188+
'jv-clickable': this.thisClickable,
189+
},
190+
on: this.thisClickable ? clickHandler : {},
191+
}, this.thisClickable ? [h('span', spanClass, elements)] : elements)
171192
}
172193
}
173194
</script>
@@ -193,4 +214,15 @@ export default {
193214
margin-left: 25px;
194215
}
195216
}
217+
218+
.jv-clickable span {
219+
&.flex {
220+
display: inline-flex;
221+
}
222+
223+
&:hover {
224+
background-color: #FFE0B2;
225+
cursor: pointer;
226+
}
227+
}
196228
</style>

lib/json-viewer.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
:preview-mode="previewMode"
3232
:show-array-index="showArrayIndex"
3333
:show-double-quotes="showDoubleQuotes"
34+
:clickable="clickable"
3435
@keyclick="onKeyclick"
36+
@fieldClick="$emit('fieldClick', $event)"
3537
/>
3638
</div>
3739
<div
@@ -102,7 +104,11 @@ export default {
102104
showDoubleQuotes: {
103105
type: Boolean,
104106
default: false,
105-
}
107+
},
108+
clickable: {
109+
type: Boolean,
110+
default: false,
111+
},
106112
},
107113
provide () {
108114
return {

lib/types/json-array.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export default {
2323
showArrayIndex: Boolean,
2424
showDoubleQuotes: Boolean,
2525
path: String,
26+
clickable: {
27+
type: Boolean,
28+
required: true,
29+
},
2630
},
2731
data() {
2832
return {
@@ -112,7 +116,13 @@ export default {
112116
showArrayIndex: this.showArrayIndex,
113117
showDoubleQuotes: this.showDoubleQuotes,
114118
path: `${this.path}.${key}`,
115-
}
119+
clickable: this.clickable,
120+
},
121+
on: {
122+
fieldClick: (data) => {
123+
this.$emit('fieldClick', data);
124+
},
125+
},
116126
}))
117127
})
118128
}

lib/types/json-object.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export default {
2323
showArrayIndex: Boolean,
2424
showDoubleQuotes: Boolean,
2525
path: String,
26+
clickable: {
27+
type: Boolean,
28+
required: true,
29+
},
2630
},
2731
data() {
2832
return {
@@ -122,8 +126,14 @@ export default {
122126
forceExpand: this.forceExpand,
123127
showArrayIndex: this.showArrayIndex,
124128
showDoubleQuotes: this.showDoubleQuotes,
125-
path: `${this.path}.${key}`
126-
}
129+
path: `${this.path}.${key}`,
130+
clickable: this.clickable,
131+
},
132+
on: {
133+
fieldClick: (data) => {
134+
this.$emit('fieldClick', data);
135+
},
136+
},
127137
}))
128138
}
129139
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-json-viewer",
3-
"version": "2.2.22",
3+
"version": "2.2.23",
44
"description": "vuejs展示json的组件",
55
"main": "vue-json-viewer.js",
66
"files": [
@@ -20,7 +20,7 @@
2020
},
2121
"repository": {
2222
"type": "git",
23-
"url": "git+https://github.com/chenfengjw163/vue-json-viewer.git"
23+
"url": "git+https://github.com/nordcloud/vue-json-viewer.git"
2424
},
2525
"keywords": [
2626
"vue",
@@ -88,4 +88,4 @@
8888
"publishConfig": {
8989
"registry": "https://registry.npmjs.org/"
9090
}
91-
}
91+
}

0 commit comments

Comments
 (0)