-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathu-radios.vue
66 lines (62 loc) · 1.61 KB
/
u-radios.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
<template>
<div class="u-radios">
<u-radio
v-for="(item, index) in formatList"
:key="index"
:label="item.label"
:checked="item.checked"
:disabled="disabled"
@change="valueChanged(item.value)"
/>
</div>
</template>
<script>
export default {
name: 'u-radios',
props: {
value: { type: [String, Number, Boolean], default: '' },
list: { type: Array, default: () => [] },
labelField: { type: String, default: 'label' },
valueField: { type: String, default: 'value' },
disabled: { type: Boolean, default: false }
},
data() {
return {
radioValue: this.value
}
},
computed: {
formatList() {
return this.list.map(item =>
Object.assign({}, item, {
label: item[this.labelField],
value: item[this.valueField],
checked: item[this.valueField] === this.radioValue
})
)
}
},
watch: {
value(val) {
this.radioValue = val
}
},
methods: {
valueChanged(val) {
let oldValue = this.radioValue
this.radioValue = val
this.$emit('change', { oldValue, newValue: this.radioValue })
this.$emit('update:value', this.radioValue) // allow sync
this.$emit('input', this.radioValue) // allow v-modal
}
}
}
</script>
<style lang="scss" scoped>
.u-radios {
display: inline-block;
.u-radio {
padding: 0 5px;
}
}
</style>