@progress/kendo-dateinputs-common 0.2.0-dev.202301061341 → 0.2.0-dev.202301101148

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -585,7 +585,7 @@ var DateObject = /** @class */ (function () {
585
585
  */
586
586
  DateObject.prototype.parsePart = function (_a) {
587
587
  var _b;
588
- var symbol = _a.symbol, currentChar = _a.currentChar, resetSegmentValue = _a.resetSegmentValue, cycleSegmentValue = _a.cycleSegmentValue, rawTextValue = _a.rawTextValue, isDeleting = _a.isDeleting;
588
+ var symbol = _a.symbol, currentChar = _a.currentChar, resetSegmentValue = _a.resetSegmentValue, cycleSegmentValue = _a.cycleSegmentValue, rawInputValue = _a.rawTextValue, isDeleting = _a.isDeleting, originalFormat = _a.originalFormat;
589
589
  var isInCaretMode = !cycleSegmentValue;
590
590
  var dateParts = this.dateFormatString(this.value, this.format);
591
591
  var datePartsLiterals = dateParts.partMap
@@ -593,9 +593,20 @@ var DateObject = /** @class */ (function () {
593
593
  .map(function (x, index) {
594
594
  return {
595
595
  datePartIndex: index,
596
- literal: x.pattern
596
+ type: x.type,
597
+ pattern: x.pattern,
598
+ literal: ""
597
599
  };
598
600
  });
601
+ for (var i = 0; i < datePartsLiterals.length; i++) {
602
+ var datePart = datePartsLiterals[i];
603
+ for (var j = 0; j < datePart.pattern.length; j++) {
604
+ if (datePartsLiterals[i + j]) {
605
+ datePartsLiterals[i + j].literal = datePart.pattern[j];
606
+ }
607
+ }
608
+ i += datePart.pattern.length - 1;
609
+ }
599
610
  var shouldResetPart = isInCaretMode && symbol === "M" && dateParts.partMap
600
611
  .filter(function (x) { return x.type === "month"; })
601
612
  .some(function (x) { return x.pattern.length > MONTH_PART_WITH_WORDS_THRESHOLD; });
@@ -608,9 +619,9 @@ var DateObject = /** @class */ (function () {
608
619
  if (isInCaretMode) {
609
620
  for (var i = 0; i < datePartsLiterals.length; i++) {
610
621
  var literal = datePartsLiterals[i].literal;
611
- var rawValueStartsWithLiteral = rawTextValue.startsWith(literal);
612
- var rawValueEndsWithLiteral = rawTextValue.endsWith(literal);
613
- var rawValueHasConsecutiveLiterals = rawTextValue.indexOf(literal + literal) >= 0;
622
+ var rawValueStartsWithLiteral = rawInputValue.startsWith(literal);
623
+ var rawValueEndsWithLiteral = rawInputValue.endsWith(literal);
624
+ var rawValueHasConsecutiveLiterals = rawInputValue.indexOf(literal + literal) >= 0;
614
625
  if (rawValueStartsWithLiteral || rawValueEndsWithLiteral || rawValueHasConsecutiveLiterals) {
615
626
  this.resetLeadingZero();
616
627
  this.setExisting(symbol, false);
@@ -633,38 +644,82 @@ var DateObject = /** @class */ (function () {
633
644
  var prefix = '';
634
645
  var current = '';
635
646
  var datePartText = '';
647
+ var basePrefix = '';
648
+ var baseSuffix = '';
636
649
  var suffix = '';
650
+ var datePartStartIndex = originalFormat.indexOf(symbol);
651
+ var datePartEndIndex = originalFormat.lastIndexOf(symbol);
652
+ var segmentLength = datePartEndIndex - datePartStartIndex + 1;
653
+ var hasFixedFormat = (this.format === baseFormat) ||
654
+ (this.format === originalFormat) ||
655
+ (this.format.length === originalFormat.length);
637
656
  if (isInCaretMode) {
638
- var datePartIndex = 0;
639
- var outOfDatePartBounds = false;
657
+ var processedSegmentCharsCount = 0;
640
658
  for (var i = 0; i < baseDate.length; i++) {
641
- var datePartLiteral = datePartsLiterals[datePartIndex];
642
- if (datePartLiteral && datePartLiteral === baseDate[i]) {
643
- datePartIndex++;
644
- }
645
659
  if (baseFormat[i] === symbol) {
646
660
  var existing = this.getExisting(symbol);
647
661
  current += existing ? baseDate[i] : '0';
648
- var rawInputChar = rawTextValue[i];
649
- if (rawInputChar !== baseDate[i] && rawInputChar === datePartsLiterals[datePartIndex].literal) {
650
- outOfDatePartBounds = true;
651
- }
652
- else if (!outOfDatePartBounds) {
653
- if (rawInputChar === undefined) {
654
- var formatOffset = Math.abs(this.format.length - baseFormat.length);
655
- datePartText += rawTextValue[i - formatOffset] || '';
656
- }
657
- else {
658
- datePartText += rawInputChar || '';
662
+ if (isDeleting) {
663
+ // when deleting, process (segmentLength - 1) chars
664
+ if (processedSegmentCharsCount < segmentLength - 1) {
665
+ datePartText += rawInputValue[i] || "";
659
666
  }
667
+ processedSegmentCharsCount++;
668
+ }
669
+ else {
670
+ datePartText += rawInputValue[i] || "";
660
671
  }
661
672
  replaced = true;
662
673
  }
663
674
  else if (!replaced) {
664
675
  prefix += baseDate[i];
676
+ basePrefix += baseDate[i];
665
677
  }
666
678
  else {
667
679
  suffix += baseDate[i];
680
+ baseSuffix += baseDate[i];
681
+ }
682
+ }
683
+ if (hasFixedFormat) {
684
+ if (originalFormat.length < rawInputValue.length) {
685
+ datePartText += currentChar;
686
+ }
687
+ if (datePartText.length > segmentLength) {
688
+ return utils_2.extend(parseResult, { value: null, switchToNext: false });
689
+ }
690
+ }
691
+ else {
692
+ processedSegmentCharsCount = 0;
693
+ current = "";
694
+ datePartText = "";
695
+ prefix = "";
696
+ suffix = "";
697
+ replaced = false;
698
+ for (var i = 0; i < originalFormat.length; i++) {
699
+ if (originalFormat[i] === symbol) {
700
+ var existing = this.getExisting(symbol);
701
+ current += existing ? baseDate[i] || "" : '0';
702
+ if (isDeleting) {
703
+ // when deleting, process (segmentLength - 1) chars
704
+ if (processedSegmentCharsCount < segmentLength - 1) {
705
+ datePartText += rawInputValue[i] || "";
706
+ }
707
+ processedSegmentCharsCount++;
708
+ }
709
+ else {
710
+ datePartText += rawInputValue[i] || "";
711
+ }
712
+ replaced = true;
713
+ }
714
+ else if (!replaced) {
715
+ prefix += rawInputValue[i] || "";
716
+ }
717
+ else {
718
+ suffix += rawInputValue[i] || "";
719
+ }
720
+ }
721
+ if (originalFormat.length < rawInputValue.length) {
722
+ datePartText += currentChar;
668
723
  }
669
724
  }
670
725
  }
@@ -699,10 +754,6 @@ var DateObject = /** @class */ (function () {
699
754
  }
700
755
  var partPattern = this.partPattern(dateParts.partMap, symbol);
701
756
  var patternValue = partPattern ? partPattern.pattern : null;
702
- if (isInCaretMode && isDeleting) {
703
- var padPrefix = utils_1.padZero(Math.abs(current.length - datePartText.length));
704
- current = padPrefix + datePartText;
705
- }
706
757
  if (isInCaretMode) {
707
758
  if (isDeleting && !datePartText) {
708
759
  this.setExisting(symbol, false);
@@ -718,14 +769,29 @@ var DateObject = /** @class */ (function () {
718
769
  if (!this.autoCorrectParts) {
719
770
  tryParse = false;
720
771
  }
721
- var middle = resetSegmentValue ? currentChar : (current.substring(i) + currentChar);
722
- if (!tryParse && isInCaretMode) {
772
+ var middle = resetSegmentValue ?
773
+ currentChar :
774
+ isInCaretMode ?
775
+ datePartText :
776
+ (current.substring(i) + currentChar);
777
+ if (isInCaretMode) {
723
778
  // try to make an exact match as there will be only 1 attempt
779
+ tryParse = false;
724
780
  middle = utils_1.unpadZero(middle);
725
781
  }
726
782
  var middleNumber = parseInt(middle, 10);
727
783
  var candidateDateString = prefix + middle + suffix;
728
784
  parsedDate = this.intl.parseDate(candidateDateString, this.format, this.localeId);
785
+ if (isInCaretMode && !hasFixedFormat && !utils_2.isValidDate(parsedDate)) {
786
+ // if part of the date is not available, e.g. "d"
787
+ // but an expanded format like "F" is used
788
+ // the element value can be "EEEE, February 1, 2022 3:04:05 AM"
789
+ // which is not parsable by intl
790
+ // use the base prefix and suffix, e.g. convert the candidate date string
791
+ // to "Thursday, February 1, 2022 3:04:05 AM"
792
+ // as "EEEE, February..." is not parsable
793
+ parsedDate = this.intl.parseDate(basePrefix + middle + baseSuffix, this.format, this.localeId);
794
+ }
729
795
  var isCurrentCharParsable = !isNaN(parseInt(currentChar, 10)) || (isInCaretMode && isDeleting && currentChar === "");
730
796
  if (!parsedDate && !isNaN(middleNumber) && isCurrentCharParsable) {
731
797
  if (symbol === MONTH_SYMBOL && !month) {
@@ -741,18 +807,22 @@ var DateObject = /** @class */ (function () {
741
807
  }
742
808
  if (symbol === 'y') {
743
809
  parsedDate = kendo_date_math_1.createDate(parseInt(middle, 10), this.month ? this.value.getMonth() : 0, this.date ? this.value.getDate() : 1, this.hours ? this.value.getHours() : 0, this.minutes ? this.value.getMinutes() : 0, this.seconds ? this.value.getSeconds() : 0, this.milliseconds ? this.value.getMilliseconds() : 0);
744
- if (this.date && parsedDate.getDate() !== this.value.getDate()) {
810
+ if (((isInCaretMode && utils_2.isValidDate(parsedDate)) ||
811
+ (!isInCaretMode && parsedDate)) && this.date && parsedDate.getDate() !== this.value.getDate()) {
745
812
  parsedDate = kendo_date_math_1.lastDayOfMonth(kendo_date_math_1.addMonths(parsedDate, -1));
746
813
  }
747
814
  }
748
815
  }
749
- if (parsedDate) {
816
+ if ((isInCaretMode && utils_2.isValidDate(parsedDate)) || (!isInCaretMode && parsedDate)) {
750
817
  // move to next segment if the part will overflow with next char
751
818
  // when start from empty date (01, then 010), padded zeros should be trimmed
752
819
  var peekDate = this.intl.parseDate("" + prefix + this.peek(middle, patternValue) + suffix, this.format, this.localeId);
753
820
  var patternLength = this.patternLength(patternValue) || patternValue.length;
754
- var patternSatisfied = (leadingZero + (utils_1.unpadZero(middle) || currentChar).length) >= patternLength;
755
- var switchToNext = peekDate === null || patternSatisfied;
821
+ var leadingZeroOffset = (this.leadingZero || {})[symbol] || 0;
822
+ var patternSatisfied = (leadingZeroOffset + (utils_1.unpadZero(middle) || currentChar).length) >= patternLength;
823
+ var switchToNext = peekDate === null ||
824
+ (leadingZero[symbol] && patternValue.length <= middle.length) ||
825
+ patternSatisfied;
756
826
  if (this.shouldNormalizeCentury()) {
757
827
  parsedDate = this.normalizeCentury(parsedDate);
758
828
  }
@@ -780,6 +850,9 @@ var DateObject = /** @class */ (function () {
780
850
  this.leadingZero = !this.isAbbrMonth(dateParts.partMap, symbol) ? (_b = {}, _b[symbol] = true, _b) : null;
781
851
  this.setExisting(symbol, false);
782
852
  }
853
+ if (isInCaretMode && datePartText.length > segmentLength) {
854
+ return utils_2.extend(parseResult, { value: null, switchToNext: false });
855
+ }
783
856
  if (!this.autoCorrectParts) {
784
857
  this.setExisting(symbol, false);
785
858
  // todo check if string is better
@@ -795,11 +868,14 @@ var DateObject = /** @class */ (function () {
795
868
  }
796
869
  if (utils_2.isNumber(datePartValue)) {
797
870
  var newDate = this.modifyDateSymbolWithValue(this.value, symbol, datePartValue);
871
+ // if (!isEqual(newDate, this.value)) {
872
+ this.setExisting(symbol, false);
798
873
  this.setInvalidDatePart(symbol, {
799
874
  value: datePartValue,
800
875
  date: kendo_date_math_1.cloneDate(newDate),
801
876
  startDate: this._partiallyInvalidDate.startDate || kendo_date_math_1.cloneDate(this.value)
802
877
  });
878
+ // }
803
879
  }
804
880
  }
805
881
  return utils_2.extend(parseResult, { value: null, switchToNext: false });
@@ -963,8 +1039,8 @@ var DateObject = /** @class */ (function () {
963
1039
  var resultText = '';
964
1040
  var resultFormat = '';
965
1041
  var format = mask.symbols;
966
- for (var formatSymbolIndex = format.length - 1; formatSymbolIndex >= 0; formatSymbolIndex--) {
967
- if (this.knownParts.indexOf(format[formatSymbolIndex]) === -1 || this.getExisting(format[formatSymbolIndex])) {
1042
+ var _loop_1 = function (formatSymbolIndex) {
1043
+ if (this_1.knownParts.indexOf(format[formatSymbolIndex]) === -1 || this_1.getExisting(format[formatSymbolIndex])) {
968
1044
  resultText = text[formatSymbolIndex] + resultText;
969
1045
  resultFormat = format[formatSymbolIndex] + resultFormat;
970
1046
  }
@@ -974,22 +1050,31 @@ var DateObject = /** @class */ (function () {
974
1050
  formatSymbolIndex--;
975
1051
  }
976
1052
  formatSymbolIndex++;
977
- if (this.leadingZero && this.leadingZero[symbol]) {
1053
+ if (this_1.leadingZero && this_1.leadingZero[symbol]) {
978
1054
  resultText = '0' + resultText;
979
1055
  }
980
1056
  else {
981
- if (!this.autoCorrectParts && this.getInvalidDatePartValue(symbol)) {
982
- var segmentText = text.substr(formatSymbolIndex, mask.partMap[formatSymbolIndex].pattern.length);
1057
+ if (!this_1.autoCorrectParts && this_1.getInvalidDatePartValue(symbol)) {
1058
+ // use mask.symbols instead of mask.partMap
1059
+ var part_1 = mask.partMap[formatSymbolIndex];
1060
+ var pattern = mask.partMap.filter(function (x) { return x.type === part_1.type && x.pattern === part_1.pattern; });
1061
+ var segmentText = text.substr(formatSymbolIndex, pattern.length);
983
1062
  resultText = segmentText + resultText;
984
1063
  }
985
1064
  else {
986
- resultText = this.dateFieldName(mask.partMap[formatSymbolIndex]) + resultText;
1065
+ resultText = this_1.dateFieldName(mask.partMap[formatSymbolIndex]) + resultText;
987
1066
  }
988
1067
  }
989
1068
  while (resultFormat.length < resultText.length) {
990
1069
  resultFormat = format[formatSymbolIndex] + resultFormat;
991
1070
  }
992
1071
  }
1072
+ out_formatSymbolIndex_1 = formatSymbolIndex;
1073
+ };
1074
+ var this_1 = this, out_formatSymbolIndex_1;
1075
+ for (var formatSymbolIndex = format.length - 1; formatSymbolIndex >= 0; formatSymbolIndex--) {
1076
+ _loop_1(formatSymbolIndex);
1077
+ formatSymbolIndex = out_formatSymbolIndex_1;
993
1078
  }
994
1079
  return { text: resultText, format: resultFormat };
995
1080
  };
@@ -57,4 +57,4 @@ export declare const areDatePartsEqualTo: (date: any, year: any, month: any, day
57
57
  /**
58
58
  * @hidden
59
59
  */
60
- export declare const isDate: (value: any) => boolean;
60
+ export declare const isValidDate: (value: any) => boolean;
@@ -127,4 +127,4 @@ exports.areDatePartsEqualTo = function (date, year, month, day, hour, minutes, s
127
127
  /**
128
128
  * @hidden
129
129
  */
130
- exports.isDate = function (value) { return exports.isPresent(value) && value.getTime && exports.isNumber(value.getTime()); };
130
+ exports.isValidDate = function (value) { return exports.isPresent(value) && value.getTime && exports.isNumber(value.getTime()); };
@@ -10,13 +10,13 @@ export declare class DateInput extends Observable {
10
10
  private isActive;
11
11
  private interactionMode;
12
12
  private previousElementValue;
13
+ private previousElementSelection;
13
14
  private keyDownEvent;
14
15
  private isPasteInProgress;
15
16
  private mouseDownStarted;
16
17
  private focusedPriorToMouseDown;
17
18
  private intl;
18
19
  private formatPlaceholder;
19
- private localeId;
20
20
  constructor(element: any, options: any);
21
21
  init(element: any, options: any): void;
22
22
  destroy(): void;
@@ -183,7 +183,18 @@ export declare class DateInput extends Observable {
183
183
  * @hidden
184
184
  */
185
185
  getStepFromSymbol(symbol: any): number;
186
- setPreviousInputEventState(keyDownEvent: any): void;
186
+ /**
187
+ * @hidden
188
+ */
189
+ restorePreviousInputEventState(): void;
190
+ /**
191
+ * @hidden
192
+ */
193
+ restorePreviousElementValue(): void;
194
+ /**
195
+ * @hidden
196
+ */
197
+ restorePreviousElementSelection(): void;
187
198
  writeValue(value: Date): void;
188
199
  verifyValue(value: Date): void;
189
200
  refreshElementValue(): void;
@@ -76,7 +76,7 @@ var DateInput = /** @class */ (function (_super) {
76
76
  _this.currentText = '';
77
77
  _this.currentFormat = '';
78
78
  _this.interactionMode = interaction_mode_1.DateInputInteractionMode.None;
79
- _this.localeId = constants_1.Constants.defaultLocaleId;
79
+ _this.previousElementSelection = { start: 0, end: 0 };
80
80
  _this.init(element, options);
81
81
  return _this;
82
82
  }
@@ -88,12 +88,12 @@ var DateInput = /** @class */ (function (_super) {
88
88
  configurable: true
89
89
  });
90
90
  DateInput.prototype.init = function (element, options) {
91
- var dateValue = utils_2.isDate(this.options.value) ? kendo_date_math_1.cloneDate(this.options.value) : new Date(options.formattedValue);
92
- if (!utils_2.isDate(dateValue)) {
91
+ var dateValue = utils_2.isValidDate(this.options.value) ? kendo_date_math_1.cloneDate(this.options.value) : new Date(options.formattedValue);
92
+ if (!utils_2.isValidDate(dateValue)) {
93
93
  dateValue = null;
94
94
  }
95
95
  this.element = element;
96
- // this.element._kendoWidget = this;
96
+ this.element._kendoWidget = this;
97
97
  this.options = utils_2.extend({}, defaultDateInputOptions, options);
98
98
  this.intl = this.options.intlService;
99
99
  this.formatPlaceholder = this.options.formatPlaceholder ? this.options.formatPlaceholder : 'formatPattern';
@@ -274,33 +274,48 @@ var DateInput = /** @class */ (function (_super) {
274
274
  var hasCaret = this.isInCaretMode();
275
275
  if (hasCaret && this.keyDownEvent.key === key_1.Key.SPACE) {
276
276
  // do not allow custom "holes" in the date segments
277
- this.setPreviousInputEventState(this.keyDownEvent);
277
+ this.restorePreviousInputEventState();
278
278
  return;
279
279
  }
280
280
  var oldDateObjectValue = this.dateObject && this.dateObject.getValue();
281
281
  var _a = this.dateObject.getTextAndFormat(), currentText = _a.text, currentFormat = _a.format;
282
282
  this.currentFormat = currentFormat;
283
- var text = hasCaret && (isBackspaceKey || isDeleteKey) ? this.previousElementValue : currentText;
283
+ var text = "";
284
+ if (hasCaret) {
285
+ if (isBackspaceKey || isDeleteKey) {
286
+ text = this.previousElementValue;
287
+ }
288
+ else if (originalInteractionMode === interaction_mode_1.DateInputInteractionMode.Caret) {
289
+ text = this.previousElementValue;
290
+ }
291
+ else {
292
+ text = currentText;
293
+ }
294
+ }
295
+ else {
296
+ text = currentText;
297
+ }
298
+ var newText = this.elementValue;
284
299
  var diff = utils_1.approximateStringMatching({
285
300
  oldText: text,
286
- newText: this.element.value,
301
+ newText: newText,
287
302
  formatPattern: this.currentFormat,
288
303
  selectionStart: this.selection.start,
289
304
  isInCaretMode: hasCaret,
290
305
  keyEvent: this.keyDownEvent
291
306
  });
292
307
  if (hasCaret && (!diff || diff.length === 0)) {
293
- this.setPreviousInputEventState(this.keyDownEvent);
308
+ this.restorePreviousInputEventState();
294
309
  return;
295
310
  }
296
311
  else if (hasCaret && diff.length === 1) {
297
312
  if (!diff[0] || !diff[0][0]) {
298
- this.setPreviousInputEventState(this.keyDownEvent);
313
+ this.restorePreviousInputEventState();
299
314
  return;
300
315
  }
301
316
  else if (hasCaret && diff[0] &&
302
317
  (diff[0][0] === constants_1.Constants.formatSeparator || diff[0][1] === constants_1.Constants.formatSeparator)) {
303
- this.setPreviousInputEventState(this.keyDownEvent);
318
+ this.restorePreviousInputEventState();
304
319
  return;
305
320
  }
306
321
  }
@@ -315,7 +330,8 @@ var DateInput = /** @class */ (function (_super) {
315
330
  resetSegmentValue: this.resetSegmentValue,
316
331
  cycleSegmentValue: !this.isInCaretMode(),
317
332
  rawTextValue: this.element.value,
318
- isDeleting: isBackspaceKey || isDeleteKey
333
+ isDeleting: isBackspaceKey || isDeleteKey,
334
+ originalFormat: this.currentFormat
319
335
  });
320
336
  parsePartsResults.push(parsePartResult);
321
337
  switchPart = parsePartResult.switchToNext;
@@ -353,6 +369,29 @@ var DateInput = /** @class */ (function (_super) {
353
369
  }
354
370
  }
355
371
  }
372
+ else if (hasCaret) {
373
+ if (this.options.format.length !== this.currentFormat.length) {
374
+ if (hasDateValueChanged && utils_2.isPresent(this.dateObject.value)) {
375
+ var elementValueLength = this.elementValue.length;
376
+ this.forceUpdate();
377
+ var selectionOffset = this.elementValue.length - elementValueLength;
378
+ this.setSelection({
379
+ start: currentSelection.start + selectionOffset,
380
+ end: currentSelection.start + selectionOffset
381
+ });
382
+ }
383
+ }
384
+ else {
385
+ if (hasDateValueChanged) {
386
+ if (lastParseResultHasNoValue) {
387
+ this.restorePreviousInputEventState();
388
+ }
389
+ }
390
+ else {
391
+ this.restorePreviousInputEventState();
392
+ }
393
+ }
394
+ }
356
395
  }
357
396
  if (!switchPart && hasCaret && !isBackspaceKey && !isDeleteKey && !resetPart && lastParseResultHasNoValue) {
358
397
  if (hasDateValueChanged) {
@@ -369,10 +408,13 @@ var DateInput = /** @class */ (function (_super) {
369
408
  this.setSelection(this.selectionBySymbol(symbolForSelection));
370
409
  }
371
410
  else {
372
- this.setPreviousInputEventState(this.keyDownEvent);
411
+ this.restorePreviousInputEventState();
373
412
  }
374
413
  }
375
414
  }
415
+ else {
416
+ this.restorePreviousInputEventState();
417
+ }
376
418
  }
377
419
  else if (this.options.autoSwitchParts && (switchPart || navigationOnly)) {
378
420
  if (!hasCaret) {
@@ -384,6 +426,12 @@ var DateInput = /** @class */ (function (_super) {
384
426
  event: e
385
427
  });
386
428
  this.triggerInputEnd({ event: e });
429
+ if (hasCaret) {
430
+ // a format like "F" can dynamically change the resolved format pattern based on the value, e.g.
431
+ // "Tuesday, February 1, 2022 3:04:05 AM" becomes
432
+ // "Wednesday, February 2, 2022 3:04:05 AM" giving a diff of 2 ("Tuesday".length - "Wednesday".length)
433
+ this.setTextAndFormat();
434
+ }
387
435
  };
388
436
  /**
389
437
  * @hidden
@@ -429,6 +477,8 @@ var DateInput = /** @class */ (function (_super) {
429
477
  }
430
478
  this.keyDownEvent = e;
431
479
  this.previousElementValue = this.element.value;
480
+ var _a = this.selection, start = _a.start, end = _a.end;
481
+ this.previousElementSelection = { start: start, end: end };
432
482
  var autoSwitchKeys = (this.options.autoSwitchKeys || [])
433
483
  .map(function (x) { return x.toString().toLowerCase().trim(); });
434
484
  if (autoSwitchKeys.indexOf(e.keyCode.toString()) >= 0 ||
@@ -438,7 +488,7 @@ var DateInput = /** @class */ (function (_super) {
438
488
  autoSwitchKeys.indexOf(keycode_1.KeyCode.TAB) >= 0 ||
439
489
  autoSwitchKeys.indexOf(keycode_1.KeyCode.TAB.toString()) >= 0;
440
490
  if (isTabKey) {
441
- var _a = this.selection, selectionStart = _a.start, selectionEnd = _a.end;
491
+ var _b = this.selection, selectionStart = _b.start, selectionEnd = _b.end;
442
492
  if (e.shiftKey && isTabKey) {
443
493
  this.switchDateSegment(-1);
444
494
  }
@@ -840,7 +890,6 @@ var DateInput = /** @class */ (function (_super) {
840
890
  * @hidden
841
891
  */
842
892
  DateInput.prototype.setElementValue = function (value) {
843
- this.previousElementValue = this.element.value;
844
893
  this.element.value = value;
845
894
  };
846
895
  /**
@@ -876,23 +925,25 @@ var DateInput = /** @class */ (function (_super) {
876
925
  }
877
926
  /* eslint-enable no-fallthrough */
878
927
  };
879
- DateInput.prototype.setPreviousInputEventState = function (keyDownEvent) {
880
- var _a = this.selection, selectionStart = _a.start, selectionEnd = _a.end;
881
- var selectionOffset = -1;
882
- if (keyDownEvent.keyCode === keycode_1.KeyCode.BACKSPACE) {
883
- selectionOffset = 1;
884
- }
885
- else if (keyDownEvent.keyCode === keycode_1.KeyCode.DELETE) {
886
- selectionOffset = 0;
887
- }
888
- else if (keyDownEvent.keyCode === keycode_1.KeyCode.SPACE) {
889
- selectionOffset = -1;
890
- }
891
- else {
892
- selectionOffset = -1;
893
- }
928
+ /**
929
+ * @hidden
930
+ */
931
+ DateInput.prototype.restorePreviousInputEventState = function () {
932
+ this.restorePreviousElementValue();
933
+ this.restorePreviousElementSelection();
934
+ };
935
+ /**
936
+ * @hidden
937
+ */
938
+ DateInput.prototype.restorePreviousElementValue = function () {
894
939
  this.setElementValue(this.previousElementValue || '');
895
- this.setSelection({ start: selectionStart + selectionOffset, end: selectionEnd + selectionOffset });
940
+ };
941
+ /**
942
+ * @hidden
943
+ */
944
+ DateInput.prototype.restorePreviousElementSelection = function () {
945
+ var _a = this.previousElementSelection, start = _a.start, end = _a.end;
946
+ this.setSelection({ start: start || 0, end: end || 0 });
896
947
  };
897
948
  DateInput.prototype.writeValue = function (value) {
898
949
  this.verifyValue(value);
@@ -900,7 +951,7 @@ var DateInput = /** @class */ (function (_super) {
900
951
  this.refreshElementValue();
901
952
  };
902
953
  DateInput.prototype.verifyValue = function (value) {
903
- if (value && !utils_2.isDate(value)) {
954
+ if (value && !utils_2.isValidDate(value)) {
904
955
  throw new Error("The 'value' should be a valid JavaScript Date instance.");
905
956
  }
906
957
  };
@@ -918,6 +969,7 @@ var DateInput = /** @class */ (function (_super) {
918
969
  element.placeholder = this.options.placeholder;
919
970
  }
920
971
  var newElementValue = !showPlaceholder ? currentText : "";
972
+ this.previousElementValue = this.elementValue;
921
973
  this.setElementValue(newElementValue);
922
974
  if (this.isActive && !this.options.allowCaretMode && this.options.selectNearestSegmentOnFocus) {
923
975
  this.selectNearestSegment(start);
@@ -1001,10 +1053,10 @@ var DateInput = /** @class */ (function (_super) {
1001
1053
  intlService: this.intl,
1002
1054
  formatPlaceholder: this.formatPlaceholder,
1003
1055
  format: this.inputFormat,
1004
- localeId: this.localeId,
1005
1056
  cycleTime: this.options.cycleTime,
1006
1057
  twoDigitYearMax: this.options.twoDigitYearMax,
1007
- autoCorrectParts: this.options.autoCorrectParts
1058
+ autoCorrectParts: this.options.autoCorrectParts,
1059
+ value: this.options.value
1008
1060
  }, options));
1009
1061
  return dateObject;
1010
1062
  };
@@ -55,27 +55,23 @@ exports.approximateStringMatching = function (_a) {
55
55
  Handle the typing over a literal as well.
56
56
  */
57
57
  if ((isInCaretMode &&
58
- newSegmentText.indexOf(oldSegmentText) === 0 && formatPattern[selectionStart - 1]) ||
58
+ (newSegmentText.indexOf(oldSegmentText) === 0 ||
59
+ formatPattern[selectionStart - 1] === constants_1.Constants.formatSeparator)) ||
59
60
  (!isInCaretMode &&
60
61
  (newSegmentText.indexOf(oldSegmentText) === 0 ||
61
62
  formatPattern[selectionStart - 1] === constants_1.Constants.formatSeparator))) {
62
- if (isInCaretMode) {
63
- var symbol = formatPattern[selectionStart - 1];
64
- return [[symbol, newSegmentText[selectionStart - 1]]];
65
- }
66
- else {
67
- var symbol = formatPattern[0];
68
- for (var i = Math.max(0, oldSegmentText.length - 1); i < formatPattern.length; i++) {
69
- if (formatPattern[i] !== constants_1.Constants.formatSeparator) {
70
- symbol = formatPattern[i];
71
- break;
72
- }
63
+ var symbol = formatPattern[0];
64
+ for (var i = Math.max(0, oldSegmentText.length - 1); i < formatPattern.length; i++) {
65
+ if (formatPattern[i] !== constants_1.Constants.formatSeparator) {
66
+ symbol = formatPattern[i];
67
+ break;
73
68
  }
74
- return [[symbol, newSegmentText[selectionStart - 1]]];
75
69
  }
70
+ return [[symbol, newSegmentText[selectionStart - 1]]];
76
71
  }
77
72
  /* Handle the entering of a space or a separator for navigating to the next item. */
78
- if (newSegmentText[newSegmentText.length - 1] === ' ' || newSegmentText[newSegmentText.length - 1] === oldTextSeparator) {
73
+ if ((!isInCaretMode && newSegmentText[newSegmentText.length - 1] === ' ') ||
74
+ (!isInCaretMode && newSegmentText[newSegmentText.length - 1] === oldTextSeparator)) {
79
75
  return [[formatPattern[selectionStart - 1], constants_1.Constants.formatSeparator]];
80
76
  }
81
77
  /* Handle typing over a correctly selected part. */