Skip to content

Commit 53d3867

Browse files
committed
fix dateTimePicker
expand date field formatter methods
1 parent d3faf1c commit 53d3867

File tree

5 files changed

+64
-46
lines changed

5 files changed

+64
-46
lines changed

dev/full/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ module.exports = {
774774
"VueJS"
775775
],
776776
onChanged(model, newVal, oldVal, field) {
777-
console.log(`Model's name changed from ${oldVal} to ${newVal}. Model:`, model);
777+
console.log(`Model's skills changed from ${oldVal} to ${newVal}. Model:`, model);
778778
},
779779
max: 4,
780780
placeholder: "placeholder",

src/fields/abstractDateField.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import abstractField from "./abstractField";
2+
import { defaultsDeep } from "lodash";
3+
4+
import fecha from "fecha";
5+
6+
export default defaultsDeep(abstractField, {
7+
methods: {
8+
9+
formatValueToField(value) {
10+
if (value != null) {
11+
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
12+
return fecha.format(dt, this.getDateFormat());
13+
}
14+
15+
return value;
16+
},
17+
18+
formatValueToModel(value) {
19+
if (value != null) {
20+
let m = fecha.parse(value, this.getDateFormat());
21+
if (this.schema.format)
22+
value = fecha.format(m, this.schema.format);
23+
else
24+
value = m.valueOf();
25+
}
26+
27+
return value;
28+
}
29+
}
30+
});

src/fields/fieldDateTimePicker.vue

+6-23
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<script>
99
/* global $ */
1010
import abstractField from "./abstractField";
11-
import fecha from "fecha";
1211
import { defaults } from "lodash";
12+
import dateFieldHelper from "../utils/dateFieldHelper";
1313
1414
let inputFormat = "YYYY-MM-DD HH:mm:ss";
1515
@@ -25,35 +25,18 @@
2525
return inputFormat;
2626
},
2727
28-
formatValueToField(value) {
29-
if (value != null) {
30-
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
31-
return fecha.format(dt, this.getDateFormat());
32-
}
33-
34-
return value;
35-
},
36-
37-
formatValueToModel(value) {
38-
if (value != null) {
39-
let m = fecha.parse(value, this.getDateFormat());
40-
if (this.schema.format)
41-
value = fecha.format(m, this.schema.format);
42-
else
43-
value = m.valueOf();
44-
}
45-
46-
return value;
47-
}
48-
28+
...dateFieldHelper
4929
},
5030
5131
mounted() {
5232
this.$nextTick(function () {
5333
if (window.$ && window.$.fn.datetimepicker) {
34+
let input = this.$el.querySelector(".form-control");
5435
$(this.$el).datetimepicker(defaults(this.schema.dateTimePickerOptions || {}, {
5536
format: inputFormat
56-
}));
37+
})).on("dp.change", e => {
38+
this.value = this.$el.querySelector(".form-control").value;
39+
});
5740
} else {
5841
console.warn("Bootstrap datetimepicker library is missing. Please download from https://eonasdan.github.io/bootstrap-datetimepicker/ and load the script and CSS in the HTML head section!");
5942
}

src/fields/fieldPikaday.vue

+2-22
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
<script>
66
import abstractField from "./abstractField";
7-
import fecha from "fecha";
87
import { defaults } from "lodash";
8+
import dateFieldHelper from "../utils/dateFieldHelper";
99
1010
let inputFormat = "YYYY-MM-DD";
1111
@@ -24,27 +24,7 @@
2424
return inputFormat;
2525
},
2626
27-
formatValueToField(value) {
28-
if (value != null) {
29-
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
30-
return fecha.format(dt, this.getDateFormat());
31-
}
32-
33-
return value;
34-
},
35-
36-
formatValueToModel(value) {
37-
if (value != null) {
38-
let m = fecha.parse(value, this.getDateFormat());
39-
if (this.schema.format)
40-
value = fecha.format(m, this.schema.format);
41-
else
42-
value = m.valueOf();
43-
}
44-
45-
return value;
46-
}
47-
27+
...dateFieldHelper
4828
},
4929
5030
mounted() {

src/utils/dateFieldHelper.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fecha from "fecha";
2+
3+
export default {
4+
5+
formatValueToField(value) {
6+
if (value != null) {
7+
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
8+
return fecha.format(dt, this.getDateFormat());
9+
}
10+
11+
return value;
12+
},
13+
14+
formatValueToModel(value) {
15+
if (value != null) {
16+
let m = fecha.parse(value, this.getDateFormat());
17+
if (this.schema.format)
18+
value = fecha.format(m, this.schema.format);
19+
else
20+
value = m.valueOf();
21+
}
22+
23+
return value;
24+
}
25+
};

0 commit comments

Comments
 (0)