td-stylekit 28.30.0 → 28.30.2

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [28.30.2](https://github.com/treasure-data/td-stylekit/compare/v28.30.1...v28.30.2) (2023-12-15)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **CON-11008:** Fix issue in TimeControl ([#1517](https://github.com/treasure-data/td-stylekit/issues/1517)) ([85fdca4](https://github.com/treasure-data/td-stylekit/commit/85fdca4))
7
+
8
+ ## [28.30.1](https://github.com/treasure-data/td-stylekit/compare/v28.30.0...v28.30.1) (2023-12-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **CON-11008:** Complete migration from momentjs to date-fns ([#1514](https://github.com/treasure-data/td-stylekit/issues/1514)) ([9956296](https://github.com/treasure-data/td-stylekit/commit/9956296))
14
+
1
15
  # [28.30.0](https://github.com/treasure-data/td-stylekit/compare/v28.29.0...v28.30.0) (2023-12-14)
2
16
 
3
17
 
@@ -65,11 +65,12 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
65
65
  if (typeof window !== "undefined" && !window.gs) window.gs = function () {};
66
66
  if (typeof window !== "undefined" && !window.gsC) window.gsC = function () {};
67
67
  var dateToString = _DatePicker.DatePickerUtils.dateToString,
68
- stringToDate = _DatePicker.DatePickerUtils.stringToDate;
68
+ stringToDate = _DatePicker.DatePickerUtils.stringToDate,
69
+ isStringCorrectFormat = _DatePicker.DatePickerUtils.isStringCorrectFormat;
69
70
  var CLOSE_KEYS = [String.fromCharCode(25), // Gets sent when you press shift+tab with Chrome...
70
71
  'Enter', 'Escape', 'Tab'];
71
72
  exports.CLOSE_KEYS = CLOSE_KEYS;
72
- var DATE_FORMATS = ['MMM DD, YYYY', 'MMM DD YYYY', 'MMM D, YYYY', 'MMM D YYYY', 'MMMM D, YYYY', 'MMMM D YYYY', 'MMMM DD, YYYY', 'MMMM DD YYYY', 'MM/DD/YYYY', 'l'];
73
+ var DATE_FORMATS = ['MMM dd, yyyy', 'MMM dd yyyy', 'MMM d, yyyy', 'MMM d yyyy', 'MMMM d, yyyy', 'MMMM d yyyy', 'MMMM dd, yyyy', 'MMMM dd yyyy', 'MM/dd/yyyy'];
73
74
  exports.DATE_FORMATS = DATE_FORMATS;
74
75
  var TIME_FORMAT_24 = 'HH:mm:ss';
75
76
  var TIME_FORMAT_12 = 'hh:mm:ss A';
@@ -78,7 +79,7 @@ var TIME_FORMAT_SHORT_12 = 'hh:mm A';
78
79
  var TIME_FORMAT_MS = ':mm:ss';
79
80
 
80
81
  var DATETIME_FORMAT = function DATETIME_FORMAT(timeFormat) {
81
- return ["MMM DD, YYYY ".concat(timeFormat), "Format MMM DD, YYYY ".concat(timeFormat)];
82
+ return ["MMM dd, yyyy ".concat(timeFormat), "Format MMM dd, yyyy ".concat(timeFormat)];
82
83
  };
83
84
 
84
85
  exports.DATETIME_FORMAT = DATETIME_FORMAT;
@@ -132,7 +133,7 @@ var UnstyledDateControl = function UnstyledDateControl(_ref) {
132
133
  _ref$clearable = _ref.clearable,
133
134
  clearable = _ref$clearable === void 0 ? true : _ref$clearable,
134
135
  _ref$dateDisplayForma = _ref.dateDisplayFormat,
135
- dateDisplayFormat = _ref$dateDisplayForma === void 0 ? 'MMM DD, YYYY' : _ref$dateDisplayForma,
136
+ dateDisplayFormat = _ref$dateDisplayForma === void 0 ? 'MMM dd, yyyy' : _ref$dateDisplayForma,
136
137
  disabledDays = _ref.disabledDays,
137
138
  dataInstrumentation = _ref['data-instrumentation'],
138
139
  _ref$enableFutureDate = _ref.enableFutureDates,
@@ -293,7 +294,7 @@ var UnstyledDateControl = function UnstyledDateControl(_ref) {
293
294
  var onInputChange = function onInputChange(_ref2) {
294
295
  var name = _ref2.name,
295
296
  value = _ref2.value;
296
- var date = typeof value === 'string' ? stringToDate(value, [resolvedDisplayFormat].concat(DATE_FORMATS), locale) : null;
297
+ var date = typeof value === 'string' && isStringCorrectFormat(value, [resolvedDisplayFormat].concat(DATE_FORMATS)) ? stringToDate(value, [resolvedDisplayFormat].concat(DATE_FORMATS), locale) : null;
297
298
  setInputValue(String(value)); // If we have a validation function and it's test doesn't pass then
298
299
  // we don't want to call onChange, but we want to keep local state
299
300
  // up to date so that the input respects the users inputs
@@ -344,7 +345,7 @@ var UnstyledDateControl = function UnstyledDateControl(_ref) {
344
345
 
345
346
  (0, _react.useEffect)(function () {
346
347
  var errorList = error ? (0, _utils.getErrorList)(error) : [];
347
- var date = stringToDate(inputValue, [resolvedDisplayFormat].concat(DATE_FORMATS), locale);
348
+ var date = isStringCorrectFormat(inputValue, [resolvedDisplayFormat].concat(DATE_FORMATS)) ? stringToDate(inputValue, [resolvedDisplayFormat].concat(DATE_FORMATS), locale) : undefined;
348
349
 
349
350
  if (inputValue && !date) {
350
351
  errorList.unshift(errorMessage);
@@ -70,7 +70,8 @@ if (typeof window !== "undefined" && !window.gsC) window.gsC = function () {};
70
70
  // this level of fine grained instrumentation is not needed
71
71
 
72
72
  var dateToString = _DatePicker.DatePickerUtils.dateToString,
73
- stringToDate = _DatePicker.DatePickerUtils.stringToDate;
73
+ stringToDate = _DatePicker.DatePickerUtils.stringToDate,
74
+ isStringCorrectFormat = _DatePicker.DatePickerUtils.isStringCorrectFormat;
74
75
  var CLOSE_KEYS = [String.fromCharCode(25), // Gets sent when you press shift+tab with Chrome...
75
76
  'Enter', 'Escape', 'Tab']; // @TODO: Change to YYYY-MM-DD?
76
77
 
@@ -163,7 +164,7 @@ var UnstyledDateField = /*#__PURE__*/function (_Component) {
163
164
  _defineProperty(_assertThisInitialized(_this), "_onInputChange", function (_ref) {
164
165
  var name = _ref.name,
165
166
  value = _ref.value;
166
- var date = typeof value === 'string' ? stringToDate(value) : null;
167
+ var date = typeof value === 'string' && isStringCorrectFormat(value) ? stringToDate(value) : null;
167
168
  var _this$props2 = _this.props,
168
169
  onChange = _this$props2.onChange,
169
170
  disabledDays = _this$props2.disabledDays,
@@ -1,15 +1,13 @@
1
1
  import { LocaleUtils } from 'react-day-picker';
2
- import 'moment/locale/ja';
3
- import 'moment/locale/ko';
4
- import 'moment/locale/es';
5
- import 'moment/locale/de';
6
2
  type Value = string | number | Date;
7
3
  /** Strings and Dates are converted using this format (eg. 6/3/2015) */
8
- export declare const MOMENT_JS_FORMAT = "l";
4
+ export declare const DEFAULT_FORMAT = "M/d/yyyy";
9
5
  /** Does the specified String represent a Date in the supported format? */
10
6
  export declare function isDateValid(value: Value, format?: string | string[], locale?: string): boolean;
11
7
  /** Convert the String specified into a Date if it valid */
12
8
  export declare function stringToDate(string: Value, format?: string | string[], locale?: string): Date | undefined;
9
+ /** Verify that the length of the provided string value matches the length of one of the provided formats */
10
+ export declare function isStringCorrectFormat(string: string, format?: string | string[]): boolean;
13
11
  /** Convert the specified Date to a String */
14
12
  export declare function dateToString(date: Value, format?: string, locale?: string): string;
15
13
  /** Custom locale utils used to override day headers */
@@ -17,7 +15,6 @@ export declare const customLocaleUtils: typeof LocaleUtils;
17
15
  export declare const dateUtils: {
18
16
  addDayToRange(day: Date, range: import("react-day-picker").RangeModifier): import("react-day-picker").RangeModifier;
19
17
  addMonths(d: Date, n: number): Date;
20
- /** Convert the String specified into a Date if it valid */
21
18
  clone(d: Date): Date;
22
19
  isDate(d: Date): boolean;
23
20
  isDayAfter(day1: Date, day2: Date): boolean;
@@ -3,27 +3,18 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.customLocaleUtils = exports.MOMENT_JS_FORMAT = void 0;
6
+ exports.customLocaleUtils = exports.DEFAULT_FORMAT = void 0;
7
7
  exports.dateToString = dateToString;
8
8
  exports.dateUtils = void 0;
9
9
  exports.isDateValid = isDateValid;
10
+ exports.isStringCorrectFormat = isStringCorrectFormat;
10
11
  exports.stringToDate = stringToDate;
11
12
 
12
13
  var _reactDayPicker = require("react-day-picker");
13
14
 
14
- var _moment = _interopRequireDefault(require("moment"));
15
+ var _dateFns = require("date-fns");
15
16
 
16
- var _moment2 = _interopRequireDefault(require("react-day-picker/moment"));
17
-
18
- require("moment/locale/ja");
19
-
20
- require("moment/locale/ko");
21
-
22
- require("moment/locale/es");
23
-
24
- require("moment/locale/de");
25
-
26
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
17
+ var _locale = require("date-fns/locale");
27
18
 
28
19
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
29
20
 
@@ -32,46 +23,128 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
32
23
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
33
24
 
34
25
  if (typeof window !== "undefined" && !window.gs) window.gs = function () {};
35
- if (typeof window !== "undefined" && !window.gsC) window.gsC = function () {}; // Make sure moment.js has the required locale data
36
-
37
- _moment["default"].locale('en');
26
+ if (typeof window !== "undefined" && !window.gsC) window.gsC = function () {};
38
27
 
39
28
  /** Strings and Dates are converted using this format (eg. 6/3/2015) */
40
- var MOMENT_JS_FORMAT = 'l';
29
+ var DEFAULT_FORMAT = 'M/d/yyyy';
30
+ exports.DEFAULT_FORMAT = DEFAULT_FORMAT;
31
+
32
+ var getLocale = function getLocale(key) {
33
+ if (key === 'ja') {
34
+ return _locale.ja;
35
+ } else if (key === 'ko') {
36
+ return _locale.ko;
37
+ } else if (key === 'es') {
38
+ return _locale.es;
39
+ } else if (key === 'de') {
40
+ return _locale.de;
41
+ }
42
+
43
+ return _locale.enUS;
44
+ };
41
45
  /** Does the specified String represent a Date in the supported format? */
42
46
 
43
- exports.MOMENT_JS_FORMAT = MOMENT_JS_FORMAT;
44
47
 
45
48
  function isDateValid(value) {
46
- var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : MOMENT_JS_FORMAT;
49
+ var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_FORMAT;
47
50
  var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en';
48
- return (0, _moment["default"])(value, format, locale, true).isValid();
51
+ var parsedDate;
52
+
53
+ if (Array.isArray(format)) {
54
+ for (var i = 0; i < format.length; i++) {
55
+ parsedDate = (0, _dateFns.parse)(value.toString(), format[i], Date.now(), {
56
+ locale: getLocale(locale)
57
+ });
58
+
59
+ if ((0, _dateFns.isValid)(parsedDate)) {
60
+ return true;
61
+ }
62
+ }
63
+ } else {
64
+ parsedDate = (0, _dateFns.parse)(value.toString(), format, Date.now(), {
65
+ locale: getLocale(locale)
66
+ });
67
+ return (0, _dateFns.isValid)(parsedDate);
68
+ }
69
+
70
+ return false;
49
71
  }
72
+
73
+ var getValidFormat = function getValidFormat(value, format) {
74
+ var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en';
75
+ var result;
76
+
77
+ for (var i = 0; i < format.length; i++) {
78
+ result = (0, _dateFns.parse)(value.toString(), format[i], Date.now(), {
79
+ locale: getLocale(locale)
80
+ });
81
+
82
+ if ((0, _dateFns.isValid)(result)) {
83
+ return format[i];
84
+ }
85
+ }
86
+
87
+ return undefined;
88
+ };
50
89
  /** Convert the String specified into a Date if it valid */
51
90
 
52
91
 
53
92
  function stringToDate(string) {
54
- var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : MOMENT_JS_FORMAT;
93
+ var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_FORMAT;
55
94
  var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en';
56
95
 
57
96
  if (isDateValid(string, format, locale)) {
58
- return (0, _moment["default"])(string, format, locale).toDate();
97
+ var validFormat = Array.isArray(format) ? getValidFormat(string, format, locale) : format;
98
+
99
+ if (!validFormat) {
100
+ return undefined;
101
+ }
102
+
103
+ return (0, _dateFns.parse)(string.toString(), validFormat, Date.now(), {
104
+ locale: getLocale(locale)
105
+ });
59
106
  }
60
107
 
61
108
  return undefined;
62
109
  }
110
+ /** Verify that the length of the provided string value matches the length of one of the provided formats */
111
+
112
+
113
+ function isStringCorrectFormat(string) {
114
+ var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_FORMAT;
115
+
116
+ if (Array.isArray(format)) {
117
+ for (var i = 0; i < format.length; i++) {
118
+ var date = (0, _dateFns.parse)(string.toString(), format[i], Date.now());
119
+
120
+ if (date.toString() !== 'Invalid Date' && (0, _dateFns.format)(date, format[i]) === string) {
121
+ return true;
122
+ }
123
+ }
124
+ } else {
125
+ var _date = (0, _dateFns.parse)(string.toString(), format, Date.now());
126
+
127
+ if (_date.toString() !== 'Invalid Date' && (0, _dateFns.format)(_date, format) === string) {
128
+ return true;
129
+ }
130
+ }
131
+
132
+ return false;
133
+ }
63
134
  /** Convert the specified Date to a String */
64
135
 
65
136
 
66
137
  function dateToString(date) {
67
- var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : MOMENT_JS_FORMAT;
138
+ var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_FORMAT;
68
139
  var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'en';
69
- return (0, _moment["default"])(date).locale(locale).format(format);
140
+ return (0, _dateFns.format)(new Date(date), format, {
141
+ locale: getLocale(locale)
142
+ });
70
143
  }
71
144
  /** Custom locale utils used to override day headers */
72
145
 
73
146
 
74
- var customLocaleUtils = _objectSpread(_objectSpread({}, _moment2["default"]), {}, {
147
+ var customLocaleUtils = _objectSpread(_objectSpread({}, _reactDayPicker.LocaleUtils), {}, {
75
148
  formatWeekdayShort: function formatWeekdayShort(dayIndex, locale) {
76
149
  return _reactDayPicker.LocaleUtils.formatWeekdayShort(dayIndex, locale).substr(0, 1);
77
150
  }
@@ -7,9 +7,7 @@ exports.useDisplayValue = useDisplayValue;
7
7
 
8
8
  var _react = require("react");
9
9
 
10
- var _moment = _interopRequireDefault(require("moment"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
+ var _dateFns = require("date-fns");
13
11
 
14
12
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
15
13
 
@@ -25,7 +23,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
25
23
 
26
24
  if (typeof window !== "undefined" && !window.gs) window.gs = function () {};
27
25
  if (typeof window !== "undefined" && !window.gsC) window.gsC = function () {};
28
- var TIME_FORMAT_24 = 'HH:mm:ss';
26
+ var TIME_FORMAT_24 = 'hh:mm:ss';
29
27
  var TIME_FORMAT_12 = 'hh:mm:ss A';
30
28
  var TIME_FORMAT_MS = ':mm:ss';
31
29
  var TIME_FORMAT_HM = 'hh:mm'; // This hook contains all the logic needed to handle user input
@@ -68,7 +66,11 @@ function useDisplayValue(time, editor, onChange) {
68
66
  }, [period, editor]);
69
67
 
70
68
  var reconcileShadowValue = function reconcileShadowValue() {
71
- return setShadowValue((0, _moment["default"])(time, TIME_FORMAT_24).format(displayFormat));
69
+ var date = (0, _dateFns.parse)(time !== null && time !== void 0 ? time : '00:00', TIME_FORMAT_24, Date.now());
70
+
71
+ if (time && (0, _dateFns.isValid)(date)) {
72
+ setShadowValue((0, _dateFns.format)(date, displayFormat));
73
+ }
72
74
  }; // If we do not have an initial value then we use the current time (when editing
73
75
  // hh:mm:ss) or always 00:00 when editing mm:ss. We override default behaviour of TimePicker.
74
76
 
@@ -76,7 +78,7 @@ function useDisplayValue(time, editor, onChange) {
76
78
  (0, _react.useEffect)(function () {
77
79
  if (!time && onChange) {
78
80
  if (editor === 'hours-minutes-seconds') {
79
- onChange((0, _moment["default"])().format(TIME_FORMAT_24));
81
+ onChange((0, _dateFns.format)(new Date(), TIME_FORMAT_24));
80
82
  } else {
81
83
  onChange('00:00');
82
84
  }
@@ -110,19 +112,19 @@ function useDisplayValue(time, editor, onChange) {
110
112
  return;
111
113
  }
112
114
 
113
- var time = (0, _moment["default"])(value, displayFormat, true);
115
+ var date = new Date(value);
114
116
 
115
- if (time.isValid()) {
117
+ if ((0, _dateFns.isValid)(date)) {
116
118
  setErrors(undefined);
117
- onChange(time.format(TIME_FORMAT_24)); // Editing the value from AM to PM (or vice-versa) causes the default
119
+ onChange((0, _dateFns.format)(date, TIME_FORMAT_24)); // Editing the value from AM to PM (or vice-versa) causes the default
118
120
  // period in the TimePicker to change (we do not want to always have 24h if user
119
121
  // changed something). Note that this works only because the TimePicker is unmounted
120
122
  // when user starts typing something.
121
123
 
122
124
  if (period !== '24' && editor === 'hours-minutes-seconds') {
123
- if (time.hour() > 11 && period === 'am') {
125
+ if ((0, _dateFns.getHours)(date) > 11 && period === 'am') {
124
126
  setPeriod('pm');
125
- } else if (time.hour() < 12 && period === 'pm') {
127
+ } else if ((0, _dateFns.getHours)(date) < 12 && period === 'pm') {
126
128
  setPeriod('am');
127
129
  }
128
130
  }
@@ -151,8 +153,8 @@ function useDisplayValue(time, editor, onChange) {
151
153
  return shadowValue;
152
154
  }
153
155
 
154
- var result = (0, _moment["default"])(time, TIME_FORMAT_24);
155
- return result.isValid() ? result.format(displayFormat) : ''; // eslint-disable-next-line react-hooks/exhaustive-deps
156
+ var date = new Date(time);
157
+ return (0, _dateFns.isValid)(date) ? (0, _dateFns.format)(date, displayFormat) : ''; // eslint-disable-next-line react-hooks/exhaustive-deps
156
158
  }, [time, shadowValue, errors, isEditing, displayFormat]);
157
159
  return {
158
160
  displayValue: displayValue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "td-stylekit",
3
- "version": "28.30.0",
3
+ "version": "28.30.2",
4
4
  "main": "dist/es/index.js",
5
5
  "module": "dist/es/index.js",
6
6
  "types": "dist/es/index.d.ts",
@@ -199,7 +199,6 @@
199
199
  "lcov-parse": "^1.0.0",
200
200
  "markdown-table": "^1.1.3",
201
201
  "marksy": "^8.0.0",
202
- "moment": "^2.29.4",
203
202
  "node-fetch": "^2.6.7",
204
203
  "prettier": "^2.1.1",
205
204
  "pretty-quick": "^1.11.1",
@@ -223,7 +222,6 @@
223
222
  "typescript": "^5.0.2"
224
223
  },
225
224
  "peerDependencies": {
226
- "moment": "^2.18.1",
227
225
  "react": "18.2.0",
228
226
  "react-dnd": "^9.4.0",
229
227
  "react-dom": "18.2.0",