-
Notifications
You must be signed in to change notification settings - Fork 0
/
caribou-date-input.html
313 lines (284 loc) · 10.7 KB
/
caribou-date-input.html
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../iron-input/iron-input.html">
<link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<dom-module id="caribou-date-input">
<template>
<style>
:host {
display: inline-block;
}
:host([hidden]) {
display: none !important;
}
input {
font: inherit;
outline: none;
box-shadow: none;
border: none;
width: auto;
text-align: center;
width: 55px;
}
.container {
@apply --layout-horizontal;
}
</style>
<template is="dom-if" if="{{__isDMY}}">
<div class="container">
<iron-input bind-value="{{_sDateD}}" aria-label="2 digits of the Date">
<input max="31" min="1" size="2" type="number" placeholder="DD">
</iron-input>
/
<iron-input bind-value="{{_sDateM}}" aria-label="2 digits of the Month">
<input max="12" min="1" size="2" type="number" placeholder="MM">
</iron-input>
/
<iron-input bind-value="{{dateY}}" aria-label="4 digits of the Year">
<input min="1900" size="4" type="number" placeholder="YYYY">
</iron-input>
</div>
</template>
<template is="dom-if" if="{{__isYMD}}">
<div class="container">
<iron-input bind-value="{{dateY}}" aria-label="4 digits of the Year">
<input min="1900" size="4" type="number" placeholder="YYYY">
</iron-input>
/
<iron-input bind-value="{{_sDateM}}" aria-label="2 digits of the Month">
<input max="12" min="1" size="2" type="number" placeholder="MM">
</iron-input>
/
<iron-input bind-value="{{_sDateD}}" aria-label="2 digits of the Date">
<input max="31" min="1" size="2" type="number" placeholder="DD">
</iron-input>
</div>
</template>
<template is="dom-if" if="{{__isMDY}}">
<div class="container">
<iron-input bind-value="{{_sDateM}}" aria-label="2 digits of the Month">
<input max="12" min="1" size="2" type="number" placeholder="MM">
</iron-input>
/
<iron-input bind-value="{{_sDateD}}" aria-label="2 digits of the Date">
<input max="31" min="1" size="2" type="number" placeholder="DD">
</iron-input>
/
<iron-input bind-value="{{dateY}}" aria-label="4 digits of the Year">
<input min="1900" size="4" type="number" placeholder="YYYY">
</iron-input>
</div>
</template>
</template>
<script>
/**
* `caribou-date-input`
*
* This element is a single line text field following the material design, used to display a date.
*
* This allow you to set the format of the input : DMY, YMD, MDY
* ```html
* <caribou-date-input date-format="YMD"></caribou-date-input>
* ```
*
* @memberof Caribouflex
* @customElement
* @polymer
* @appliesMixin Polymer.IronValidatableBehavior
* @element caribou-date-input
*/
class DateInputElement extends Polymer.mixinBehaviors([Polymer.IronValidatableBehavior], Polymer.Element) {
static get is() {
return 'caribou-date-input';
}
static get properties() {
return {
/**
* This is string date that will be inserted into the input.
* All the number under 10 will be preceded by a 0. (ex: 01)
*/
_sDateD: {
type: String,
observer: "_setDateD"
},
/**
* This is string month that will be inserted into the input.
* All the number under 10 will be preceded by a 0. (ex: 01)
*/
_sDateM: {
type: String,
observer: "_setDateM"
},
/**
* This is string year that will be inserted into the input.
*/
_sDateY: {
type: String
},
/**
* This is the real date number value
*/
dateD: {
type: Number,
notify: true,
observer: "_set_sDateD"
},
/**
* This is the real month number value
*/
dateM: {
type: Number,
notify: true,
observer: "_set_sDateM"
},
/**
* This is the real year number value
*/
dateY: {
type: Number,
notify: true
},
/**
* True if the date is invalid.
* False otherwise
*/
invalidDate: {
type: Boolean,
value: false,
notify: true
},
/**
* This is the format of the date for the input.
* Possible values :
* - DMY
* - YMD
* - MDY
*/
format: {
type: String,
value: "DMY",
observer: "_initFormat"
},
/**
* Property used to display the view DD/MM/YYYY
*/
__isDMY: {
type: Boolean,
value: false,
readOnly: true
},
/**
* Property used to display the view YYYY/MM/DD
*/
__isYMD: {
type: Boolean,
value: false,
readOnly: true
},
/**
* Property used to display the view MM/DD/YYYY
*/
__isMDY: {
type: Boolean,
value: false,
readOnly: true
}
}
}
static get observers() {
return [
"_computeValue(dateD, dateM, dateY)"
];
}
/**
* Observer function called to valide the date everytime the input change
* @param {Number} dateD The date
* @param {Number} dateM The month
* @param {Number} dateY The year
* @private
*/
_computeValue(dateD, dateM, dateY) {
if (dateD !== undefined && dateM !== undefined && dateY !== undefined) {
this._validDate();
}
}
/**
* Function called to valide the date
* @return {Boolean} True if valid false otherwise
* @private
*/
_validDate() {
var date = new Date(this.dateY, this.dateM - 1, this.dateD);
if (this.dateY < 1900 || !date || (date.getMonth() + 1) != this.dateM) {
this.invalidDate = true;
} else
this.invalidDate = false;
return this.invalidDate;
}
/**
* Init the view
* @param {String} format The string format
* @private
*/
_initFormat(format) {
switch (format) {
case "DMY":
this._set__isDMY(true);
this._set__isYMD(false);
this._set__isMDY(false);
break;
case "YMD":
this._set__isYMD(true);
this._set__isDMY(false);
this._set__isMDY(false);
break;
case "MDY":
this._set__isMDY(true);
this._set__isYMD(false);
this._set__isDMY(false);
break;
default:
this.format = "DMY";
}
}
/**
* Observer function used to init the date value when the input value change
* @param {String} value The input date value
* @private
*/
_setDateD(value) {
this.dateD = Number(value);
}
/**
* Observer function used to init the date value when the input value change
* @param {String} value The input month value
* @private
*/
_setDateM(value) {
this.dateM = Number(value);
}
/**
* Observer function used to init the string date value when the real selected date change
* @param {String} value The date value
* @private
*/
_set_sDateD(value) {
this._sDateD = (value < 10 ? '0' : '') + value;
}
/**
* Observer function used to init the string month value when the real selected month change
* @param {String} value The month value
* @private
*/
_set_sDateM(value) {
this._sDateM = (value < 10 ? '0' : '') + value;
}
}
window.customElements.define(DateInputElement.is, DateInputElement);
/**
* @namespace Caribouflex
*/
window.Caribouflex = window.Caribouflex || {};
window.Caribouflex.DateInputElement = DateInputElement;
</script>
</dom-module>