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