Skip to content

Commit 0d48ac0

Browse files
committed
feat Add an argument(defaultFormattedResult) to CustomFormatter prop.
1 parent 117ac50 commit 0d48ac0

File tree

5 files changed

+98
-48
lines changed

5 files changed

+98
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default {
6868
| showSelectController | higher | whether to show the select controller at left | boolean | false |
6969
| selectOnClickNode | higher | whether to change selected value when click node | boolean | true |
7070
| highlightSelectedNode | higher | highlight current node when selected | boolean | true |
71-
| customValueFormatter | higher | a function that can return different html or strings to display for values in the data. If it returns null or empty, the default formatter is used | Function | false |
71+
| customValueFormatter | higher | a function that can return different html or strings to display for values in the data. | Function(data, key, parent, defaultFormatted) | - |
7272

7373
## Events
7474

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default {
6969
| showSelectController | 高级 | 是否展示选择控制器 | boolean | false |
7070
| selectOnClickNode | 高级 | 是否在点击节点的时候触发v-model双向绑定 | boolean | true |
7171
| highlightSelectedNode | 高级 | 是否高亮已选项 | boolean | true |
72+
| customValueFormatter | 高级 | 可以进行值的自定义渲染 | Function(data, key, parent, defaultFormatted) | - |
7273

7374
## Events
7475

example/App.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<label>collapsedOnClickBrackets</label>
2929
<input type="checkbox" v-model="collapsedOnClickBrackets">
3030
</div>
31-
<div>
32-
<label>use custom link formatter</label>
31+
<div>
32+
<label>use custom formatter</label>
3333
<input type="checkbox" v-model="useCustomLinkFormatter">
3434
</div>
3535
<div>
@@ -118,6 +118,10 @@
118118
<option :value="4">4</option>
119119
</select>
120120
</div>
121+
<div>
122+
<label>use custom formatter</label>
123+
<input type="checkbox" v-model="useCustomLinkFormatter">
124+
</div>
121125
</div>
122126
<h3>v-model:</h3>
123127
<div>{{value}}</div>
@@ -143,6 +147,7 @@
143147
:path-selectable="((path, data) => typeof data !== 'number')"
144148
:selectable-type="selectableType"
145149
:show-select-controller="showSelectController"
150+
:custom-value-formatter="useCustomLinkFormatter ? customLinkFormatter : null"
146151
@click="handleClick(...arguments, 'complexTree')"
147152
@change="handleChange">
148153
</vue-json-pretty>
@@ -236,11 +241,11 @@ export default {
236241
handleChange (newVal, oldVal) {
237242
console.log('newVal: ', newVal, ' oldVal: ', oldVal)
238243
},
239-
customLinkFormatter(data) {
244+
customLinkFormatter (data, key, parent, defaultFormatted) {
240245
if (data.startsWith('http://')) {
241246
return `<a style="color:red;" href="${data}">"${data}"</a>`;
242247
} else {
243-
return null;
248+
return defaultFormatted;
244249
}
245250
}
246251
}

src/components/app.vue

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@
1111
}"
1212
@click="handleClick"
1313
@mouseover.stop="handleMouseover"
14-
@mouseout.stop="handleMouseout">
14+
@mouseout.stop="handleMouseout"
15+
>
1516
<template v-if="showSelectController && selectable">
16-
<vue-checkbox v-if="isMultiple" v-model="currentCheckboxVal" @change="handleValueChange('checkbox')"></vue-checkbox>
17-
<vue-radio v-else-if="isSingle" v-model="model" @change="handleValueChange('radio')" :path="path"></vue-radio>
17+
<vue-checkbox
18+
v-if="isMultiple"
19+
v-model="currentCheckboxVal"
20+
@change="handleValueChange('checkbox')"
21+
/>
22+
<vue-radio
23+
v-else-if="isSingle"
24+
v-model="model"
25+
:path="path"
26+
@change="handleValueChange('radio')"
27+
/>
1828
</template>
1929

2030
<template v-if="Array.isArray(data) || isObject(data)">
@@ -24,19 +34,26 @@
2434
:data="data"
2535
:show-length="showLength"
2636
:collapsed-on-click-brackets="collapsedOnClickBrackets"
27-
:show-comma="notLastKey">
28-
<span v-if="currentDeep > 1 && !Array.isArray(parentData)" class="vjs-key">{{ keyFormatter(currentKey) }}:</span>
37+
:show-comma="notLastKey"
38+
>
39+
<span
40+
v-if="currentDeep > 1 && !Array.isArray(parentData)"
41+
class="vjs-key"
42+
>
43+
{{ keyFormatter(currentKey) }}:
44+
</span>
2945
</brackets-left>
3046

3147
<!-- 数据内容, data 为对象时, key 表示键名, 为数组时表示索引 -->
3248
<div
3349
v-for="(item, key) in data"
3450
v-show="visible"
51+
:key="key"
3552
:class="{
3653
'vjs-tree__content': true,
3754
'has-line': showLine
3855
}"
39-
:key="key">
56+
>
4057
<vue-json-pretty
4158
v-model="model"
4259
:parent-data="data"
@@ -57,17 +74,17 @@
5774
:current-deep="currentDeep + 1"
5875
:custom-value-formatter="customValueFormatter"
5976
@click="handleItemClick"
60-
@change="handleItemChange">
61-
</vue-json-pretty>
77+
@change="handleItemChange"
78+
/>
6279
</div>
6380

6481
<!-- 右闭合 -->
6582
<brackets-right
6683
:visible.sync="visible"
6784
:data="data"
6885
:collapsed-on-click-brackets="collapsedOnClickBrackets"
69-
:show-comma="notLastKey">
70-
</brackets-right>
86+
:show-comma="notLastKey"
87+
/>
7188
</template>
7289

7390
<simple-text
@@ -77,7 +94,8 @@
7794
:show-comma="notLastKey"
7895
:parent-data="parentData"
7996
:data="data"
80-
:current-key="currentKey">
97+
:current-key="currentKey"
98+
>
8199
<span
82100
v-if="parentData && currentKey && !Array.isArray(parentData)"
83101
class="vjs-key"
@@ -97,7 +115,7 @@
97115
import { getDataType } from 'src/utils'
98116
99117
export default {
100-
name: 'vue-json-pretty',
118+
name: 'VueJsonPretty',
101119
components: {
102120
SimpleText,
103121
VueCheckbox,
@@ -108,7 +126,10 @@
108126
props: {
109127
/* outer props */
110128
// 当前树的数据
111-
data: {},
129+
data: {
130+
type: [String, Number, Boolean, Array, Object],
131+
default: null
132+
},
112133
// 定义树的深度, 大于该深度的子树将不被展开
113134
deep: {
114135
type: Number,
@@ -183,14 +204,20 @@
183204
184205
/* inner props */
185206
// 当前树的父级数据
186-
parentData: {},
207+
parentData: {
208+
type: [String, Number, Boolean, Array, Object],
209+
default: null
210+
},
187211
// 当前树的深度, 以根节点作为0开始, 所以第一层树的深度为1, 递归逐次递增
188212
currentDeep: {
189213
type: Number,
190214
default: 1
191215
},
192216
// 当前树的数据 data 为数组时 currentKey 表示索引, 为对象时表示键名
193-
currentKey: [Number, String]
217+
currentKey: {
218+
type: [Number, String],
219+
default: ''
220+
}
194221
/* outer props */
195222
},
196223
data () {
@@ -219,6 +246,7 @@
219246
let arr = Object.keys(this.parentData)
220247
return arr[arr.length - 1]
221248
}
249+
return ''
222250
},
223251
224252
// 是否不是最后一项
@@ -256,6 +284,19 @@
256284
return error ? 'When selectableType is not null, selectOnClickNode and showSelectController cannot be false at the same time, because this will cause the selection to fail.' : ''
257285
}
258286
},
287+
watch: {
288+
deep (newVal) {
289+
this.visible = this.currentDeep <= newVal
290+
},
291+
propsError: {
292+
handler (message) {
293+
if (message) {
294+
throw new Error(`[vue-json-pretty] ${message}`)
295+
}
296+
},
297+
immediate: true
298+
}
299+
},
259300
methods: {
260301
handleValueChange (emitType) {
261302
if (this.isMultiple && (emitType === 'checkbox' || emitType === 'tree')) {
@@ -334,19 +375,6 @@
334375
// 因为是递归组件,因此错误只对外暴露一次,子组件的错误不再对外传递
335376
errorCaptured () {
336377
return false
337-
},
338-
watch: {
339-
deep (newVal) {
340-
this.visible = this.currentDeep <= newVal
341-
},
342-
propsError: {
343-
handler (message) {
344-
if (message) {
345-
throw new Error(`[vue-json-pretty] ${message}`)
346-
}
347-
},
348-
immediate: true
349-
}
350378
}
351379
}
352380
</script>

src/components/simple-text.vue

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<template>
22
<div>
3-
<slot></slot>
4-
<span v-if="customValueFormatter" :class="valueClass" v-html="customFormatter(data)"/>
5-
<span v-else :class="valueClass">{{ defaultFormatter(data) }}</span>
3+
<slot />
4+
<span
5+
v-if="customValueFormatter"
6+
:class="valueClass"
7+
v-html="customFormatter(data)"
8+
/>
9+
<span
10+
v-else
11+
:class="valueClass"
12+
>
13+
{{ defaultFormatter(data) }}
14+
</span>
615
</div>
716
</template>
817

@@ -12,11 +21,23 @@
1221
export default {
1322
props: {
1423
showDoubleQuotes: Boolean,
15-
parentData: {},
16-
data: {},
24+
parentData: {
25+
type: [String, Number, Boolean, Array, Object],
26+
default: null
27+
},
28+
data: {
29+
type: [String, Number, Boolean],
30+
default: ''
31+
},
1732
showComma: Boolean,
18-
currentKey: [Number, String],
19-
customValueFormatter: Function
33+
currentKey: {
34+
type: [Number, String],
35+
default: ''
36+
},
37+
customValueFormatter: {
38+
type: Function,
39+
default: null
40+
},
2041
},
2142
computed: {
2243
valueClass () {
@@ -27,11 +48,6 @@
2748
dataType () {
2849
return getDataType(this.data)
2950
},
30-
31-
// 父级数据类型
32-
parentDataType () {
33-
return getDataType(this.parentData)
34-
}
3551
},
3652
methods: {
3753
defaultFormatter (data) {
@@ -42,9 +58,9 @@
4258
},
4359
4460
customFormatter (data) {
45-
return this.customValueFormatter(
46-
data, this.currentKey, this.parentData
47-
) || this.defaultFormatter(data)
61+
return this.customValueFormatter
62+
? this.customValueFormatter(data, this.currentKey, this.parentData, this.defaultFormatter(data))
63+
: this.defaultFormatter(data)
4864
}
4965
}
5066
}

0 commit comments

Comments
 (0)