-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathradio.vue
73 lines (71 loc) · 2.03 KB
/
radio.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
<template>
<span class="vmc-radio" :class="{ inline: inline }">
<input type="radio" :id="id" :value="originValue" v-model="localValue" :disabled="disabled">
<label :for="id" :class="{disabled: disabled}">
<slot></slot>
</label>
<slot name="extra"></slot>
</span>
</template>
<script type="es6">
export default {
props: {
id: {
type: String,
default() {
return 'radio-' + Math.random().toString(36).substr(2, 8);
}
},
value: [String, Number, Boolean],
originValue: [String, Number, Boolean],
disabled: Boolean,
inline: Boolean,
childValues: Array,
childChecked: Array
},
data() {
return {
localValue: this.value,
localChild: this.childChecked
}
},
computed: {
isChecked: {
get() {
return this.localValue === this.originValue;
}
}
},
methods: {
_updateChild() {
if (this.isChecked) {
this.localChild = [].concat(this.childValues);
} else {
this.localChild = [];
}
this.$emit('on-sync-child', this.localChild);
}
},
watch: {
value(value) {
if (value !== this.localValue) {
this.localValue = value;
}
},
localValue(value) {
this._updateChild();
this.$emit('input', value);
},
childChecked(value) {
if (!this.isChecked && value.length) {
this.$nextTick(() => {
this._updateChild();
});
}
}
},
mounted() {
this._updateChild();
}
}
</script>