Skip to content

Commit 1b427d6

Browse files
committed
feat(field-switch): add simple switch field
1 parent ad0ef0c commit 1b427d6

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

src/fields/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import FieldNumber from '@/fields/input/FieldNumber.vue'
99
import FieldSubmit from '@/fields/buttons/FieldSubmit.vue'
1010
import FieldReset from '@/fields/buttons/FieldReset.vue'
1111
import FieldButton from '@/fields/buttons/FieldButton.vue'
12+
import FieldSwitch from '@/fields/input/FieldSwitch.vue'
1213

1314

1415
const fieldComponents = [
1516
FieldText, FieldCheckBox, FieldPassword, FieldSelect, FieldRadio, FieldColor, FieldNumber,
16-
FieldSubmit, FieldReset, FieldButton
17+
FieldSubmit, FieldReset, FieldButton, FieldSwitch
1718
]
1819

1920
export default {

src/fields/input/FieldSwitch.vue

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<template>
2+
<label class="field-switch" :for="formFieldId">
3+
<input :id="formFieldId" type="checkbox" :disabled="isDisabled" @change="onFieldValueChanged">
4+
<span class="slider" />
5+
</label>
6+
</template>
7+
8+
<script>
9+
import { abstractField } from '@/mixins/'
10+
11+
export default {
12+
name: 'FieldSwitch',
13+
mixins: [ abstractField ],
14+
methods: {
15+
formatFieldValue(target) {
16+
return target.checked
17+
}
18+
}
19+
}
20+
</script>
21+
22+
<style>
23+
.vue-form-generator .field-switch {
24+
position: relative;
25+
display: inline-block;
26+
width: 60px;
27+
height: 34px;
28+
}
29+
30+
.vue-form-generator .field-switch input {
31+
opacity: 0;
32+
width: 0;
33+
height: 0;
34+
}
35+
36+
.vue-form-generator .slider {
37+
position: absolute;
38+
cursor: pointer;
39+
top: 0;
40+
left: 0;
41+
right: 0;
42+
bottom: 0;
43+
background-color: #ccc;
44+
-webkit-transition: .4s;
45+
transition: .4s;
46+
border-radius: 34px;
47+
}
48+
49+
.vue-form-generator .slider:before {
50+
position: absolute;
51+
content: '';
52+
height: 26px;
53+
width: 26px;
54+
left: 4px;
55+
bottom: 4px;
56+
background-color: white;
57+
-webkit-transition: .4s;
58+
transition: .4s;
59+
border-radius: 50%;
60+
}
61+
62+
.vue-form-generator input:checked + .slider {
63+
background-color: #2196F3;
64+
}
65+
66+
.vue-form-generator input:focus + .slider {
67+
box-shadow: 0 0 1px #2196F3;
68+
}
69+
70+
.vue-form-generator input:checked + .slider:before {
71+
-webkit-transform: translateX(26px);
72+
-ms-transform: translateX(26px);
73+
transform: translateX(26px);
74+
}
75+
</style>

src/mixins/abstractField.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ export default {
8888

8989
if (isFunction(this.field.onValidated)) {
9090
this.field.onValidated.call(this, this.model, results, this.field)
91-
}
92-
else if (this.field.onValidated !== undefined) {
91+
} else if (this.field.onValidated !== undefined) {
9392
throw new Error('onValidated property must be of type `function`, on ' + this.field.name)
9493
}
9594

@@ -148,6 +147,26 @@ export default {
148147

149148
computed: {
150149

150+
/**
151+
* Compute the id for the current form field
152+
* @returns {string}
153+
*/
154+
formFieldId () {
155+
/** From: vue-form-generator **/
156+
return (this.field.inputName || this.field.label || this.field.model || '')
157+
.toString()
158+
.trim()
159+
.toLowerCase()
160+
// Spaces & underscores to dashes
161+
.replace(/ |_/g, '-')
162+
// Multiple dashes to one
163+
.replace(/-{2,}/g, '-')
164+
// Remove leading & trailing dashes
165+
.replace(/^-+|-+$/g, '')
166+
// Remove anything that isn't a (English/ASCII) letter, number or dash.
167+
.replace(/([^a-zA-Z0-9-]+)/g, '')
168+
},
169+
151170
/**
152171
* Compute all validators that should be present by default.
153172
*

0 commit comments

Comments
 (0)