forked from vue-generators/vue-form-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfieldInput.vue
181 lines (173 loc) · 4.23 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<template lang="pug">
.wrapper(v-attributes="'wrapper'")
input.form-control(
:id="fieldID",
:type="inputType",
:value="value",
@input="onInput",
@blur="onBlur",
:class="fieldClasses",
@change="schema.onChange || null",
:disabled="disabled",
:accept="fieldOptions.accept",
:alt="fieldOptions.alt",
:autocomplete="fieldOptions.autocomplete",
:checked="fieldOptions.checked",
:dirname="fieldOptions.dirname",
:formaction="fieldOptions.formaction",
:formenctype="fieldOptions.formenctype",
:formmethod="fieldOptions.formmethod",
:formnovalidate="fieldOptions.formnovalidate",
:formtarget="fieldOptions.formtarget",
:height="fieldOptions.height",
:list="fieldOptions.list",
:max="fieldOptions.max",
:maxlength="fieldOptions.maxlength",
:min="fieldOptions.min",
:minlength="fieldOptions.minlength",
:multiple="fieldOptions.multiple",
:name="inputName",
:pattern="fieldOptions.pattern",
:placeholder="placeholder",
:readonly="readonly",
:required="schema.required",
:size="fieldOptions.size",
:src="fieldOptions.src",
:step="fieldOptions.step",
:width="fieldOptions.width",
:files="fieldOptions.files"
v-attributes="'input'")
span.helper(v-if="inputType === 'color' || inputType === 'range'") {{ value }}
</template>
<script>
import abstractField from "../abstractField";
import { debounce, get as objGet, isFunction, isNumber } from "lodash";
import fecha from "fecha";
const DATETIME_FORMATS = {
date: "YYYY-MM-DD",
datetime: "YYYY-MM-DD HH:mm:ss",
"datetime-local": "YYYY-MM-DDTHH:mm:ss"
};
export default {
name: "field-input",
mixins: [abstractField],
computed: {
inputType() {
if (typeof this.fieldOptions.inputType !== "undefined") {
return this.fieldOptions.inputType.toLowerCase();
} else {
console.warn("Missing inputType", this.fieldOptions, this.fieldOptions.inputType);
}
}
},
methods: {
formatValueToModel(value) {
if (value != null) {
switch (this.inputType) {
case "date":
case "datetime":
case "datetime-local":
case "number":
case "range":
// debounce
return (newValue, oldValue) => {
this.debouncedFormatFunc(value, oldValue);
};
}
}
return value;
},
formatDatetimeToModel(newValue, oldValue) {
let defaultFormat = DATETIME_FORMATS[this.inputType];
let m = fecha.parse(newValue, defaultFormat);
if (m !== false) {
if (this.schema.format) {
newValue = fecha.format(m, this.schema.format);
} else {
newValue = m.valueOf();
}
}
this.updateModelValue(newValue, oldValue);
},
formatNumberToModel(newValue, oldValue) {
if (!isNumber(newValue)) {
newValue = NaN;
}
this.updateModelValue(newValue, oldValue);
},
onInput($event) {
let value = $event.target.value;
switch (this.inputType) {
case "number":
case "range":
if (isNumber(parseFloat($event.target.value))) {
value = parseFloat($event.target.value);
}
break;
}
this.value = value;
},
onBlur() {
if (isFunction(this.debouncedFormatFunc)) {
this.debouncedFormatFunc.flush();
}
}
},
mounted() {
switch (this.inputType) {
case "number":
case "range":
this.debouncedFormatFunc = debounce(
(newValue, oldValue) => {
this.formatNumberToModel(newValue, oldValue);
},
parseInt(objGet(this.schema, "debounceFormatTimeout", 1000)),
{
trailing: true,
leading: false
}
);
break;
case "date":
case "datetime":
case "datetime-local":
// wait 1s before calling 'formatDatetimeToModel' to allow user to input data
this.debouncedFormatFunc = debounce(
(newValue, oldValue) => {
this.formatDatetimeToModel(newValue, oldValue);
},
parseInt(objGet(this.schema, "debounceFormatTimeout", 1000)),
{
trailing: true,
leading: false
}
);
break;
}
},
created() {
if (this.inputType === "file") {
console.warn("The 'file' type in input field is deprecated. Use 'file' field instead.");
}
}
};
</script>
<style lang="scss">
.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>