Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feature/362-integer-validator

* 'master' of https://github.com/icebob/vue-form-generator:
  fixes vue-generators#361 - use $event.target.valueAsNumber for number/range inputs, debounce `formatValueToModel` for number/range, removed `formatValueToField`

# Conflicts:
#	dist/vfg-core.js
#	dist/vfg.js
  • Loading branch information
zoul0813 committed Dec 13, 2017
2 parents 8d436be + 2b579cd commit 67fc382
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 55 deletions.
6 changes: 3 additions & 3 deletions dist/vfg-core.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/vfg.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/fields/abstractField.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default {
data() {
return {
errors: [],
debouncedValidateFunc: null
debouncedValidateFunc: null,
debouncedFormatFunction: null,
};
},

Expand All @@ -45,7 +46,6 @@ export default {

set(newValue) {
let oldValue = this.value;

newValue = this.formatValueToModel(newValue);

if(isFunction(newValue)) {
Expand Down
95 changes: 49 additions & 46 deletions src/fields/core/fieldInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
.wrapper
input.form-control(
:id="getFieldID(schema)",
:type="schema.inputType",
:type="schema.inputType.toLowerCase()",
:value="value",
@input="value = $event.target.value",
@input="onInput",
@blur="onBlur",
:class="schema.fieldClasses",
@change="schema.onChange || null",
Expand Down Expand Up @@ -36,12 +36,12 @@
:step="schema.step",
:width="schema.width",
:files="schema.files")
span.helper(v-if="schema.inputType === 'color' || schema.inputType === 'range'") {{ value }}
span.helper(v-if="schema.inputType.toLowerCase() === 'color' || schema.inputType.toLowerCase() === 'range'") {{ value }}
</template>

<script>
import abstractField from "../abstractField";
import { debounce, isFunction } from "lodash";
import { debounce, isFunction, isNumber } from "lodash";
import fecha from "fecha";
const DATETIME_FORMATS = {
Expand All @@ -50,83 +50,85 @@ const DATETIME_FORMATS = {
"datetime-local": "YYYY-MM-DDTHH:mm:ss",
};
let debouncedFormatDateTime = null;
const DEBOUNCE_FORMAT_MS = 1000;
export default {
mixins: [abstractField],
methods: {
formatValueToField(value) {
// TODO: remove this, we shouldn't be formatting the value for date fields as it breaks keyboard input
// if (value != null) {
// let dt;
// switch (this.schema.inputType) {
// case "date":
// dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
// if(isNaN(dt.getTime())) break;
// return fecha.format(dt, "YYYY-MM-DD");
// case "datetime":
// dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
// if(isNaN(dt.getTime())) break;
// 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);
// if(isNaN(dt.getTime())) break;
// return fecha.format(dt, "YYYY-MM-DDTHH:mm:ss");
// }
// }
return value;
},
formatValueToModel(value) {
if (value != null) {
// let m;
switch (this.schema.inputType) {
switch (this.schema.inputType.toLowerCase()) {
case "date":
case "datetime":
case "datetime-local":
case "number":
case "range":
// debounce
return (newValue, oldValue) => {
debouncedFormatDateTime(value, oldValue);
this.debouncedFormatFunc(value, oldValue);
};
case "number":
return Number(value);
case "range":
return Number(value);
}
}
return value;
},
formatDatetimeToModel(newValue, oldValue) {
let defaultFormat = DATETIME_FORMATS[this.schema.inputType];
let defaultFormat = DATETIME_FORMATS[this.schema.inputType.toLowerCase()];
let m = fecha.parse(newValue, defaultFormat);
if (m !== false) {
if (this.schema.format)
if (this.schema.format) {
newValue = fecha.format(m, this.schema.format);
else
} 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.schema.inputType.toLowerCase()) {
case "number":
case "range":
if($event.target.valueAsNumber) {
value = $event.target.valueAsNumber;
}
break;
}
this.value = value;
},
onBlur() {
if(isFunction(debouncedFormatDateTime)) {
// TODO: flush pending calls immediately on blur
debouncedFormatDateTime.flush();
if(isFunction(this.debouncedFormatFunc)) {
this.debouncedFormatFunc.flush();
}
}
},
mounted () {
switch(this.schema.inputType) {
switch(this.schema.inputType.toLowerCase()) {
case "number":
case "range":
this.debouncedFormatFunc = debounce((newValue, oldValue) => {
this.formatNumberToModel(newValue, oldValue);
}
, DEBOUNCE_FORMAT_MS, {
trailing: true,
leading: false
});
break;
case "date":
case "datetime":
case "datetime-local":
// wait 1s before calling 'formatDatetimeToModel' to allow user to input data
debouncedFormatDateTime = debounce((newValue, oldValue) => {
this.debouncedFormatFunc = debounce((newValue, oldValue) => {
this.formatDatetimeToModel(newValue, oldValue);
}, 1000, {
}
, DEBOUNCE_FORMAT_MS, {
trailing: true,
leading: false
});
Expand All @@ -135,8 +137,9 @@ export default {
},
created () {
if(this.schema.inputType == "file")
if(this.schema.inputType.toLowerCase() == "file") {
console.warn("The 'file' type in input field is deprecated. Use 'file' field instead.");
}
}
};
Expand Down

0 comments on commit 67fc382

Please sign in to comment.