forked from Manohar-Gunturu/calendar-lite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
datepicker-lite.js
84 lines (72 loc) · 1.94 KB
/
datepicker-lite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import '@polymer/paper-dropdown-menu/paper-dropdown-menu.js';
import '@polymer/paper-input/paper-input.js';
import './calendar-lite.js';
/**
* @customElement
* @polymer
*/
class DatePickerLite extends PolymerElement {
static get template() {
return html`
<style>
paper-dropdown-menu {
width: 100%;
}
*[hidden] {
display: none;
}
</style>
<paper-dropdown-menu id="ddMenu" label="[[label]]" value="[[readableDate]]" hidden$="[[readonly]]">
<calendar-lite slot="dropdown-content" on-date-change="datePicked">
</calendar-lite>
</paper-dropdown-menu>
<paper-input type="text" label="[[label]]" readonly value="[[readableDate]]" hidden$="[[!readonly]]"></paper-input>
`;
}
static get properties() {
return {
value: {
type: String,
notify: true,
observer: 'valueChanged'
},
date: {
type: Object,
observer: 'dateChanged'
},
readonly: {
type: Boolean,
value: false
},
readableDate: String,
label: String
};
}
_getDateString(date) {
let month = '' + (date.getMonth() + 1);
let day = '' + date.getDate();
let year = date.getFullYear();
month = month.length < 2 ? '0' + month : month;
day = day.length < 2 ? '0' + day : day;
return [year, month, day].join('-');
}
datePicked(event) {
this.set('date', event.detail.date);
}
dateChanged() {
this.$.ddMenu.opened = false;
this.readableDate = this.date.toDateString();
this.dateJustChanged = true;
this.value = this._getDateString(this.date);
}
valueChanged() {
if (this.dateJustChanged) {
this.dateJustChanged = false;
return;
}
this.date = new Date(this.value);
}
}
window.customElements.define('datepicker-lite', DatePickerLite);