|
2 | 2 | <div
|
3 | 3 | :class="{
|
4 | 4 | 'vjs-tree': true,
|
5 |
| - 'has-selectable-control': existCheckbox || existRadio, |
| 5 | + 'has-selectable-control': isMultiple || showRadio, |
6 | 6 | 'is-root': currentDeep === 1,
|
7 | 7 | 'is-selectable': selectable,
|
8 | 8 | 'is-mouseover': isMouseover
|
9 | 9 | }"
|
10 |
| - @click.stop="handleClick($event)" |
| 10 | + @click.stop="handleClick($event, 'tree')" |
11 | 11 | @mouseover.stop="handleMouseover"
|
12 | 12 | @mouseout.stop="handleMouseout">
|
13 | 13 | <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> |
16 | 16 | </template>
|
17 | 17 |
|
18 | 18 | <template v-if="Array.isArray(data) || isObject(data)">
|
|
42 | 42 | :path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)"
|
43 | 43 | :path-selectable="pathSelectable"
|
44 | 44 | :selectable-type="selectableType"
|
| 45 | + :show-radio="showRadio" |
45 | 46 | :current-key="key"
|
46 | 47 | :current-deep="currentDeep + 1"
|
47 | 48 | @click="handleItemClick"
|
|
118 | 119 | // 定义数据层级支持的选中方式, 默认无该功能
|
119 | 120 | selectableType: {
|
120 | 121 | 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 |
122 | 128 | },
|
123 | 129 | // 存在选择功能时, 定义已选中的数据层级
|
124 | 130 | // 多选时为数组['root.a', 'root.b'], 单选时为字符串'root.a'
|
|
155 | 161 | computed: {
|
156 | 162 | model: {
|
157 | 163 | get () {
|
158 |
| - const defaultVal = this.selectableType === 'checkbox' ? [] : this.selectableType === 'radio' ? '' : null |
| 164 | + const defaultVal = this.selectableType === 'multiple' ? [] : this.selectableType === 'single' ? '' : null |
159 | 165 | return this.value || defaultVal
|
160 | 166 | },
|
161 | 167 | set (val) {
|
|
183 | 189 | return this.pathSelectable(this.path, this.data)
|
184 | 190 | },
|
185 | 191 |
|
186 |
| - // 存在复选框 |
187 |
| - existCheckbox () { |
188 |
| - return this.selectableType === 'checkbox' |
| 192 | + // 多选模式 |
| 193 | + isMultiple () { |
| 194 | + return this.selectableType === 'multiple' |
189 | 195 | },
|
190 | 196 |
|
191 |
| - existRadio () { |
192 |
| - return this.selectableType === 'radio' |
| 197 | + // 单选模式 |
| 198 | + isSingle () { |
| 199 | + return this.selectableType === 'single' |
193 | 200 | }
|
194 | 201 | },
|
195 | 202 | methods: {
|
196 | 203 | /**
|
197 | 204 | * emit click event
|
198 |
| - * @param {Boolean} emitType |
| 205 | + * @param {Boolean} emitType tree/checkbox/radio |
199 | 206 | */
|
200 | 207 | handleClick (e, emitType = '') {
|
201 | 208 | this.$emit('click', this.path, this.data)
|
202 |
| - if (emitType === 'checkbox') { |
| 209 | + if (this.isMultiple && emitType === 'checkbox') { |
| 210 | + // handle multiple |
203 | 211 | const index = this.model.findIndex(item => item === this.path)
|
204 | 212 | if (index !== -1) {
|
205 | 213 | this.model.splice(index, 1)
|
206 | 214 | } else {
|
207 | 215 | this.model.push(this.path)
|
208 | 216 | }
|
209 | 217 | this.$emit('change', this.currentCheckboxVal)
|
210 |
| - } else if (emitType === 'radio') { |
| 218 | + } else if (this.isSingle && (emitType === 'radio' || emitType === 'tree')) { |
| 219 | + // handle single |
211 | 220 | if (this.model !== this.path) {
|
212 | 221 | this.model = this.path
|
213 | 222 | this.$emit('change', this.model)
|
|
222 | 231 |
|
223 | 232 | // handle children's change, and propagation
|
224 | 233 | handleItemChange (val) {
|
225 |
| - // 不存在选择的时候change事件无意义 |
226 |
| - if (this.existCheckbox) { |
| 234 | + // 存在选择的时候change事件才有意义 |
| 235 | + if (this.selectableType) { |
227 | 236 | this.$emit('change', val)
|
228 | 237 | }
|
229 | 238 | },
|
|
0 commit comments