-
Notifications
You must be signed in to change notification settings - Fork 77
/
calcite-date.tsx
330 lines (306 loc) · 9.25 KB
/
calcite-date.tsx
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {
Component,
h,
Prop,
Event,
Element,
Host,
State,
Listen,
Build,
} from "@stencil/core";
import {
parseDateString,
getLocaleFormatData,
DateFormattingData,
} from "../../utils/locale";
import { DateChangeEvent, DateChangeEmitter } from "../../interfaces/Date";
import { getElementDir } from "../../utils/dom";
import { ESCAPE } from "../../utils/keys";
import {
dateFromRange,
inRange,
dateFromISO,
dateToISO,
} from "../../utils/date";
@Component({
tag: "calcite-date",
styleUrl: "calcite-date.scss",
shadow: true,
})
export class CalciteDatePicker {
//--------------------------------------------------------------------------
//
// Element
//
//--------------------------------------------------------------------------
@Element() el: HTMLElement;
//--------------------------------------------------------------------------
//
// Public Properties
//
//--------------------------------------------------------------------------
/** Selected date */
@Prop({ reflect: true, mutable: true }) value?: string;
/** Selected date as full date object*/
@Prop({ mutable: true }) valueAsDate?: Date;
/** Earliest allowed date ("yyyy-mm-dd") */
@Prop() min?: string;
/** Latest allowed date ("yyyy-mm-dd") */
@Prop() max?: string;
/** Expand or collapse when calendar does not have input */
@Prop({ reflect: true }) showCalendar: boolean = false;
/** Localized string for "previous month" */
@Prop() prevMonthLabel?: string = "previous month";
/** Localized string for "next month" */
@Prop() nextMonthLabel?: string = "next month";
/** BCP 47 language tag for desired language and country format */
@Prop() locale?: string = "en-US";
/** Show only calendar popup */
@Prop() noCalendarInput?: boolean = false;
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
@Listen("blur") focusOutHandler() {
this.reset();
}
/**
* Blur doesn't fire properly when there is no shadow dom (ege/IE11)
* Check if the focused element is inside the date picker, if not close
*/
@Listen("focusin", { target: "window" }) focusInHandler(e: FocusEvent) {
if (!this.hasShadow && !this.el.contains(e.srcElement as HTMLElement)) {
this.reset();
}
}
@Listen("keyup") keyDownHandler(e: KeyboardEvent) {
if (e.keyCode === ESCAPE) {
this.reset();
}
}
//--------------------------------------------------------------------------
//
// Events
//
//--------------------------------------------------------------------------
/**
* Trigger calcite date change when a user changes the date.
*/
@Event() calciteDateChange: DateChangeEmitter;
/**
* Active date.
*/
@State() activeDate: Date;
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
this.setupProxyInput();
}
disconnectedCallback() {
this.observer.disconnect();
}
componentWillRender() {
this.syncProxyInputToThis();
}
render() {
const min = dateFromISO(this.min);
const max = dateFromISO(this.max);
const date = dateFromRange(this.valueAsDate, min, max);
const activeDate = this.getActiveDate(date, min, max);
const formattedDate = date ? date.toLocaleDateString(this.locale) : "";
const dir = getElementDir(this.el);
return (
<Host role="application" dir={dir}>
<slot></slot>
{!this.noCalendarInput && (
<div class="date-input-wrapper" role="application">
<calcite-icon icon="calendar" class="calendar-icon" scale="s" />
<input
type="text"
placeholder={this.localeData.placeholder}
value={formattedDate}
class="date-input"
onFocus={() => (this.showCalendar = true)}
onInput={(e) => this.input((e.target as HTMLInputElement).value)}
onBlur={(e) => this.blur(e.target as HTMLInputElement)}
/>
</div>
)}
<div class="calendar-picker-wrapper">
<calcite-date-month-header
activeDate={activeDate}
selectedDate={date || new Date()}
prevMonthLabel={this.prevMonthLabel}
nextMonthLabel={this.nextMonthLabel}
locale={this.locale}
min={min}
max={max}
onCalciteActiveDateChange={(e: DateChangeEvent) => {
this.activeDate = new Date(e.detail);
}}
dir={dir}
/>
<calcite-date-month
min={min}
max={max}
selectedDate={date}
activeDate={activeDate}
locale={this.locale}
onCalciteDateSelect={(e: DateChangeEvent) => {
this.setValue(new Date(e.detail));
this.activeDate = new Date(e.detail);
this.calciteDateChange.emit(new Date(e.detail));
this.reset();
}}
onCalciteActiveDateChange={(e: DateChangeEvent) => {
this.activeDate = new Date(e.detail);
}}
dir={dir}
/>
</div>
</Host>
);
}
//--------------------------------------------------------------------------
//
// Private State/Props
//
//--------------------------------------------------------------------------
private localeData: DateFormattingData = getLocaleFormatData(this.locale);
private hasShadow: boolean = Build.isBrowser && !!document.head.attachShadow;
private inputProxy: HTMLInputElement;
private observer: MutationObserver;
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
/**
* Register slotted date input proxy, or create one if not provided
*/
setupProxyInput() {
// check for a proxy input
this.inputProxy = this.el.querySelector("input");
// if the user didn't pass a proxy input create one for them
if (!this.inputProxy) {
this.inputProxy = document.createElement("input");
try {
this.inputProxy.type = "date";
} catch (e) {
this.inputProxy.type = "text";
}
this.syncProxyInputToThis();
this.el.appendChild(this.inputProxy);
}
this.syncThisToProxyInput();
if (Build.isBrowser) {
this.observer = new MutationObserver(this.syncThisToProxyInput);
this.observer.observe(this.inputProxy, { attributes: true });
}
}
/**
* Update component based on input proxy
*/
syncThisToProxyInput = () => {
this.min = this.inputProxy.min;
this.max = this.inputProxy.max;
const min = dateFromISO(this.min);
const max = dateFromISO(this.max);
const date = dateFromISO(this.inputProxy.value);
this.valueAsDate = dateFromRange(date, min, max);
this.value = dateToISO(this.valueAsDate);
};
/**
* Update input proxy
*/
syncProxyInputToThis = () => {
if (this.inputProxy) {
this.inputProxy.value = this.value || "";
if (this.min) {
this.inputProxy.min = this.min;
}
if (this.max) {
this.inputProxy.max = this.max;
}
}
};
/**
* Set both iso value and date value and update proxy
*/
private setValue(date: Date) {
this.valueAsDate = new Date(date);
this.value = date.toISOString().split("T")[0];
this.syncProxyInputToThis();
}
/**
* Reset active date and close
*/
private reset() {
if (this.valueAsDate) {
this.activeDate = new Date(this.valueAsDate);
}
if (!this.noCalendarInput) {
this.showCalendar = false;
}
}
/**
* If inputted string is a valid date, update value/active
*/
private input(value: string) {
const date = this.getDateFromInput(value);
if (date) {
this.setValue(date);
this.activeDate = date as Date;
this.calciteDateChange.emit(new Date(date));
}
}
/**
* Clean up invalid date from input on blur
*/
private blur(target: HTMLInputElement) {
const date = this.getDateFromInput(target.value);
if (!date && this.valueAsDate) {
target.value = this.valueAsDate.toLocaleDateString(this.locale);
}
}
/**
* Get an active date using the value, or current date as default
*/
private getActiveDate(
value: Date | null,
min: Date | null,
max: Date | null
) {
return (
dateFromRange(this.activeDate, min, max) ||
value ||
dateFromRange(new Date(), min, max)
);
}
/**
* Find a date from input string
* return false if date is invalid, or out of range
*/
private getDateFromInput(value: string): Date | false {
const { separator } = this.localeData;
const { day, month, year } = parseDateString(value, this.locale);
const date = new Date(year, month, day);
const validDate = !isNaN(date.getTime());
const validLength = value.split(separator).filter((c) => c).length > 2;
const validYear = year.toString().length > 3;
if (
validDate &&
validLength &&
validYear &&
inRange(date, this.min, this.max)
) {
return date;
}
return false;
}
}