form-radio.vue
2.25 KB
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
<template>
<fieldset :class="['form-group',this.stacked?'custom-controls-stacked':'',inputState]">
<label :class="['custom-control','custom-radio']" v-for="item in items">
<input
v-model="localValue"
class="custom-control-input"
type="radio"
:id="item.id"
:name="name"
:value="item[valueKey]"
:disabled="item.disabled"
>
<span class="custom-control-indicator"></span>
<span class="custom-control-description">{{item[textKey]}}</span>
</label>
</fieldset>
</template>
<script>
import {uniqueId} from '../utils/helpers';
export default {
data() {
return {
localValue: this.value
};
},
computed: {
inputState() {
return this.state ? `has-${this.state}` : '';
}
},
props: {
value: {
default: null
},
valueKey: {
type: String,
default: 'value'
},
textKey: {
type: String,
default: 'text'
},
name: {
type: String,
default: uniqueId
},
items: {
type: Array,
default: () => [],
required: true
},
stacked: {
type: Boolean,
default: false
},
state: {
type: String,
default: null
},
returnObject: {
type: Boolean,
default: false
}
},
watch: {
localValue(value, old_value) {
if (value === old_value) {
return;
}
if (this.returnObject) {
this.items.forEach(item => {
if (item.value === value) {
value = item;
}
});
}
this.$emit('input', value);
}
}
};
</script>