Skip to content

Commit 1011d45

Browse files
committed
Add new props: showSelectController, selectOnClickNode
1 parent 694e82f commit 1011d45

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

example/App.vue

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@
2828
<div>
2929
<label>selectableType</label>
3030
<select v-model="selectableType">
31-
<option>-</option>
31+
<option label="-"></option>
3232
<option>single</option>
3333
<option>multiple</option>
3434
</select>
3535
</div>
36-
<div v-if="selectableType === 'single'">
37-
<label>showRadio</label>
38-
<input type="checkbox" v-model="showRadio">
36+
<div>
37+
<label>showSelectController</label>
38+
<input type="checkbox" v-model="showSelectController">
39+
</div>
40+
<div>
41+
<label>selectOnClickNode</label>
42+
<input type="checkbox" v-model="selectOnClickNode">
3943
</div>
4044
<div>
4145
<label>path</label>
@@ -78,10 +82,11 @@
7882
:show-double-quotes="showDoubleQuotes"
7983
:show-mouse-over="showMouseOver"
8084
:show-length="showLength"
85+
:select-on-click-node="selectOnClickNode"
8186
v-model="value"
8287
:path-selectable="((path, data) => typeof data !== 'number')"
8388
:selectable-type="selectableType"
84-
:show-radio="showRadio"
89+
:show-select-controller="showSelectController"
8590
@click="handleClick(...arguments, 'complexTree')"
8691
@change="handleChange">
8792
</vue-json-pretty>
@@ -123,10 +128,11 @@ export default {
123128
},
124129
value: 'res.error',
125130
selectableType: 'single',
126-
showRadio: true,
131+
showSelectController: true,
127132
showLength: true,
128133
showDoubleQuotes: true,
129134
showMouseOver: true,
135+
selectOnClickNode: true,
130136
path: 'res',
131137
deep: 4,
132138
itemData: {},

src/components/app.vue

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
<div
33
:class="{
44
'vjs-tree': true,
5-
'has-selectable-control': isMultiple || showRadio,
5+
'has-selectable-control': isMultiple || showSelectController,
66
'is-root': currentDeep === 1,
77
'is-selectable': selectable,
88
'is-mouseover': isMouseover
99
}"
1010
@click.stop="handleClick($event, 'tree')"
1111
@mouseover.stop="handleMouseover"
1212
@mouseout.stop="handleMouseout">
13-
<template v-if="selectable">
13+
<template v-if="showSelectController && selectable">
1414
<vue-checkbox v-if="isMultiple" v-model="currentCheckboxVal" @change="handleClick($event, 'checkbox')"></vue-checkbox>
15-
<vue-radio v-else-if="isSingle && showRadio" v-model="model" @change="handleClick($event, 'radio')" :path="path"></vue-radio>
15+
<vue-radio v-else-if="isSingle" v-model="model" @change="handleClick($event, 'radio')" :path="path"></vue-radio>
1616
</template>
1717

1818
<template v-if="Array.isArray(data) || isObject(data)">
@@ -42,7 +42,8 @@
4242
:path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)"
4343
:path-selectable="pathSelectable"
4444
:selectable-type="selectableType"
45-
:show-radio="showRadio"
45+
:show-select-controller="showSelectController"
46+
:select-on-click-node="selectOnClickNode"
4647
:current-key="key"
4748
:current-deep="currentDeep + 1"
4849
@click="handleItemClick"
@@ -121,11 +122,16 @@
121122
type: String,
122123
default: '' // ''|multiple|single radio, checkbox
123124
},
124-
// defined radio's view when selectableType=single
125-
showRadio: {
125+
// 是否展示左侧选择控件
126+
showSelectController: {
126127
type: Boolean,
127128
default: false
128129
},
130+
// 是否在点击树的时候选中节点
131+
selectOnClickNode: {
132+
type: Boolean,
133+
default: true
134+
},
129135
// 存在选择功能时, 定义已选中的数据层级
130136
// 多选时为数组['root.a', 'root.b'], 单选时为字符串'root.a'
131137
value: {
@@ -206,16 +212,20 @@
206212
*/
207213
handleClick (e, emitType = '') {
208214
this.$emit('click', this.path, this.data)
209-
if (this.isMultiple && emitType === 'checkbox') {
215+
if (this.isMultiple && (emitType === 'checkbox' || (this.selectOnClickNode && emitType === 'tree'))) {
210216
// handle multiple
211217
const index = this.model.findIndex(item => item === this.path)
212218
if (index !== -1) {
213219
this.model.splice(index, 1)
214220
} else {
215221
this.model.push(this.path)
216222
}
223+
224+
if (emitType !== 'checkbox') {
225+
this.currentCheckboxVal = !this.currentCheckboxVal
226+
}
217227
this.$emit('change', this.currentCheckboxVal)
218-
} else if (this.isSingle && (emitType === 'radio' || emitType === 'tree')) {
228+
} else if (this.isSingle && (emitType === 'radio' || (this.selectOnClickNode && emitType === 'tree'))) {
219229
// handle single
220230
if (this.model !== this.path) {
221231
this.model = this.path
@@ -254,6 +264,11 @@
254264
return this.showDoubleQuotes ? `"${key}"` : key
255265
}
256266
},
267+
created () {
268+
if (this.selectableType && !this.selectOnClickNode && !this.showSelectController) {
269+
throw new Error('[vue-json-pretty] error')
270+
}
271+
},
257272
watch: {
258273
deep (newVal) {
259274
this.visible = this.currentDeep <= newVal

0 commit comments

Comments
 (0)