Skip to content

Commit 694e82f

Browse files
committed
Unified selectableType, only multiple or single
1 parent 9b2fae8 commit 694e82f

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

example/App.vue

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
<label>selectableType</label>
3030
<select v-model="selectableType">
3131
<option>-</option>
32-
<option>checkbox</option>
33-
<option>radio</option>
32+
<option>single</option>
33+
<option>multiple</option>
3434
</select>
3535
</div>
36+
<div v-if="selectableType === 'single'">
37+
<label>showRadio</label>
38+
<input type="checkbox" v-model="showRadio">
39+
</div>
3640
<div>
3741
<label>path</label>
3842
<input type="text" v-model="path">
@@ -77,6 +81,7 @@
7781
v-model="value"
7882
:path-selectable="((path, data) => typeof data !== 'number')"
7983
:selectable-type="selectableType"
84+
:show-radio="showRadio"
8085
@click="handleClick(...arguments, 'complexTree')"
8186
@change="handleChange">
8287
</vue-json-pretty>
@@ -117,7 +122,8 @@ export default {
117122
}]
118123
},
119124
value: 'res.error',
120-
selectableType: 'radio',
125+
selectableType: 'single',
126+
showRadio: true,
121127
showLength: true,
122128
showDoubleQuotes: true,
123129
showMouseOver: true,
@@ -133,9 +139,9 @@ export default {
133139
watch: {
134140
selectableType (newVal) {
135141
this.renderOK = false
136-
if (newVal === 'radio') {
142+
if (newVal === 'single') {
137143
this.value = 'res.error'
138-
} else if (newVal === 'checkbox') {
144+
} else if (newVal === 'multiple') {
139145
this.value = ['res', 'res.error']
140146
}
141147
// 重新渲染, 因为2中情况的v-model格式不同

src/components/app.vue

Lines changed: 25 additions & 16 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': existCheckbox || existRadio,
5+
'has-selectable-control': isMultiple || showRadio,
66
'is-root': currentDeep === 1,
77
'is-selectable': selectable,
88
'is-mouseover': isMouseover
99
}"
10-
@click.stop="handleClick($event)"
10+
@click.stop="handleClick($event, 'tree')"
1111
@mouseover.stop="handleMouseover"
1212
@mouseout.stop="handleMouseout">
1313
<template v-if="selectable">
14-
<vue-checkbox v-if="existCheckbox" v-model="currentCheckboxVal" @change="handleClick($event, 'checkbox')"></vue-checkbox>
15-
<vue-radio v-else-if="existRadio" v-model="model" @change="handleClick($event, 'radio')" :path="path"></vue-radio>
14+
<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>
1616
</template>
1717

1818
<template v-if="Array.isArray(data) || isObject(data)">
@@ -42,6 +42,7 @@
4242
:path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)"
4343
:path-selectable="pathSelectable"
4444
:selectable-type="selectableType"
45+
:show-radio="showRadio"
4546
:current-key="key"
4647
:current-deep="currentDeep + 1"
4748
@click="handleItemClick"
@@ -118,7 +119,12 @@
118119
// 定义数据层级支持的选中方式, 默认无该功能
119120
selectableType: {
120121
type: String,
121-
default: '' // radio, checkbox
122+
default: '' // ''|multiple|single radio, checkbox
123+
},
124+
// defined radio's view when selectableType=single
125+
showRadio: {
126+
type: Boolean,
127+
default: false
122128
},
123129
// 存在选择功能时, 定义已选中的数据层级
124130
// 多选时为数组['root.a', 'root.b'], 单选时为字符串'root.a'
@@ -155,7 +161,7 @@
155161
computed: {
156162
model: {
157163
get () {
158-
const defaultVal = this.selectableType === 'checkbox' ? [] : this.selectableType === 'radio' ? '' : null
164+
const defaultVal = this.selectableType === 'multiple' ? [] : this.selectableType === 'single' ? '' : null
159165
return this.value || defaultVal
160166
},
161167
set (val) {
@@ -183,31 +189,34 @@
183189
return this.pathSelectable(this.path, this.data)
184190
},
185191
186-
// 存在复选框
187-
existCheckbox () {
188-
return this.selectableType === 'checkbox'
192+
// 多选模式
193+
isMultiple () {
194+
return this.selectableType === 'multiple'
189195
},
190196
191-
existRadio () {
192-
return this.selectableType === 'radio'
197+
// 单选模式
198+
isSingle () {
199+
return this.selectableType === 'single'
193200
}
194201
},
195202
methods: {
196203
/**
197204
* emit click event
198-
* @param {Boolean} emitType
205+
* @param {Boolean} emitType tree/checkbox/radio
199206
*/
200207
handleClick (e, emitType = '') {
201208
this.$emit('click', this.path, this.data)
202-
if (emitType === 'checkbox') {
209+
if (this.isMultiple && emitType === 'checkbox') {
210+
// handle multiple
203211
const index = this.model.findIndex(item => item === this.path)
204212
if (index !== -1) {
205213
this.model.splice(index, 1)
206214
} else {
207215
this.model.push(this.path)
208216
}
209217
this.$emit('change', this.currentCheckboxVal)
210-
} else if (emitType === 'radio') {
218+
} else if (this.isSingle && (emitType === 'radio' || emitType === 'tree')) {
219+
// handle single
211220
if (this.model !== this.path) {
212221
this.model = this.path
213222
this.$emit('change', this.model)
@@ -222,8 +231,8 @@
222231
223232
// handle children's change, and propagation
224233
handleItemChange (val) {
225-
// 不存在选择的时候change事件无意义
226-
if (this.existCheckbox) {
234+
// 存在选择的时候change事件才有意义
235+
if (this.selectableType) {
227236
this.$emit('change', val)
228237
}
229238
},

0 commit comments

Comments
 (0)