2
2
.wrapper
3
3
input.form-control (
4
4
:id ="getFieldID(schema)" ,
5
- :type ="schema.inputType" ,
5
+ :type ="schema.inputType.toLowerCase() " ,
6
6
:value ="value" ,
7
- @input ="value = $event.target.value " ,
7
+ @input ="onInput " ,
8
8
@blur ="onBlur" ,
9
9
:class ="schema.fieldClasses" ,
10
10
@change ="schema.onChange || null" ,
36
36
:step ="schema.step" ,
37
37
:width ="schema.width" ,
38
38
:files ="schema.files" )
39
- span.helper ( v-if ="schema.inputType === 'color' || schema.inputType === 'range'" ) {{ value }}
39
+ span.helper ( v-if ="schema.inputType.toLowerCase() === 'color' || schema.inputType.toLowerCase() === 'range'" ) {{ value }}
40
40
</template >
41
41
42
42
<script >
43
43
import abstractField from " ../abstractField" ;
44
- import { debounce , isFunction } from " lodash" ;
44
+ import { debounce , isFunction , isNumber } from " lodash" ;
45
45
import fecha from " fecha" ;
46
46
47
47
const DATETIME_FORMATS = {
@@ -50,83 +50,85 @@ const DATETIME_FORMATS = {
50
50
" datetime-local" : " YYYY-MM-DDTHH:mm:ss" ,
51
51
};
52
52
53
- let debouncedFormatDateTime = null ;
53
+ const DEBOUNCE_FORMAT_MS = 1000 ;
54
54
55
55
export default {
56
56
mixins: [abstractField],
57
57
methods: {
58
- formatValueToField (value ) {
59
- // TODO: remove this, we shouldn't be formatting the value for date fields as it breaks keyboard input
60
- // if (value != null) {
61
- // let dt;
62
- // switch (this.schema.inputType) {
63
- // case "date":
64
- // dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
65
- // if(isNaN(dt.getTime())) break;
66
- // return fecha.format(dt, "YYYY-MM-DD");
67
- // case "datetime":
68
- // dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
69
- // if(isNaN(dt.getTime())) break;
70
- // return fecha.format(dt, "YYYY-MM-DD HH:mm:ss");
71
- // case "datetime-local":
72
- // dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
73
- // if(isNaN(dt.getTime())) break;
74
- // return fecha.format(dt, "YYYY-MM-DDTHH:mm:ss");
75
- // }
76
- // }
77
-
78
- return value;
79
- },
80
-
81
58
formatValueToModel (value ) {
82
59
if (value != null ) {
83
- // let m;
84
- switch (this .schema .inputType ) {
60
+ switch (this .schema .inputType .toLowerCase ()) {
85
61
case " date" :
86
62
case " datetime" :
87
63
case " datetime-local" :
64
+ case " number" :
65
+ case " range" :
88
66
// debounce
89
-
90
67
return (newValue , oldValue ) => {
91
- debouncedFormatDateTime (value, oldValue);
68
+ this . debouncedFormatFunc (value, oldValue);
92
69
};
93
- case " number" :
94
- return Number (value);
95
- case " range" :
96
- return Number (value);
97
70
}
98
71
}
99
72
100
73
return value;
101
74
},
102
75
formatDatetimeToModel (newValue , oldValue ) {
103
- let defaultFormat = DATETIME_FORMATS [this .schema .inputType ];
76
+ let defaultFormat = DATETIME_FORMATS [this .schema .inputType . toLowerCase () ];
104
77
let m = fecha .parse (newValue, defaultFormat);
105
78
if (m !== false ) {
106
- if (this .schema .format )
79
+ if (this .schema .format ) {
107
80
newValue = fecha .format (m, this .schema .format );
108
- else
81
+ } else {
109
82
newValue = m .valueOf ();
83
+ }
110
84
}
111
85
this .updateModelValue (newValue, oldValue);
112
86
},
87
+ formatNumberToModel (newValue , oldValue ) {
88
+ if (! isNumber (newValue)) {
89
+ newValue = NaN ;
90
+ }
91
+ this .updateModelValue (newValue, oldValue);
92
+ },
93
+ onInput ($event ) {
94
+ let value = $event .target .value ;
95
+ switch (this .schema .inputType .toLowerCase ()) {
96
+ case " number" :
97
+ case " range" :
98
+ if ($event .target .valueAsNumber ) {
99
+ value = $event .target .valueAsNumber ;
100
+ }
101
+ break ;
102
+ }
103
+ this .value = value;
104
+ },
113
105
onBlur () {
114
- if (isFunction (debouncedFormatDateTime)) {
115
- // TODO: flush pending calls immediately on blur
116
- debouncedFormatDateTime .flush ();
106
+ if (isFunction (this .debouncedFormatFunc )) {
107
+ this .debouncedFormatFunc .flush ();
117
108
}
118
109
}
119
110
},
120
111
121
112
mounted () {
122
- switch (this .schema .inputType ) {
113
+ switch (this .schema .inputType .toLowerCase ()) {
114
+ case " number" :
115
+ case " range" :
116
+ this .debouncedFormatFunc = debounce ((newValue , oldValue ) => {
117
+ this .formatNumberToModel (newValue, oldValue);
118
+ }
119
+ , DEBOUNCE_FORMAT_MS , {
120
+ trailing: true ,
121
+ leading: false
122
+ });
123
+ break ;
123
124
case " date" :
124
125
case " datetime" :
125
126
case " datetime-local" :
126
127
// wait 1s before calling 'formatDatetimeToModel' to allow user to input data
127
- debouncedFormatDateTime = debounce ((newValue , oldValue ) => {
128
+ this . debouncedFormatFunc = debounce ((newValue , oldValue ) => {
128
129
this .formatDatetimeToModel (newValue, oldValue);
129
- }, 1000 , {
130
+ }
131
+ , DEBOUNCE_FORMAT_MS , {
130
132
trailing: true ,
131
133
leading: false
132
134
});
@@ -135,8 +137,9 @@ export default {
135
137
},
136
138
137
139
created () {
138
- if (this .schema .inputType == " file" )
140
+ if (this .schema .inputType . toLowerCase () == " file" ) {
139
141
console .warn (" The 'file' type in input field is deprecated. Use 'file' field instead." );
142
+ }
140
143
}
141
144
};
142
145
0 commit comments