-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathfieldRadios.vue
99 lines (93 loc) · 2.81 KB
/
fieldRadios.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
<template lang="pug">
.radio-list(:disabled="disabled", v-attributes="'wrapper'")
label(v-for="item in items", :class="getItemCssClasses(item)", v-attributes="'label'")
input(:id="getFieldID(schema, true)", type="radio", :disabled="isItemDisabled(item)", :name="id", @click="onSelection(item)", :value="getItemValue(item)", :checked="isItemChecked(item)", :class="schema.fieldClasses", :required="schema.required", v-attributes="'input'")
| {{ getItemName(item) }}
</template>
<script>
import { isObject, isFunction, get as objGet } from "lodash";
import abstractField from "../abstractField";
export default {
mixins: [abstractField],
computed: {
items() {
let values = this.schema.values;
if (typeof values == "function") {
return values.apply(this, [this.model, this.schema]);
} else {
return values;
}
},
id() {
return this.getFieldID(this.schema, true);
}
},
methods: {
getItemValue(item) {
if (isObject(item)) {
if (typeof this.schema["radiosOptions"] !== "undefined" && typeof this.schema["radiosOptions"]["value"] !== "undefined") {
return item[this.schema.radiosOptions.value];
} else {
if (typeof item["value"] !== "undefined") {
return item.value;
} else {
throw "`value` is not defined. If you want to use another key name, add a `value` property under `radiosOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
}
}
} else {
return item;
}
},
getItemName(item) {
if (isObject(item)) {
if (typeof this.schema["radiosOptions"] !== "undefined" && typeof this.schema["radiosOptions"]["name"] !== "undefined") {
return item[this.schema.radiosOptions.name];
} else {
if (typeof item["name"] !== "undefined") {
return item.name;
} else {
throw "`name` is not defined. If you want to use another key name, add a `name` property under `radiosOptions` in the schema. https://icebob.gitbooks.io/vueformgenerator/content/fields/radios.html#radios-field-with-object-values";
}
}
} else {
return item;
}
},
getItemCssClasses(item) {
return {
"is-checked": this.isItemChecked(item),
"is-disabled": this.isItemDisabled(item)
};
},
onSelection(item) {
this.value = this.getItemValue(item);
},
isItemChecked(item) {
let currentValue = this.getItemValue(item);
return currentValue === this.value;
},
isItemDisabled(item) {
if (this.disabled) {
return true;
}
let disabled = objGet(item, "disabled", false);
if (isFunction(disabled)) {
return disabled(this.model);
}
return disabled;
}
}
};
</script>
<style lang="scss">
.vue-form-generator .field-radios {
.radio-list {
label {
display: block;
input[type="radio"] {
margin-right: 5px;
}
}
}
}
</style>