|
4 | 4 |
|
5 | 5 | <script>
|
6 | 6 | import abstractField from "../abstractField";
|
7 |
| -import { defaults } from "lodash"; |
| 7 | +import { defaults, get as objGet } from "lodash"; |
8 | 8 | import dateFieldHelper from "../../utils/dateFieldHelper";
|
9 | 9 |
|
10 | 10 | let inputFormat = "YYYY-MM-DD";
|
11 | 11 |
|
12 | 12 | export default {
|
13 |
| - mixins: [abstractField], |
| 13 | + mixins: [ abstractField ], |
14 | 14 | data() {
|
15 |
| - return { picker: null }; |
| 15 | + return { |
| 16 | + picker: null, |
| 17 | + options: null |
| 18 | + }; |
16 | 19 | },
|
17 | 20 |
|
18 | 21 | methods: {
|
19 | 22 | getDateFormat() {
|
20 |
| - if (this.schema.pikadayOptions && this.schema.pikadayOptions.format) return this.schema.pikadayOptions.format; |
21 |
| - else return inputFormat; |
| 23 | + return objGet(this.schema, 'pikadayOptions.format', inputFormat); |
22 | 24 | },
|
| 25 | + ...dateFieldHelper, |
| 26 | + initialize(options) { |
| 27 | + if(this.picker && this.picker.destroy) { |
| 28 | + // if an existing picker is already set, destroy it first |
| 29 | + this.picker.destroy(); |
| 30 | + } |
23 | 31 |
|
24 |
| - ...dateFieldHelper |
25 |
| - }, |
26 |
| -
|
27 |
| - mounted() { |
28 |
| - this.$nextTick(() => { |
29 |
| - if (window.Pikaday) { |
30 |
| - this.picker = new window.Pikaday( |
31 |
| - defaults(this.schema.pikadayOptions || {}, { |
| 32 | + this.$nextTick(() => { |
| 33 | + if (window.Pikaday) { |
| 34 | + this.options = defaults({}, options, { |
32 | 35 | field: this.$el, // bind the datepicker to a form field
|
33 | 36 | onSelect: () => {
|
34 | 37 | this.value = this.picker.toString();
|
35 | 38 | }
|
36 | 39 | // trigger: , // use a different element to trigger opening the datepicker, see [trigger example][] (default to `field`)
|
37 |
| - }) |
38 |
| - ); |
39 |
| - } else { |
40 |
| - console.warn("Pikaday is missing. Please download from https://github.com/dbushell/Pikaday/ and load the script and CSS in the HTML head section!"); |
41 |
| - } |
42 |
| - }); |
| 40 | + }); |
| 41 | + this.picker = new window.Pikaday(this.options); |
| 42 | + } else { |
| 43 | + console.warn("Pikaday is missing. Please download from https://github.com/dbushell/Pikaday/ and load the script and CSS in the HTML head section!"); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + }, |
| 48 | +
|
| 49 | + mounted() { |
| 50 | + this.initialize(objGet(this.schema, "pikadayOptions", {})); |
43 | 51 | },
|
44 | 52 |
|
45 | 53 | beforeDestroy() {
|
46 |
| - if (this.picker) this.picker.destroy(); |
| 54 | + if (this.picker) { |
| 55 | + this.picker.destroy(); |
| 56 | + } |
47 | 57 | }
|
48 | 58 | };
|
49 | 59 | </script>
|
|
0 commit comments