-
Notifications
You must be signed in to change notification settings - Fork 2
/
date-time-input.ts
722 lines (623 loc) · 18.7 KB
/
date-time-input.ts
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import { type ComplexAttributeConverter, html } from 'lit';
import { property } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import {
addKeybindings,
arrowDown,
arrowLeft,
arrowRight,
arrowUp,
ctrlKey,
} from '../common/controllers/key-bindings.js';
import { blazorTwoWayBind } from '../common/decorators/blazorTwoWayBind.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import messages from '../common/localization/validation-en.js';
import type { AbstractConstructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { formatString, partNameMap } from '../common/util.js';
import type { Validator } from '../common/validators.js';
import type { IgcInputEventMap } from '../input/input-base.js';
import {
IgcMaskInputBaseComponent,
type MaskRange,
} from '../mask-input/mask-input-base.js';
import {
DatePart,
type DatePartDeltas,
type DatePartInfo,
DateParts,
DateTimeUtil,
} from './date-util.js';
export interface IgcDateTimeInputEventMap
extends Omit<IgcInputEventMap, 'igcChange'> {
igcChange: CustomEvent<Date | null>;
}
const converter: ComplexAttributeConverter<Date | null> = {
fromAttribute: (value: string) =>
!value ? null : DateTimeUtil.parseIsoDate(value),
toAttribute: (value: Date) => value.toISOString(),
};
/**
* A date time input is an input field that lets you set and edit the date and time in a chosen input element
* using customizable display and input formats.
*
* @element igc-date-time-input
*
* @slot prefix - Renders content before the input.
* @slot suffix - Renders content after input.
* @slot helper-text - Renders content below the input.
*
* @fires igcInput - Emitted when the control input receives user input.
* @fires igcChange - Emitted when the control's checked state changes.
* @fires igcFocus - Emitted when the control gains focus.
* @fires igcBlur - Emitted when the control loses focus.
*
* @csspart container - The main wrapper that holds all main input elements.
* @csspart input - The native input element.
* @csspart label - The native label element.
* @csspart prefix - The prefix wrapper.
* @csspart suffix - The suffix wrapper.
* @csspart helper-text - The helper text wrapper.
*/
export default class IgcDateTimeInputComponent extends EventEmitterMixin<
IgcDateTimeInputEventMap,
AbstractConstructor<IgcMaskInputBaseComponent>
>(IgcMaskInputBaseComponent) {
public static readonly tagName = 'igc-date-time-input';
/* blazorSuppress */
public static register() {
registerComponent(IgcDateTimeInputComponent);
}
protected override validators: Validator<this>[] = [
{
key: 'valueMissing',
message: messages.required,
isValid: () => (this.required ? !!this.value : true),
},
{
key: 'rangeUnderflow',
message: () => formatString(messages.min, this.min),
isValid: () =>
this.min
? !DateTimeUtil.lessThanMinValue(
this.value || new Date(),
this.min,
this.hasTimeParts,
this.hasDateParts
)
: true,
},
{
key: 'rangeOverflow',
message: () => formatString(messages.max, this.max),
isValid: () =>
this.max
? !DateTimeUtil.greaterThanMaxValue(
this.value || new Date(),
this.max,
this.hasTimeParts,
this.hasDateParts
)
: true,
},
];
protected _defaultMask!: string;
protected _value: Date | null = null;
private _oldValue: Date | null = null;
private _inputDateParts!: DatePartInfo[];
private _inputFormat!: string;
private _datePartDeltas: DatePartDeltas = {
date: 1,
month: 1,
year: 1,
hours: 1,
minutes: 1,
seconds: 1,
};
/**
* The date format to apply on the input.
* @attr input-format
*/
@property({ attribute: 'input-format' })
public get inputFormat(): string {
return this._inputFormat || this._defaultMask;
}
public set inputFormat(val: string) {
if (val) {
this.setMask(val);
this._inputFormat = val;
if (this.value) {
this.updateMask();
}
}
}
/**
* The value of the input.
* @attr
*/
public get value(): Date | null {
return this._value;
}
@property({ converter: converter })
@blazorTwoWayBind('igcChange', 'detail')
public set value(val: Date | null) {
this._value = val
? DateTimeUtil.isValidDate(val)
? val
: DateTimeUtil.parseIsoDate(val)
: null;
this.updateMask();
this.updateFormValue();
this.updateValidity();
this.setInvalidState();
}
protected updateFormValue() {
this._value
? this.setFormValue(this._value.toISOString())
: this.setFormValue(null);
}
/**
* The minimum value required for the input to remain valid.
* @attr
*/
@property({ converter: converter })
public min!: Date;
/**
* The maximum value required for the input to remain valid.
* @attr
*/
@property({ converter: converter })
public max!: Date;
/**
* The minimum value required for the input to remain valid.
* @attr min-value
*
* @deprecated since v4.4.0. Use the `min` property instead.
*/
@property({ attribute: 'min-value', converter: converter })
public set minValue(value: Date) {
this.min = value;
}
public get minValue() {
return this.min;
}
/**
* The maximum value required for the input to remain valid.
* @attr max-value
*
* @deprecated since v4.4.0. Use the `max` property instead.
*/
@property({ attribute: 'max-value', converter: converter })
public set maxValue(value: Date) {
this.max = value;
}
public get maxValue() {
return this.max;
}
/**
* Format to display the value in when not editing.
* Defaults to the input format if not set.
* @attr display-format
*/
@property({ attribute: 'display-format' })
public displayFormat!: string;
/**
* Delta values used to increment or decrement each date part on step actions.
* All values default to `1`.
*/
@property({ attribute: false })
public spinDelta!: DatePartDeltas;
/**
* Sets whether to loop over the currently spun segment.
* @attr spin-loop
*/
@property({ type: Boolean, attribute: 'spin-loop' })
public spinLoop = true;
/**
* The locale settings used to display the value.
* @attr
*/
@property()
public locale = 'en';
@watch('locale', { waitUntilFirstUpdate: true })
protected setDefaultMask(): void {
if (!this._inputFormat) {
this.updateDefaultMask();
this.setMask(this._defaultMask);
}
if (this.value) {
this.updateMask();
}
}
@watch('displayFormat', { waitUntilFirstUpdate: true })
protected setDisplayFormat(): void {
if (this.value) {
this.updateMask();
}
}
@watch('prompt', { waitUntilFirstUpdate: true })
protected promptChange(): void {
if (!this.prompt) {
this.prompt = this.parser.prompt;
} else {
this.parser.prompt = this.prompt;
}
}
@watch('min', { waitUntilFirstUpdate: true })
@watch('max', { waitUntilFirstUpdate: true })
protected constraintChange() {
this.updateValidity();
}
private get hasDateParts(): boolean {
const parts =
this._inputDateParts ||
DateTimeUtil.parseDateTimeFormat(this.inputFormat);
return parts.some(
(p) =>
p.type === DateParts.Date ||
p.type === DateParts.Month ||
p.type === DateParts.Year
);
}
private get hasTimeParts(): boolean {
const parts =
this._inputDateParts ||
DateTimeUtil.parseDateTimeFormat(this.inputFormat);
return parts.some(
(p) =>
p.type === DateParts.Hours ||
p.type === DateParts.Minutes ||
p.type === DateParts.Seconds
);
}
private get targetDatePart(): DatePart | undefined {
let result: DatePart | undefined;
if (this.focused) {
const partType = this._inputDateParts.find(
(p) =>
p.start <= this.inputSelection.start &&
this.inputSelection.start <= p.end &&
p.type !== DateParts.Literal
)?.type as string as DatePart;
if (partType) {
result = partType;
}
} else if (this._inputDateParts.some((p) => p.type === DateParts.Date)) {
result = DatePart.Date;
} else if (this._inputDateParts.some((p) => p.type === DateParts.Hours)) {
result = DatePart.Hours;
} else {
result = this._inputDateParts[0].type as string as DatePart;
}
return result;
}
private get datePartDeltas(): DatePartDeltas {
return Object.assign({}, this._datePartDeltas, this.spinDelta);
}
constructor() {
super();
addKeybindings(this, {
skip: () => this.readOnly,
bindingDefaults: { preventDefault: true },
})
.set([ctrlKey, ';'], () => {
this.value = new Date();
})
.set(arrowUp, this.keyboardSpin.bind(this, 'up'))
.set(arrowDown, this.keyboardSpin.bind(this, 'down'))
.set([ctrlKey, arrowLeft], this.navigateParts.bind(this, 0))
.set([ctrlKey, arrowRight], this.navigateParts.bind(this, 1));
}
public override connectedCallback() {
super.connectedCallback();
this.updateDefaultMask();
this.setMask(this.inputFormat);
this.updateValidity();
if (this.value) {
this.updateMask();
}
}
/** Increments a date/time portion. */
public stepUp(datePart?: DatePart, delta?: number): void {
const targetPart = datePart || this.targetDatePart;
if (!targetPart) {
return;
}
const { start, end } = this.inputSelection;
const newValue = this.trySpinValue(targetPart, delta);
this.value = newValue;
this.updateComplete.then(() => this.input.setSelectionRange(start, end));
}
/** Decrements a date/time portion. */
public stepDown(datePart?: DatePart, delta?: number): void {
const targetPart = datePart || this.targetDatePart;
if (!targetPart) {
return;
}
const { start, end } = this.inputSelection;
const newValue = this.trySpinValue(targetPart, delta, true);
this.value = newValue;
this.updateComplete.then(() => this.input.setSelectionRange(start, end));
}
/** Clears the input element of user input. */
public clear(): void {
this.maskedValue = '';
this.value = null;
}
protected updateMask() {
if (this.focused) {
this.maskedValue = this.getMaskedValue();
} else {
if (!DateTimeUtil.isValidDate(this.value)) {
this.maskedValue = '';
return;
}
const format = this.displayFormat || this.inputFormat;
if (this.displayFormat) {
this.maskedValue = DateTimeUtil.formatDate(
this.value,
this.locale,
format,
true
);
} else if (this.inputFormat) {
this.maskedValue = DateTimeUtil.formatDate(
this.value,
this.locale,
format
);
} else {
this.maskedValue = this.value.toLocaleString();
}
}
}
protected handleChange() {
this.emitEvent('igcChange', { detail: this.value });
}
protected override handleInput() {
this.emitEvent('igcInput', { detail: this.value?.toString() });
}
protected handleDragLeave() {
if (!this.focused) {
this.updateMask();
}
}
protected handleDragEnter() {
if (!this.focused) {
this.maskedValue = this.getMaskedValue();
}
}
protected async updateInput(string: string, range: MaskRange) {
const { value, end } = this.parser.replace(
this.maskedValue,
string,
range.start,
range.end
);
this.maskedValue = value;
this.updateValue();
this.requestUpdate();
if (range.start !== this.inputFormat.length) {
this.handleInput();
}
await this.updateComplete;
this.input.setSelectionRange(end, end);
}
private trySpinValue(
datePart: DatePart,
delta?: number,
negative = false
): Date {
// default to 1 if a delta is set to 0 or any other falsy value
const _delta =
delta || this.datePartDeltas[datePart as keyof DatePartDeltas] || 1;
const spinValue = negative ? -Math.abs(_delta) : Math.abs(_delta);
return this.spinValue(datePart, spinValue);
}
private spinValue(datePart: DatePart, delta: number): Date {
if (!this.value || !DateTimeUtil.isValidDate(this.value)) {
return new Date();
}
const newDate = new Date(this.value.getTime());
let formatPart: DatePartInfo | undefined;
let amPmFromMask: string;
switch (datePart) {
case DatePart.Date:
DateTimeUtil.spinDate(delta, newDate, this.spinLoop);
break;
case DatePart.Month:
DateTimeUtil.spinMonth(delta, newDate, this.spinLoop);
break;
case DatePart.Year:
DateTimeUtil.spinYear(delta, newDate);
break;
case DatePart.Hours:
DateTimeUtil.spinHours(delta, newDate, this.spinLoop);
break;
case DatePart.Minutes:
DateTimeUtil.spinMinutes(delta, newDate, this.spinLoop);
break;
case DatePart.Seconds:
DateTimeUtil.spinSeconds(delta, newDate, this.spinLoop);
break;
case DatePart.AmPm:
formatPart = this._inputDateParts.find(
(dp) => dp.type === DateParts.AmPm
);
if (formatPart !== undefined) {
amPmFromMask = this.maskedValue.substring(
formatPart!.start,
formatPart!.end
);
return DateTimeUtil.spinAmPm(newDate, this.value, amPmFromMask);
}
break;
}
return newDate;
}
private async onWheel(event: WheelEvent) {
if (!this.focused || this.readOnly) {
return;
}
event.preventDefault();
event.stopPropagation();
const { start, end } = this.inputSelection;
event.deltaY > 0 ? this.stepDown() : this.stepUp();
this.handleInput();
await this.updateComplete;
this.setSelectionRange(start, end);
}
private updateDefaultMask(): void {
this._defaultMask = DateTimeUtil.getDefaultMask(this.locale);
}
private setMask(string: string): void {
const oldFormat = this._inputDateParts?.map((p) => p.format).join('');
this._inputDateParts = DateTimeUtil.parseDateTimeFormat(string);
const value = this._inputDateParts.map((p) => p.format).join('');
this._defaultMask = value;
const newMask = (value || DateTimeUtil.DEFAULT_INPUT_FORMAT).replace(
new RegExp(/(?=[^t])[\w]/, 'g'),
'0'
);
this._mask = newMask.includes('tt')
? newMask.replace(/tt/g, 'LL')
: newMask;
this.parser.mask = this._mask;
this.parser.prompt = this.prompt;
if (!this.placeholder || oldFormat === this.placeholder) {
this.placeholder = value;
}
}
private parseDate(val: string) {
return val
? DateTimeUtil.parseValueFromMask(val, this._inputDateParts, this.prompt)
: null;
}
private getMaskedValue(): string {
let mask = this.emptyMask;
if (DateTimeUtil.isValidDate(this.value)) {
for (const part of this._inputDateParts) {
if (part.type === DateParts.Literal) {
continue;
}
const targetValue = DateTimeUtil.getPartValue(
part,
part.format.length,
this.value
);
mask = this.parser.replace(
mask,
targetValue,
part.start,
part.end
).value;
}
return mask;
}
return this.maskedValue === '' ? mask : this.maskedValue;
}
private isComplete(): boolean {
return !this.maskedValue.includes(this.prompt);
}
private updateValue(): void {
if (this.isComplete()) {
const parsedDate = this.parseDate(this.maskedValue);
this.value = DateTimeUtil.isValidDate(parsedDate) ? parsedDate : null;
} else {
this.value = null;
}
}
protected override _updateSetRangeTextValue() {
this.updateValue();
}
private getNewPosition(value: string, direction = 0): number {
const cursorPos = this.selection.start;
if (!direction) {
// Last literal before the current cursor position or start of input value
const part = this._inputDateParts.findLast(
(part) => part.type === DateParts.Literal && part.end < cursorPos
);
return part?.end ?? 0;
}
// First literal after the current cursor position or end of input value
const part = this._inputDateParts.find(
(part) => part.type === DateParts.Literal && part.start > cursorPos
);
return part?.start ?? value.length;
}
protected override async handleFocus() {
this.focused = true;
super.handleFocus();
if (this.readOnly) {
return;
}
this._oldValue = this.value;
if (!this._value) {
this.maskedValue = this.emptyMask;
await this.updateComplete;
this.select();
}
}
protected override handleBlur() {
this.focused = false;
if (!this.isComplete() && this.maskedValue !== this.emptyMask) {
const parse = this.parseDate(this.maskedValue);
if (parse) {
this.value = parse;
} else {
this.value = null;
this.maskedValue = '';
}
} else {
this.updateMask();
}
if (this._oldValue !== this.value) {
this.handleChange();
}
this.checkValidity();
super.handleBlur();
}
protected navigateParts(delta: number) {
const position = this.getNewPosition(this.input.value, delta);
this.setSelectionRange(position, position);
}
protected async keyboardSpin(direction: 'up' | 'down') {
direction === 'up' ? this.stepUp() : this.stepDown();
this.handleInput();
await this.updateComplete;
this.setSelectionRange(this.selection.start, this.selection.end);
}
protected override renderInput() {
return html`
<input
type="text"
part=${partNameMap(this.resolvePartNames('input'))}
name=${ifDefined(this.name)}
.value=${live(this.maskedValue)}
.placeholder=${live(this.placeholder || this.emptyMask)}
?readonly=${this.readOnly}
?disabled=${this.disabled}
@blur=${this.handleBlur}
@focus=${this.handleFocus}
@input=${super.handleInput}
@wheel=${this.onWheel}
@keydown=${super.handleKeydown}
@click=${this.handleClick}
@cut=${this.handleCut}
@change=${this.handleChange}
@compositionstart=${this.handleCompositionStart}
@compositionend=${this.handleCompositionEnd}
@dragenter=${this.handleDragEnter}
@dragleave=${this.handleDragLeave}
@dragstart=${this.handleDragStart}
/>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-date-time-input': IgcDateTimeInputComponent;
}
}