@skyux/datetime 6.0.0-beta.5 → 6.0.0-beta.8

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.
@@ -2675,10 +2675,13 @@ class SkyDatepickerInputDirective {
2675
2675
  this.dateFormatter = new SkyDateFormatter();
2676
2676
  this.ngUnsubscribe = new Subject();
2677
2677
  // eslint-disable-next-line @typescript-eslint/no-empty-function
2678
+ // istanbul ignore next
2678
2679
  this.onChange = (_) => { };
2679
2680
  // eslint-disable-next-line @typescript-eslint/no-empty-function
2681
+ // istanbul ignore next
2680
2682
  this.onTouched = () => { };
2681
2683
  // eslint-disable-next-line @typescript-eslint/no-empty-function
2684
+ // istanbul ignore next
2682
2685
  this.onValidatorChange = () => { };
2683
2686
  this.initialPlaceholder = this.adapter.getPlaceholder(this.elementRef);
2684
2687
  this.updatePlaceholder();
@@ -2887,9 +2890,40 @@ class SkyDatepickerInputDirective {
2887
2890
  if (!value) {
2888
2891
  return;
2889
2892
  }
2890
- const dateValue = this.getDateValue(value);
2891
- const isDateValid = dateValue && this.dateFormatter.dateIsValid(dateValue);
2892
- if (!isDateValid || !this.isDateStringValid(value)) {
2893
+ if (value instanceof Date) {
2894
+ const isDateValid = this.dateFormatter.dateIsValid(value);
2895
+ if (!isDateValid) {
2896
+ // Mark the invalid control as touched so that the input's invalid CSS styles appear.
2897
+ // (This is only required when the invalid value is set by the FormControl constructor.)
2898
+ this.control.markAsTouched();
2899
+ return {
2900
+ skyDate: {
2901
+ invalid: value,
2902
+ },
2903
+ };
2904
+ }
2905
+ const minDate = this.minDate;
2906
+ if (minDate &&
2907
+ this.dateFormatter.dateIsValid(minDate) &&
2908
+ value < minDate) {
2909
+ return {
2910
+ skyDate: {
2911
+ minDate,
2912
+ },
2913
+ };
2914
+ }
2915
+ const maxDate = this.maxDate;
2916
+ if (maxDate &&
2917
+ this.dateFormatter.dateIsValid(maxDate) &&
2918
+ value > maxDate) {
2919
+ return {
2920
+ skyDate: {
2921
+ maxDate,
2922
+ },
2923
+ };
2924
+ }
2925
+ }
2926
+ else {
2893
2927
  // Mark the invalid control as touched so that the input's invalid CSS styles appear.
2894
2928
  // (This is only required when the invalid value is set by the FormControl constructor.)
2895
2929
  this.control.markAsTouched();
@@ -2899,22 +2933,6 @@ class SkyDatepickerInputDirective {
2899
2933
  },
2900
2934
  };
2901
2935
  }
2902
- const minDate = this.minDate;
2903
- if (minDate && this.dateFormatter.dateIsValid(minDate) && value < minDate) {
2904
- return {
2905
- skyDate: {
2906
- minDate,
2907
- },
2908
- };
2909
- }
2910
- const maxDate = this.maxDate;
2911
- if (maxDate && this.dateFormatter.dateIsValid(maxDate) && value > maxDate) {
2912
- return {
2913
- skyDate: {
2914
- maxDate,
2915
- },
2916
- };
2917
- }
2918
2936
  }
2919
2937
  registerOnChange(fn) {
2920
2938
  this.onChange = fn;
@@ -2950,18 +2968,40 @@ class SkyDatepickerInputDirective {
2950
2968
  setInputElementValue(value) {
2951
2969
  this.renderer.setProperty(this.elementRef.nativeElement, 'value', value);
2952
2970
  }
2971
+ /**
2972
+ * Gets the date value from a value - if possible.
2973
+ * Will not convert unconvertable dates or numbers outside of the current month's number of days.
2974
+ * Returns `undefined` if the value can not be converted.
2975
+ */
2953
2976
  getDateValue(value) {
2954
- let dateValue;
2955
2977
  if (value instanceof Date) {
2956
- dateValue = value;
2978
+ return value;
2957
2979
  }
2958
2980
  else if (typeof value === 'string') {
2981
+ return this.getShortcutOrDateValue(value);
2982
+ }
2983
+ }
2984
+ /**
2985
+ * Converts a string to a date object if the string is a valid date string.
2986
+ * It will also convert numeric input to a date if that number is within the current month's number of days.
2987
+ * If the string can not be converted, `undefined` be returned.
2988
+ */
2989
+ getShortcutOrDateValue(value) {
2990
+ const num = Number(value);
2991
+ if (Number.isInteger(num)) {
2992
+ const now = new Date();
2993
+ const shortcutDate = new Date(now.getFullYear(), now.getMonth(), num);
2994
+ const daysInMonth = shortcutDate.getDate();
2995
+ if (num > 0 && num <= daysInMonth) {
2996
+ return shortcutDate;
2997
+ }
2998
+ }
2999
+ else {
2959
3000
  const date = this.dateFormatter.getDateFromString(value, this.dateFormat, this.strict);
2960
3001
  if (this.dateFormatter.dateIsValid(date)) {
2961
- dateValue = date;
3002
+ return date;
2962
3003
  }
2963
3004
  }
2964
- return dateValue;
2965
3005
  }
2966
3006
  /**
2967
3007
  * Validates the input value to ensure it is formatted correctly.
@@ -2994,10 +3034,6 @@ class SkyDatepickerInputDirective {
2994
3034
  if (this._value === value) {
2995
3035
  return;
2996
3036
  }
2997
- const dateValue = this.getDateValue(value);
2998
- const areDatesEqual = this._value instanceof Date &&
2999
- dateValue &&
3000
- dateValue.getTime() === this._value.getTime();
3001
3037
  const isValidDateString = this.isDateStringValid(value);
3002
3038
  // If the string value supplied is malformed, do not set the value to its Date equivalent.
3003
3039
  // (JavaScript's Date parser will convert poorly formatted dates to Date objects, such as "abc 123", which isn't ideal.)
@@ -3010,23 +3046,32 @@ class SkyDatepickerInputDirective {
3010
3046
  this.control?.setValue(this._value, { emitEvent: false });
3011
3047
  }
3012
3048
  this.datepickerComponent.selectedDate = this._value;
3049
+ this.setInputElementValue(value || '');
3013
3050
  }
3014
- else if (dateValue !== this._value || !areDatesEqual) {
3015
- this._value = dateValue || value;
3016
- if (emitEvent) {
3017
- this.onChange(this._value);
3051
+ else {
3052
+ // This value represents the date value for the input if possible.
3053
+ // This value will take into account all shortcut functionality.
3054
+ const dateValue = this.getDateValue(value);
3055
+ const areDatesEqual = this._value instanceof Date &&
3056
+ dateValue &&
3057
+ dateValue.getTime() === this._value.getTime();
3058
+ if (dateValue !== this._value || !areDatesEqual) {
3059
+ this._value = dateValue || value;
3060
+ if (emitEvent) {
3061
+ this.onChange(this._value);
3062
+ }
3063
+ else {
3064
+ this.control?.setValue(this._value, { emitEvent: false });
3065
+ }
3066
+ this.datepickerComponent.selectedDate = this._value;
3067
+ }
3068
+ if (dateValue) {
3069
+ const formattedDateString = this.dateFormatter.format(dateValue, this.dateFormat);
3070
+ this.setInputElementValue(formattedDateString);
3018
3071
  }
3019
3072
  else {
3020
- this.control?.setValue(this._value, { emitEvent: false });
3073
+ this.setInputElementValue(value || '');
3021
3074
  }
3022
- this.datepickerComponent.selectedDate = this._value;
3023
- }
3024
- if (dateValue && isValidDateString) {
3025
- const formattedDate = this.dateFormatter.format(dateValue, this.dateFormat);
3026
- this.setInputElementValue(formattedDate);
3027
- }
3028
- else {
3029
- this.setInputElementValue(value || '');
3030
3075
  }
3031
3076
  }
3032
3077
  }