-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathfieldInput.vue
205 lines (196 loc) · 4.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<template lang="pug">
.wrapper(v-attributes="'wrapper'")
input.form-control(
:id="getFieldID(schema)",
:type="inputType",
:value="value",
@input="onInput",
@blur="onBlur",
:class="schema.fieldClasses",
@change="schema.onChange || null",
: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",
:minlength="schema.minlength",
: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"
v-attributes="'input'")
span.helper(v-if="schema.inputType.toLowerCase() === 'color' || schema.inputType.toLowerCase() === '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 {
mixins: [abstractField],
computed: {
inputType() {
if(this.schema && this.schema.inputType === "datetime") {
// convert "datetime" to "datetime-local" (datetime deprecated in favor of "datetime-local")
// ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime
return "datetime-local";
}
return this.schema.inputType;
}
},
methods: {
formatValueToModel(value) {
if (value != null) {
switch (this.schema.inputType.toLowerCase()) {
case "date":
case "datetime":
case "datetime-local":
case "number":
case "range":
// debounce
return (newValue, oldValue) => {
this.debouncedFormatFunc(value, oldValue);
};
}
}
return value;
},
formatValueToField(value) {
switch(this.schema.inputType.toLowerCase()) {
case "date":
case "datetime":
case "datetime-local":
return this.formatDatetimeValueToField(value);
}
return value;
},
formatDatetimeToModel(newValue, oldValue) {
let defaultFormat = DATETIME_FORMATS[this.schema.inputType.toLowerCase()];
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);
},
formatDatetimeValueToField(value) {
if(value === null || undefined === value) {
return null;
}
let defaultFormat = DATETIME_FORMATS[this.schema.inputType.toLowerCase()];
let m = value;
if(!isNumber(value)) {
m = fecha.parse(value, defaultFormat);
}
if(m !== false) {
return fecha.format(m, defaultFormat);
}
return value;
},
formatNumberToModel(newValue, oldValue) {
if (!isNumber(newValue)) {
newValue = NaN;
}
this.updateModelValue(newValue, oldValue);
},
onInput($event) {
let value = $event.target.value;
switch (this.schema.inputType.toLowerCase()) {
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.schema.inputType.toLowerCase()) {
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.schema.inputType.toLowerCase() === "file") {
console.warn("The 'file' type in input field is deprecated. Use 'upload' 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>