-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.ts
747 lines (681 loc) · 21.2 KB
/
index.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
import {
PropertyValues,
FocusedPropertyKey,
WarningNotice,
TemplateResult,
html
} from '@refinitiv-ui/core';
import { customElement } from '@refinitiv-ui/core/decorators/custom-element.js';
import { property } from '@refinitiv-ui/core/decorators/property.js';
import { state } from '@refinitiv-ui/core/decorators/state.js';
import { TemplateMap } from '@refinitiv-ui/core/directives/template-map.js';
import { AnimationTaskRunner } from '@refinitiv-ui/utils/async.js';
import {
utcParse,
utcFormat,
getFormat,
isBefore,
isAfter,
iterateUnit,
Locale
} from '@refinitiv-ui/utils/date.js';
import {
translate,
getLocale,
TranslateDirective,
TranslatePropertyKey
} from '@refinitiv-ui/translate';
import { Direction } from './constants.js';
import {
NavigationKeys,
DateTimeFormatPart,
InputSelection
} from './types.js';
import { TextField } from '../text-field/index.js';
import {
getSelectedPartIndex,
getNextSelectedPartIndex,
selectPart
} from './utils.js';
import { VERSION } from '../version.js';
import '@refinitiv-ui/phrasebook/locale/en/datetime-field.js';
/**
* A form control element for datetime input.
*
* @fires value-changed - Dispatched when value changes
* @fires error-changed - Dispatched when error state changes
* @fires icon-click - Dispatched when icon is clicked
*
* @attr {boolean} disabled - Set disabled state
* @prop {boolean} [disabled=false] - Set disabled state
*
* @attr {boolean} error - Set error state
* @prop {boolean} [error=false] - Set error state
*
* @attr {string} icon - Specify icon to display in input. Value can be icon name
* @prop {string | null} [icon=null] - Specify icon to display in input. Value can be icon name
*
* @attr {boolean} icon-has-action - Specify when icon need to be clickable
* @prop {boolean} [iconHasAction=false] - Specify when icon need to be clickable
*
* @attr {number} maxlength - Set character max limit
* @prop {number | null} [maxLength=null] - Set character max limit
*
* @attr {number} minlength - Set character min limit
* @prop {number | null} [minLength=null] - Set character min limit
*
* @prop {string} [pattern=""] - Set regular expression for input validation
*
* @attr {string} placeholder - Set placeholder text
* @prop {string} [placeholder=""] - Set placeholder text
*
* @attr {boolean} readonly - Set readonly state
* @prop {boolean} [readonly=false] - Set readonly state
*
* @attr {boolean} transparent - Disables all other states and border/background styles.
* @prop {boolean} [transparent=false] - Disables all other states and border/background styles.
*
* @attr {boolean} warning - Set warning state
* @prop {boolean} [warning=false] - Set warning state
*/
@customElement('ef-datetime-field')
export class DatetimeField extends TextField {
/**
* Element version number
* @returns version number
*/
static get version (): string {
return VERSION;
}
/**
* Delay selecting a part to ensure
* that scroll can be updated
*/
private selectPartFrame = new AnimationTaskRunner();
/**
* Set minimum date.
* This value must follow the `format` and be less
* than or equal to the value of the `max` attribute
*/
@property({ type: String, reflect: true })
public min: string | null = null;
/**
* Set maximum date.
* This value must follow the `format` and be greater
* than or equal to the value of the `min` attribute
*/
@property({ type: String, reflect: true })
public max: string | null = null;
private _timepicker = false;
/**
* Toggle to display the time picker
* @param timepicker true to set timepicker mode
* @default false
*/
@property({ type: Boolean, reflect: true })
public set timepicker (timepicker: boolean) {
const oldTimepicker = this._timepicker;
if (timepicker !== oldTimepicker) {
this._timepicker = timepicker;
this._locale = null;
this.requestUpdate('timepicker', oldTimepicker);
}
}
public get timepicker (): boolean {
return this._timepicker;
}
private _showSeconds = false;
/**
* Toggle to display the seconds
* @param showSeconds true to show seconds
* @default false
*/
@property({ type: Boolean, attribute: 'show-seconds', reflect: true })
public set showSeconds (showSeconds: boolean) {
const oldShowSeconds = this._showSeconds;
if (oldShowSeconds !== showSeconds) {
this._showSeconds = showSeconds;
this._locale = null;
this.requestUpdate('showSeconds', oldShowSeconds);
}
}
public get showSeconds (): boolean {
return this._showSeconds;
}
private _amPm = false;
/**
* Overrides 12hr time display format
* @param amPm true to show 12hr time format
* @default false
*/
@property({ type: Boolean, attribute: 'am-pm', reflect: true })
public set amPm (amPm: boolean) {
const oldAmPm = this._amPm;
if (oldAmPm !== amPm) {
this._amPm = amPm;
this._locale = null;
this.requestUpdate('amPm', oldAmPm);
}
}
public get amPm (): boolean {
return this._amPm;
}
private _formatOptions: Intl.DateTimeFormatOptions | null = null;
/**
* Set the datetime format options based on
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
* `formatOptions` overrides `timepicker` and `showSeconds` properties.
* Note: time-zone is not supported
* @param formatOptions Format options
* @default - null
*/
@property({ attribute: false })
public set formatOptions (formatOptions: Intl.DateTimeFormatOptions | null) {
const oldFormatOptions = this._formatOptions;
if (oldFormatOptions !== formatOptions) {
this._formatOptions = formatOptions;
this._locale = null;
this.requestUpdate('formatOptions', oldFormatOptions);
}
}
public get formatOptions (): Intl.DateTimeFormatOptions | null {
return this._formatOptions;
}
/**
* Used for translations
*/
@translate({ mode: 'directive', scope: 'ef-datetime-field' })
protected t!: TranslateDirective;
/**
* Format, which is based on locale
*/
private _locale: Locale | null = null;
protected get locale (): Locale {
if (!this._locale) {
this._locale = this.resolveLocale();
}
return this._locale;
}
private interimValueState = false; // make sure that internal input field value is updated only on external value change
/**
* Current date time value
* @param value Calendar value
* @default -
*/
@property({ type: String })
public set value (value: string) {
this.interimValueState = true;
super.value = value;
// Always call update cycle when value is set externally
// This is to ensure that value and inputValue never run out of sync
this.requestUpdate('interimValueState', false);
}
public get value (): string {
return super.value;
}
/**
* Returns the value of the element, interpreted as double number
*/
public get valueAsNumber (): number {
const date = this.valueAsDate;
return date ? date.getTime() : Number.NaN;
}
/**
* Set the value of the element, interpreted as double number
* @param value number value
*/
public set valueAsNumber (value: number) {
const date = new Date(value);
this.valueAsDate = date;
}
/**
* Returns the value of the element, interpreted as Date
*/
public get valueAsDate (): null | Date {
return this.value ? utcParse(this.value) : null;
}
/**
* Set the value of the element, interpreted as Date
* @param value date value
*/
public set valueAsDate (value: null | Date) {
this.value = value ? this.dateToString(value) : '';
}
/**
* Currently selected part
*/
@state()
protected partLabel = '';
/**
* Transform Date object to date string
* @param value Date
* @returns dateSting
*/
protected dateToString (value: Date): string {
return isNaN(value.getTime()) ? '' : utcFormat(value, this.locale.isoFormat);
}
/**
* Returns true if the datetime field has timepicker
* @returns hasTimePicker
*/
protected get hasTimePicker (): boolean {
// need to check for attribute to resolve the value correctly until the first lifecycle is run
return this.timepicker || this.hasAttribute('timepicker') || this.hasAmPm || this.hasSeconds;
}
/**
* Returns true if the datetime field has seconds
* @returns hasSeconds
*/
protected get hasSeconds (): boolean {
return this.showSeconds || this.hasAttribute('show-seconds');
}
/**
* Returns true if the datetime field has am-pm
* @returns hasAmPm
*/
protected get hasAmPm (): boolean {
return this.amPm || this.hasAttribute('am-pm');
}
/**
* Get today's date noon as the start date when the value is not set
*/
protected get startDate (): string {
// Noon in UTC
const today = new Date();
const date = new Date(0);
date.setUTCFullYear(today.getFullYear());
date.setUTCMonth(today.getMonth());
date.setUTCDate(today.getDate());
date.setUTCHours(12);
return this.dateToString(date);
}
/**
* Updates the element
* @param changedProperties Properties that has changed
* @returns {void}
*/
public willUpdate (changedProperties: PropertyValues): void {
super.willUpdate(changedProperties);
if (changedProperties.has(TranslatePropertyKey)) {
this._locale = null; // Locale is updated on next call via getter
}
if (changedProperties.has(FocusedPropertyKey) && !this.focused) {
this.partLabel = '';
}
}
/**
* Check if input value should be synchronised with component value
* @param changedProperties Properties that has changed
* @returns True if input should be synchronised
*/
protected override shouldSyncInputValue (changedProperties: PropertyValues): boolean {
// Note: changing any of these properties override the input value
// On blur, if the value is correct makes sure strict format is used
return changedProperties.has('interimValueState')
|| changedProperties.has(TranslatePropertyKey)
|| changedProperties.has('formatOptions')
|| changedProperties.has('timepicker')
|| changedProperties.has('showSeconds')
|| changedProperties.has('amPm')
|| (changedProperties.has(FocusedPropertyKey) && this.value !== '' && !this.focused);
}
/**
* Synchronise input value with value.
* Override the method if value and input value are not the same
* @param changedProperties Properties that has changed
* @returns {void}
*/
protected override syncInputValue (changedProperties: PropertyValues): void;
protected syncInputValue (): void;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected syncInputValue (changedProperties?: PropertyValues): void {
this.interimValueState = false;
const inputValue = this.toInputValue(this.value);
if (inputValue !== this.inputValue) {
this.inputValue = inputValue; // setting the value resets selection
}
}
/**
* Check if input should be re-validated
* @param changedProperties Properties that has changed
* @returns True if input should be re-validated
*/
protected override shouldValidateInput (changedProperties: PropertyValues): boolean {
// TODO: this needs refactoring with all other fields to support common validation patterns
return (changedProperties.has(FocusedPropertyKey) && !this.focused);
}
/**
* Validate input according `pattern`, `minLength` and `maxLength` properties
* change state of `error` property according pattern validation
* @returns {void}
*/
protected override validateInput (): void {
this.reportValidity();
}
/**
* Override validation method for value
* @param value value
* @returns value validity
*/
protected override isValidValue (value: string): boolean {
if (value === '') {
return true;
}
// value format depends on locale.
return getFormat(value) === this.locale.isoFormat;
}
/**
* Used to show a warning when the value does not pass the validation
* @param value that is invalid
* @returns {void}
*/
protected override warnInvalidValue (value: string): void {
new WarningNotice(`${this.localName}: the specified value "${value}" does not conform to the required format. The format is '${this.locale.isoFormat}'.`).show();
}
/**
* Resolve locale based on element parameters
* @returns locale Resolved locale
*/
protected resolveLocale (): Locale {
const hasTimePicker = this.hasTimePicker;
// TODO: Do not use dateStyle and timeStyle as these are supported only in modern browsers
return Locale.fromOptions(this.formatOptions || {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: hasTimePicker ? 'numeric' : undefined,
minute: hasTimePicker ? 'numeric' : undefined,
second: this.hasSeconds ? 'numeric' : undefined,
hour12: this.hasAmPm ? true : undefined // force am-pm if provided, otherwise rely on locale
}, `${getLocale(this)}`);
}
/**
* Get Intl.DateTimeFormat object from locale
*/
protected get formatter (): Intl.DateTimeFormat {
return this.locale.formatter;
}
/**
* Try to format ISO date/time/datetime string into datetime format parts
* Throw an error if value is invalid
* @param value ISO string date/time/datetime
* @returns parts
*/
protected formatToParts (value: string): DateTimeFormatPart[] {
const date = utcParse(value);
return this.formatter.formatToParts(date);
}
/**
* Format value to inputValue
* @param value Date string
* @returns inputValue
*/
protected toInputValue (value: string): string {
return value ? this.formatter.format(utcParse(value)) : '';
}
/**
* Format inputValue to value
* @param inputValue Date string
* @returns value
*/
protected toValue (inputValue: string): string {
let value = '';
try {
value = inputValue ? this.locale.parse(inputValue, this.value || this.startDate) : '';
}
catch (error) {
// do nothing
}
return value;
}
/**
* On *user-interaction* set the value and notify.
* @param value New value
* @returns {void}
*/
protected override setValueAndNotify (value: string): void {
// must be called on super to avoid re-rendering of actual value
if (super.value !== value) {
super.value = value;
this.notifyPropertyChange('value', value);
}
}
/**
* Reset error state on input
* @returns {void}
*/
protected resetError (): void {
if (this.error && this.checkValidity()) {
this.reportValidity();
}
}
/**
* Returns true if an input element contains valid data.
* @returns true if input is valid
*/
public checkValidity (): boolean {
const inputValue = this.inputValue;
// Invalid input value
if (inputValue && !this.toValue(inputValue)) {
return false;
}
const value = this.value;
// No support for required
if (value === '') {
return true;
}
// Value before min
if (this.min && value !== this.min && isBefore(value, this.min)) {
return false;
}
// Value after max
if (this.max && value !== this.max && isAfter(value, this.max)) {
return false;
}
return true;
}
/**
* Validate input. Mark as error if input is invalid
* @returns false if there is an error
*/
public reportValidity (): boolean {
const hasError = !this.checkValidity();
this.notifyErrorChange(hasError);
return !hasError;
}
/**
* Select part
* @param index Part index
* @param parts The list of parts
* @returns {void}
*/
protected selectPart (index = 0, parts: DateTimeFormatPart[]): void {
const { selectionStart, selectionEnd } = selectPart(index, parts);
this.partLabel = parts[index] ? parts[index].type : '';
this.setSelectionRange(selectionStart, selectionEnd);
}
/**
* Change value or selection based on keyboard navigation
* @param key Navigation key
* @param event Keyboard event
* @returns {void}
*/
protected onNavigation (key: NavigationKeys, event: KeyboardEvent): void {
this.selectPartFrame.cancel();
this.partLabel = '';
// Invalid value
if (this.inputValue && !this.value) {
return;
}
// Empty value
if (!this.inputValue) {
this.setValueAndNotify(this.startDate);
this.syncInputValue();
this.select();
event.preventDefault();
return;
}
// Set selection into cursor mode with Escape key
if (key === 'Escape') {
if (this.selectionStart !== this.selectionEnd) {
this.selectionStart = this.selectionEnd;
event.preventDefault();
}
return;
}
// Because of weak formatting, the result of formatting to part might be
// different to actual input value
const selection: InputSelection = {
selectionStart: this.selectionStart,
selectionEnd: this.selectionEnd
};
this.syncInputValue();
const parts = this.formatToParts(this.value);
const selectedPartIndex = getSelectedPartIndex(selection, parts);
// If no segments selected, select the segment where the cursor is
if (this.selectionStart === this.selectionEnd) {
this.selectPart(selectedPartIndex, parts);
event.preventDefault();
return;
}
const selectedPart = parts[selectedPartIndex];
switch (key) {
case 'ArrowUp':
case 'ArrowDown':
if (selectedPart) {
let newValue = '';
const amount = key === 'ArrowDown' ? -1 : 1;
switch (selectedPart.type) {
case 'year':
case 'month':
case 'day':
case 'hour':
case 'minute':
case 'second':
newValue = iterateUnit(this.value, selectedPart.type, amount);
break;
case 'weekday':
newValue = iterateUnit(this.value, 'day', amount);
break;
case 'dayPeriod':
newValue = iterateUnit(this.value, 'hour', amount * 12);
break;
case 'fractionalSecond':
newValue = iterateUnit(this.value, 'millisecond', amount * 100);
break;
// no default
}
this.setValueAndNotify(newValue);
this.syncInputValue();
this.selectPart(selectedPartIndex, this.formatToParts(newValue));
this.resetError();
event.preventDefault();
}
break;
case 'ArrowRight':
case 'ArrowLeft':
const nextPartIdx = getNextSelectedPartIndex(selection, parts, this.inputValue, key === 'ArrowLeft' ? Direction.Down : Direction.Up);
this.selectPartFrame.schedule(() => {
this.selectPart(nextPartIdx, parts);
});
break;
// no default
}
}
/**
* Check if value is changed and fire event
* @returns {void}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected override onPossibleValueChange (event: InputEvent): void {
// Nobody likes to see a red border
this.resetError();
this.selectPartFrame.cancel(); // ensure no pending selection
this.partLabel = '';
const inputValue = this.inputElement?.value || '';
const parsedValue = this.toValue(inputValue);
this.setValueAndNotify(parsedValue);
}
/**
* Handles key down input event
* @param event Key down event object
* @returns {void}
*/
protected onInputKeyDown (event: KeyboardEvent): void {
if (this.readonly || this.disabled || event.defaultPrevented) {
return;
}
// Ignore special keys
if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) {
return;
}
switch (event.key) {
case 'Up': // IE11
case 'ArrowUp':
this.onNavigation('ArrowUp', event);
break;
case 'Down':
case 'ArrowDown':
this.onNavigation('ArrowDown', event);
break;
case 'Left':
case 'ArrowLeft':
this.onNavigation('ArrowLeft', event);
break;
case 'Right':
case 'ArrowRight':
this.onNavigation('ArrowRight', event);
break;
case 'Esc':
case 'Escape':
this.onNavigation('Escape', event);
break;
default:
return;
}
}
/**
* Decorate `<input>` element with common properties.
* @returns template map
*/
protected override get decorateInputMap (): TemplateMap {
return {
...super.decorateInputMap,
'@keydown': this.onInputKeyDown
};
}
/**
* Used to announce part and value statuses
*/
protected get screenReaderTemplate (): TemplateResult | null {
if (!this.focused) {
return null;
}
return html`
${this.partLabel && this.value ? html`<div
aria-label="${this.t(`PICK_${this.partLabel.toUpperCase()}`)}"
aria-live="polite"
role="status"></div>` : undefined}
<div
aria-label="${this.value ? this.t('VALUE', { value: this.toInputValue(this.value) }) : this.t('NO_VALUE')}"
aria-live="polite"
role="status"></div>
`;
}
/**
* A `TemplateResult` that will be used
* to render the updated internal template.
* @return Render template
*/
protected render (): TemplateResult {
return html`
${super.render()}
${this.screenReaderTemplate}
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'ef-datetime-field': DatetimeField;
}
}