Skip to content

Commit 2b06d02

Browse files
convert templates components to render functions (and functional when it is relevant)
1 parent 8634437 commit 2b06d02

13 files changed

+697
-291
lines changed

.eslintrc.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
extends: [
3+
'plugin:vue/recommended'
4+
],
5+
rules: {
6+
// override/add rules settings here, such as:
7+
// 'vue/no-unused-vars': 'error'
8+
}
9+
}

example/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ new Vue({
1111
total: 25,
1212
limit: 10,
1313
skip: 0,
14+
success: true,
1415
links: {
1516
previous: undefined,
1617
next: function () {},

lib/json-box.vue

+80-54
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
<template>
2-
<div class="jv-node">
3-
<span class="jv-toggle" :class="{open: !!expand}" v-if="keyName && isObject" @click.stop="toggleNode"></span>
4-
<span class="jv-key" v-if="keyName">
5-
{{keyName}}:
6-
</span>
7-
<commponent
8-
:expand.sync="expand"
9-
:is="`Json${valueType}`"
10-
:json-value="value"
11-
:key-name="keyName"
12-
:sort="sort"
13-
:depth="depth"></commponent>
14-
</div>
15-
</template>
16-
171
<script>
182
import JsonString from './types/json-string'
193
import JsonUndefined from './types/json-undefined'
@@ -27,8 +11,14 @@ export default {
2711
name: 'JsonBox',
2812
inject: ['expandDepth'],
2913
props: {
30-
value: [Object, Array, String, Number, Boolean, Function],
31-
keyName: String,
14+
value: {
15+
type: [Object, Array, String, Number, Boolean, Function],
16+
default: null
17+
},
18+
keyName: {
19+
type: String,
20+
default: ''
21+
},
3222
sort: Boolean,
3323
depth: {
3424
type: Number,
@@ -40,53 +30,89 @@ export default {
4030
expand: true
4131
}
4232
},
33+
mounted() {
34+
this.expand = this.depth >= this.expandDepth ? false : true
35+
},
4336
methods: {
44-
toggleNode() {
37+
toggle() {
4538
this.expand = !this.expand
4639
4740
try {
48-
this.$el.dispatchEvent(new Event("resized"))
41+
this.$el.dispatchEvent(new Event('resized'))
4942
} catch (e) {
5043
// handle IE not supporting Event constructor
51-
var evt = document.createEvent("Event")
52-
evt.initEvent("resized", true, false)
44+
var evt = document.createEvent('Event')
45+
evt.initEvent('resized', true, false)
5346
this.$el.dispatchEvent(evt)
5447
}
5548
}
5649
},
57-
mounted() {
58-
this.expand = this.depth >= this.expandDepth ? false : true
59-
},
60-
computed: {
61-
valueType() {
62-
if (this.value === null || this.value === undefined) {
63-
return 'Undefined'
64-
} else if (Array.isArray(this.value)) {
65-
return 'Array'
66-
} else if (typeof this.value === 'object') {
67-
return 'Object'
68-
} else if (typeof this.value === 'number') {
69-
return 'Number'
70-
} else if (typeof this.value === 'string') {
71-
return 'String'
72-
} else if (typeof this.value === 'boolean') {
73-
return 'Boolean'
74-
} else if (typeof this.value === 'function') {
75-
return 'Function'
76-
}
77-
},
78-
isObject() {
79-
return this.valueType == 'Array' || this.valueType == 'Object'; // eslint-disable-line
50+
render (h) {
51+
let elements = []
52+
let dataType
53+
54+
if (this.value === null || this.value === undefined) {
55+
dataType = JsonUndefined
56+
} else if (Array.isArray(this.value)) {
57+
dataType = JsonArray
58+
} else if (typeof this.value === 'object') {
59+
dataType = JsonObject
60+
} else if (typeof this.value === 'number') {
61+
dataType = JsonNumber
62+
} else if (typeof this.value === 'string') {
63+
dataType = JsonString
64+
} else if (typeof this.value === 'boolean') {
65+
dataType = JsonBoolean
66+
} else if (typeof this.value === 'function') {
67+
dataType = JsonFunction
8068
}
81-
},
82-
components: {
83-
JsonString,
84-
JsonNumber,
85-
JsonBoolean,
86-
JsonObject,
87-
JsonArray,
88-
JsonFunction,
89-
JsonUndefined
69+
70+
if (this.keyName && (this.value && (Array.isArray(this.value) || typeof this.value === 'object'))) {
71+
elements.push(h('span', {
72+
class: {
73+
'jv-toggle': true,
74+
open: !!this.expand
75+
},
76+
on: {
77+
click: this.toggle
78+
}
79+
}))
80+
}
81+
82+
if (this.keyName) {
83+
elements.push(h('span', {
84+
class: {
85+
'jv-key': true
86+
},
87+
domProps: {
88+
innerHTML: `${this.keyName}:`
89+
}
90+
}))
91+
}
92+
93+
elements.push(h(dataType, {
94+
class: {
95+
'jv-push': true
96+
},
97+
props: {
98+
jsonValue: this.value,
99+
keyName: this.keyName,
100+
sort: this.sort,
101+
depth: this.depth,
102+
expand: this.expand
103+
},
104+
on: {
105+
'update:expand': (event) => {
106+
this.expand = event
107+
}
108+
}
109+
}))
110+
111+
return h('div', {
112+
class: {
113+
'jv-node': true
114+
}
115+
}, elements)
90116
}
91117
}
92118
</script>

0 commit comments

Comments
 (0)