forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldInput.vue
135 lines (126 loc) · 3.17 KB
/
fieldInput.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
<template lang="pug">
.wrapper
input.form-control(
:id="getFieldID(schema)",
:type="schema.inputType",
:value="value",
@input="value = $event.target.value",
@change="onChange",
:disabled="disabled",
:accept="schema.accept",
:alt="schema.alt",
:autocomplete="schema.autocomplete",
:checked="schema.checked",
:dirname="schema.dirname",
:formaction="schema.formaction",
:formenctype="schema.formenctype",
:formmethod="schema.formmethod",
:formnovalidate="schema.formnovalidate",
:formtarget="schema.formtarget",
:height="schema.height",
:list="schema.list",
:max="schema.max",
:maxlength="schema.maxlength",
:min="schema.min",
:multiple="schema.multiple",
:name="schema.inputName",
:pattern="schema.pattern",
:placeholder="schema.placeholder",
:readonly="schema.readonly",
:required="schema.required",
:size="schema.size",
:src="schema.src",
:step="schema.step",
:width="schema.width",
:files="schema.files")
span.helper(v-if="schema.inputType === 'color' || schema.inputType === 'range'") {{ value }}
</template>
<script>
import abstractField from "../abstractField";
import fecha from "fecha";
export default {
mixins: [ abstractField ],
methods: {
onChange(event){
if (this.schema.inputType === "file") {
this.value = event.target.files;
}
},
formatValueToField(value) {
if (value != null) {
let dt;
switch(this.schema.inputType){
case "date":
dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, "YYYY-MM-DD");
case "datetime":
dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, "YYYY-MM-DD HH:mm:ss");
case "datetime-local":
dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, "YYYY-MM-DDTHH:mm:ss");
}
}
return value;
},
formatValueToModel(value) {
if (value != null) {
let m;
switch (this.schema.inputType){
case "date":
m = fecha.parse(value, "YYYY-MM-DD");
if (m !== false) {
if (this.schema.format)
value = fecha.format(m, this.schema.format);
else
value = m.valueOf();
}
break;
case "datetime":
m = fecha.parse(value, "YYYY-MM-DD HH:mm:ss");
if (m !== false) {
if (this.schema.format)
value = fecha.format(m, this.schema.format);
else
value = m.valueOf();
}
break;
case "datetime-local":
m = fecha.parse(value, "YYYY-MM-DDTHH:mm:ss");
if (m !== false) {
if (this.schema.format)
value = fecha.format(m, this.schema.format);
else
value = m.valueOf();
}
break;
case "number":
return Number(value);
case "range":
return Number(value);
}
}
return value;
}
}
};
</script>
<style lang="sass">
.vue-form-generator .field-input {
.wrapper {
width: 100%;
}
input[type="radio"] {
width: 100%;
}
input[type="color"] {
width: 60px;
}
input[type="range"] {
padding: 0;
}
.helper {
margin: auto 0.5em;
}
}
</style>