forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldSwitch.vue
138 lines (128 loc) · 3.16 KB
/
fieldSwitch.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<template lang="pug">
label
input(type="checkbox", v-model="value", :autocomplete="fieldOptions.autocomplete", :disabled="disabled", :name="inputName", :id="fieldID")
span.label(:data-on="fieldOptions.textOn || 'On'", :data-off="fieldOptions.textOff || 'Off'", :for="fieldID")
span.handle
</template>
<script>
import abstractField from "../abstractField";
export default {
name: "field-switch",
mixins: [abstractField],
methods: {
formatValueToField(value) {
if (value != null && this.fieldOptions.valueOn) return value === this.fieldOptions.valueOn;
return value;
},
formatValueToModel(value) {
if (value != null && this.fieldOptions.valueOn) {
if (value) return this.fieldOptions.valueOn;
else return this.fieldOptions.valueOff;
}
return value;
}
}
};
</script>
<style lang="scss">
$field-switch-width: 120px;
$field-switch-height: 30px;
.vue-form-generator .field-switch {
.field-wrap label {
position: relative;
display: block;
width: $field-switch-width;
height: $field-switch-height;
padding: 0;
margin: 0 10px 10px 0;
border-radius: $field-switch-height / 2;
box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
cursor: pointer;
}
input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
}
.label {
position: relative;
display: block;
height: inherit;
font-size: 10px;
text-transform: uppercase;
background: #eceeef;
border-radius: inherit;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
}
.label:before,
.label:after {
position: absolute;
top: 50%;
margin-top: -0.5em;
line-height: 1;
-webkit-transition: inherit;
-moz-transition: inherit;
-o-transition: inherit;
transition: inherit;
}
.label:before {
content: attr(data-off);
right: 11px;
color: #aaaaaa;
text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}
.label:after {
content: attr(data-on);
left: 11px;
color: #ffffff;
text-shadow: 0 1px rgba(0, 0, 0, 0.2);
opacity: 0;
}
input:checked ~ .label {
background: #e1b42b;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}
input:checked ~ .label:before {
opacity: 0;
}
input:checked ~ .label:after {
opacity: 1;
}
.handle {
position: absolute;
top: 1px;
left: 1px;
width: $field-switch-height - 2px;
height: $field-switch-height - 2px;
background: linear-gradient(to bottom, #ffffff 40%, #f0f0f0);
background-image: -webkit-linear-gradient(top, #ffffff 40%, #f0f0f0);
border-radius: 100%;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}
.handle:before {
content: "";
position: absolute;
top: 50%;
left: 50%;
margin: -6px 0 0 -6px;
width: 12px;
height: 12px;
background: linear-gradient(to bottom, #eeeeee, #ffffff);
background-image: -webkit-linear-gradient(top, #eeeeee, #ffffff);
border-radius: 6px;
box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
}
input:checked ~ .handle {
left: $field-switch-width - ($field-switch-height - 1px);
left: calc(100% - (#{$field-switch-height} - 1px));
box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
}
/* Transition
========================== */
.label,
.handle {
transition: all 0.3s ease;
}
}
</style>