-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMdOption.vue
108 lines (100 loc) · 2.42 KB
/
MdOption.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<template>
<md-menu-item :class="optionClasses" :disabled="isDisabled" @click="setSelection">
<md-checkbox class="md-primary" v-model="isChecked" v-if="MdSelect.multiple" />
<span class="md-list-item-text" ref="text">
<slot />
</span>
</md-menu-item>
</template>
<script>
import MdUuid from 'core/utils/MdUuid'
export default {
name: 'MdOption',
props: {
value: [String, Number, Boolean],
disabled: Boolean
},
inject: {
MdSelect: {},
MdOptgroup: {
default: {}
}
},
data: () => ({
uniqueId: 'md-option-' + MdUuid(),
isSelected: false,
isChecked: false
}),
computed: {
selectValue () {
return this.MdSelect.modelValue
},
isMultiple () {
return this.MdSelect.multiple
},
isDisabled () {
return this.MdOptgroup.disabled || this.disabled
},
key () {
let isSet = (this.value || this.value === 0)
return isSet ? this.value : this.uniqueId
},
inputLabel () {
return this.MdSelect.label
},
optionClasses () {
return {
'md-selected': this.isSelected || this.isChecked
}
}
},
watch: {
inputLabel () {
this.setIsSelected()
}
},
methods: {
getTextContent () {
if (this.$el) {
return this.$el.textContent.trim()
}
const slot = this.$slots.default
return slot ? slot[0].text.trim() : ''
},
setIsSelected () {
this.isSelected = this.inputLabel === this.getTextContent()
},
setSingleSelection () {
this.MdSelect.setValue(this.value)
},
setMultipleSelection () {
this.isChecked = !this.isChecked
this.MdSelect.setMultipleValue(this.value)
},
setSelection () {
if (!this.disabled) {
if (this.isMultiple) {
this.setMultipleSelection()
} else {
this.setSingleSelection()
}
}
},
setItem () {
this.$set(this.MdSelect.items, this.key, this.getTextContent())
}
},
updated () {
this.setItem()
},
created () {
this.setItem()
this.setIsSelected()
if (this.isMultiple && this.selectValue && this.selectValue.length) {
this.isChecked = this.selectValue.includes(this.value)
}
}
}
</script>
<style lang="scss">
</style>