22.wrapper
33 input.form-control (
44 :id ="getFieldID(schema)" ,
5- :type ="schema.inputType" ,
5+ :type ="schema.inputType.toLowerCase() " ,
66 :value ="value" ,
7- @input ="value = $event.target.value " ,
7+ @input ="onInput " ,
88 @blur ="onBlur" ,
99 :class ="schema.fieldClasses" ,
1010 @change ="schema.onChange || null" ,
3636 :step ="schema.step" ,
3737 :width ="schema.width" ,
3838 :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 }}
4040</template >
4141
4242<script >
4343import abstractField from " ../abstractField" ;
44- import { debounce , isFunction } from " lodash" ;
44+ import { debounce , isFunction , isNumber } from " lodash" ;
4545import fecha from " fecha" ;
4646
4747const DATETIME_FORMATS = {
@@ -50,83 +50,85 @@ const DATETIME_FORMATS = {
5050 " datetime-local" : " YYYY-MM-DDTHH:mm:ss" ,
5151};
5252
53- let debouncedFormatDateTime = null ;
53+ const DEBOUNCE_FORMAT_MS = 1000 ;
5454
5555export default {
5656 mixins: [abstractField],
5757 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-
8158 formatValueToModel (value ) {
8259 if (value != null ) {
83- // let m;
84- switch (this .schema .inputType ) {
60+ switch (this .schema .inputType .toLowerCase ()) {
8561 case " date" :
8662 case " datetime" :
8763 case " datetime-local" :
64+ case " number" :
65+ case " range" :
8866 // debounce
89-
9067 return (newValue , oldValue ) => {
91- debouncedFormatDateTime (value, oldValue);
68+ this . debouncedFormatFunc (value, oldValue);
9269 };
93- case " number" :
94- return Number (value);
95- case " range" :
96- return Number (value);
9770 }
9871 }
9972
10073 return value;
10174 },
10275 formatDatetimeToModel (newValue , oldValue ) {
103- let defaultFormat = DATETIME_FORMATS [this .schema .inputType ];
76+ let defaultFormat = DATETIME_FORMATS [this .schema .inputType . toLowerCase () ];
10477 let m = fecha .parse (newValue, defaultFormat);
10578 if (m !== false ) {
106- if (this .schema .format )
79+ if (this .schema .format ) {
10780 newValue = fecha .format (m, this .schema .format );
108- else
81+ } else {
10982 newValue = m .valueOf ();
83+ }
11084 }
11185 this .updateModelValue (newValue, oldValue);
11286 },
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+ },
113105 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 ();
117108 }
118109 }
119110 },
120111
121112 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 ;
123124 case " date" :
124125 case " datetime" :
125126 case " datetime-local" :
126127 // wait 1s before calling 'formatDatetimeToModel' to allow user to input data
127- debouncedFormatDateTime = debounce ((newValue , oldValue ) => {
128+ this . debouncedFormatFunc = debounce ((newValue , oldValue ) => {
128129 this .formatDatetimeToModel (newValue, oldValue);
129- }, 1000 , {
130+ }
131+ , DEBOUNCE_FORMAT_MS , {
130132 trailing: true ,
131133 leading: false
132134 });
@@ -135,8 +137,9 @@ export default {
135137 },
136138
137139 created () {
138- if (this .schema .inputType == " file" )
140+ if (this .schema .inputType . toLowerCase () == " file" ) {
139141 console .warn (" The 'file' type in input field is deprecated. Use 'file' field instead." );
142+ }
140143 }
141144};
142145
0 commit comments