-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaribou-datepicker.html
More file actions
289 lines (253 loc) · 8.56 KB
/
Copy pathcaribou-datepicker.html
File metadata and controls
289 lines (253 loc) · 8.56 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-input/paper-input-container.html">
<link rel="import" href="../paper-input/paper-input-error.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../iron-dropdown/iron-dropdown.html">
<link rel="import" href="caribou-date-input.html">
<link rel="import" href="caribou-datepicker-custom-icons.html">
<link rel="import" href="caribou-datepicker-overlay.html">
<dom-module id="caribou-datepicker">
<template>
<style>
:host {
display: block;
}
[slot=dropdown-content] {
border-radius: 3px;
box-shadow: 0px 2px 6px #ccc;
}
#calendarIcon {
@apply --input-calendar-icon;
}
</style>
<paper-input-container id="inputContainer" always-float-label attr-for-value="value" invalid="[[invalid]]">
<label slot="label">{{label}}</label>
<caribou-date-input class="paper-input-input" slot="input" invalid-date="{{invalid}}" date-d="{{date}}" date-m="{{month}}"
date-y="{{year}}" format="[[dateFormat]]"></caribou-date-input>
<paper-icon-button id="calendarIcon" slot="suffix" on-tap="open" icon="caribou-datepicker-icons:calendar"></paper-icon-button>
<paper-input-error slot="add-on">{{errorMessage}}</paper-input-error>
</paper-input-container>
<iron-dropdown id="dropdown">
<div slot="dropdown-content">
<caribou-datepicker-overlay id="datepickerOverlay" horizontal="[[horizontal]]" day="[[day]]" date="[[date]]" month="[[month]]"
year="[[year]]"></caribou-datepicker-overlay>
</div>
</iron-dropdown>
</template>
<script>
/**
* `caribou-datepicker`
* Polymer 2 element used to select a date with a picker following material design guidelines
*
* Material design: [Pickers](https://material.io/guidelines/components/pickers.html#pickers-usage)
*
* This element is a single line text field following the material design,
* and allow you to select a date from an overlay selector.
*
* ```html
* <caribou-datepicker label="My date picker input label"></caribou-datepicker>
* ```
*
* It may include an optional error message
*
* ```html
* <caribou-datepicker label="My date picker input label" error-message="Not valid dude !"></caribou-datepicker>
* ```
*
* By default the date will be displayed following DD-MM-YYYY, but it can be updated :
*
* ```html
* <caribou-datepicker label="My date picker input label" date-format="YMD"></caribou-datepicker>
* ```
*
* By default no value will be set on the input, but it can be initialized with the current date :
*
* ```html
* <caribou-datepicker label="My date picker input label" date-format="YMD" init-with-current-date></caribou-datepicker>
* ```
*
* Also you can set the date directly :
*
* ```html
* <caribou-datepicker label="My date picker input label" date-format="YMD" year="2018" month="1" date="1"></caribou-datepicker>
* ```
*
* You can choose the picker disposition : horizontal or vertical.
* By default it will be displayed vertically, but by added the horizontal property it will be changed.
*
* ```html
* <caribou-datepicker label="My date picker input label" date-format="YMD" year="2018" month="1" date="1" horizontal></caribou-datepicker>
* ```
*
* ### Styling The following custom properties and mixins are available for styling:
*
* Custom property | Description | Default
* ----------------|-------------|----------
* `--input-calendar-icon`| Mixin to customize the input calendar icon | `{}`
*
*
* @memberof Caribouflex
* @customElement
* @polymer
* @demo demo/index.html
* @element caribou-datepicker
*/
class DatepickerElement extends Polymer.Element {
static get is() {
return 'caribou-datepicker';
}
static get properties() {
return {
/**
* Label of the input.
* This is always a floating label.
*/
label: {
type: String,
value: "Date"
},
/**
* The error message if the date is invalid
*/
errorMessage: {
type: String,
value: "Invalid date"
},
/**
* True if the date is invalid.
* False otherwise
*/
invalid: {
type: Boolean,
value: false
},
/**
* The date value
*/
date: Number,
/**
* The month value
* 1-12
*/
month: Number,
/**
* The year value
*/
year: Number,
/**
* The day value
*/
day: Number,
/**
* Used to indicate if the picker should be close when a date is selected
* If true, selected a date will automatically change the input value, and close the picker.
* Warning : If true, the setInputOnDateSelection property is true by default.
*/
closePickerOnDateSelection: {
type: Boolean,
value: false
},
/**
* This will set the input when a date is selected, but this will not close the picker.
*/
setInputOnDateSelection: {
type: Boolean,
value: false
},
/**
* This is the format of the date for the input.
* Possible values :
* - DMY
* - YMD
* - MDY
*/
dateFormat: {
type: String,
value: "DMY",
notify: true
},
/**
* Used to display the picker horizontally
* This can be usefull on mobile landscape.
*/
horizontal: {
type: Boolean,
value: false
},
/**
* If true this will initialize the input with the current date,
* Only if there is no value bind already.
*/
initWithCurrentDate: {
type: Boolean,
value: false,
observer: "_setCurrentDate"
}
}
}
ready() {
super.ready();
this.$.datepickerOverlay.addEventListener("date-selected", this._setDateInput.bind(this));
this.$.datepickerOverlay.addEventListener("ok-clicked", this._setDateInput.bind(this));
this.$.datepickerOverlay.addEventListener("close-overlay", this.closeOverlay.bind(this));
}
static get observers() {
return [
"_setDay(date, month, year)"
];
}
/**
* This function is used to open the overlay.
*/
open() {
if (this.year === undefined || this.month === undefined || this.date === undefined) {
this.$.datepickerOverlay.initCurrentDate();
}
this.$.dropdown.open();
}
_setDay(date, month, year) {
if (date !== undefined && month !== undefined && year !== undefined) {
var dateObj = new Date(year, month - 1, date);
this.day = dateObj.getDay();
}
}
_setDateInput(e) {
if (e.type == "date-selected" && !this.closePickerOnDateSelection && !this.setInputOnDateSelection)
return;
this.month = e.detail.month;
this.year = e.detail.year;
this.date = e.detail.date;
if (e.type == "date-selected" && !this.closePickerOnDateSelection)
return;
this.closeOverlay();
}
_setCurrentDate(initWithCurrentDate) {
if (initWithCurrentDate && this.date === undefined && this.month === undefined && this.year === undefined) {
this.initCurrentDate();
}
}
/**
* This function is used to set the current date.
*/
initCurrentDate() {
var today = new Date();
this.day = today.getDay();
this.month = today.getMonth();
this.year = today.getFullYear();
this.date = today.getDate();
}
/**
* This function is used to close the overlay.
*/
closeOverlay() {
this.$.dropdown.close();
}
}
window.customElements.define(DatepickerElement.is, DatepickerElement);
/**
* @namespace Caribouflex
*/
window.Caribouflex = window.Caribouflex || {};
window.Caribouflex.DatepickerElement = DatepickerElement;
</script>
</dom-module>