@commercetools-uikit/date-range-input 0.0.0-FCT-1500-adjust-legacy-css-reset-20250527090339

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.
@@ -0,0 +1,441 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _Reflect$construct = require('@babel/runtime-corejs3/core-js-stable/reflect/construct');
6
+ var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
7
+ var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
8
+ var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
9
+ var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
10
+ var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
11
+ var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
12
+ var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
13
+ var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
14
+ var _classCallCheck = require('@babel/runtime-corejs3/helpers/classCallCheck');
15
+ var _createClass = require('@babel/runtime-corejs3/helpers/createClass');
16
+ var _possibleConstructorReturn = require('@babel/runtime-corejs3/helpers/possibleConstructorReturn');
17
+ var _getPrototypeOf = require('@babel/runtime-corejs3/helpers/getPrototypeOf');
18
+ var _inherits = require('@babel/runtime-corejs3/helpers/inherits');
19
+ require('prop-types');
20
+ var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
21
+ var _mapInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/map');
22
+ var _trimInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/trim');
23
+ var _sortInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/sort');
24
+ var _fillInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/fill');
25
+ var react = require('react');
26
+ var Downshift = require('downshift');
27
+ var reactIntl = require('react-intl');
28
+ var Constraints = require('@commercetools-uikit/constraints');
29
+ var utils = require('@commercetools-uikit/utils');
30
+ var calendarUtils = require('@commercetools-uikit/calendar-utils');
31
+ var calendarTimeUtils = require('@commercetools-uikit/calendar-time-utils');
32
+ var jsxRuntime = require('@emotion/react/jsx-runtime');
33
+
34
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
35
+
36
+ var _Reflect$construct__default = /*#__PURE__*/_interopDefault(_Reflect$construct);
37
+ var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
38
+ var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
39
+ var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
40
+ var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
41
+ var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
42
+ var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
43
+ var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
44
+ var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
45
+ var _mapInstanceProperty__default = /*#__PURE__*/_interopDefault(_mapInstanceProperty);
46
+ var _trimInstanceProperty__default = /*#__PURE__*/_interopDefault(_trimInstanceProperty);
47
+ var _sortInstanceProperty__default = /*#__PURE__*/_interopDefault(_sortInstanceProperty);
48
+ var _fillInstanceProperty__default = /*#__PURE__*/_interopDefault(_fillInstanceProperty);
49
+ var Downshift__default = /*#__PURE__*/_interopDefault(Downshift);
50
+ var Constraints__default = /*#__PURE__*/_interopDefault(Constraints);
51
+
52
+ function ownKeys(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; }
53
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context4, _context5; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context4 = ownKeys(Object(t), !0)).call(_context4, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context5 = ownKeys(Object(t))).call(_context5, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
54
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? _Reflect$construct__default["default"](o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
55
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(_Reflect$construct__default["default"](Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function () { return !!t; })(); }
56
+ const preventDownshiftDefault = event => {
57
+ event.nativeEvent.preventDownshiftDefault = true;
58
+ };
59
+ const parseRangeText = (text, locale) => {
60
+ var _context, _context2;
61
+ const parts = _filterInstanceProperty__default["default"](_context = _mapInstanceProperty__default["default"](_context2 = text.split(' - ')).call(_context2, part => {
62
+ const parsedDate = calendarUtils.parseInputToDate(_trimInstanceProperty__default["default"](part).call(part), locale);
63
+ return parsedDate === '' ? null : parsedDate;
64
+ })).call(_context, Boolean);
65
+ return parts;
66
+ };
67
+ const isSameRange = (a, b) => {
68
+ if (a.length !== b.length) return false;
69
+ if (a.length === 0) return true;
70
+ if (a[0] === b[0] && a[1] === b[1]) return true;
71
+ return false;
72
+ };
73
+ const getRange = _ref => {
74
+ let item = _ref.item,
75
+ value = _ref.value,
76
+ startDate = _ref.startDate,
77
+ highlightedItem = _ref.highlightedItem;
78
+ const isRangeSelectionInProgress = startDate;
79
+ const hasSelection = value.length === 2;
80
+ const isStartDate = calendarUtils.isSameDay(item, startDate);
81
+ const isBetween = highlightedItem && calendarUtils.isBetween(item, startDate, highlightedItem);
82
+ let isRangeStart = false;
83
+ if (isRangeSelectionInProgress) {
84
+ isRangeStart = isStartDate;
85
+ } else if (hasSelection) {
86
+ isRangeStart = value[0] === item;
87
+ }
88
+ let isRangeBetween = false;
89
+ if (isRangeSelectionInProgress) {
90
+ isRangeBetween = isBetween;
91
+ } else if (hasSelection) {
92
+ isRangeBetween = calendarUtils.isBetween(item, value[0], value[1]);
93
+ }
94
+ let isRangeEnd = false;
95
+ if (isRangeSelectionInProgress) {
96
+ isRangeEnd = item === highlightedItem;
97
+ } else if (hasSelection) {
98
+ isRangeEnd = value[1] === item;
99
+ }
100
+ return {
101
+ isRangeStart,
102
+ isRangeBetween,
103
+ isRangeEnd
104
+ };
105
+ };
106
+ let DateRangeInput = /*#__PURE__*/function (_Component) {
107
+ function DateRangeInput() {
108
+ var _this;
109
+ _classCallCheck(this, DateRangeInput);
110
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
111
+ args[_key] = arguments[_key];
112
+ }
113
+ _this = _callSuper(this, DateRangeInput, [...args]);
114
+ _this.inputRef = /*#__PURE__*/react.createRef();
115
+ _this.state = {
116
+ calendarDate: _this.props.value.length === 2 ? _this.props.value[0] : calendarUtils.getToday(),
117
+ suggestedItems: [],
118
+ startDate: null,
119
+ highlightedIndex: null,
120
+ isOpen: false,
121
+ inputValue: calendarUtils.formatRange(_this.props.value, _this.props.intl.locale),
122
+ prevValue: _this.props.value,
123
+ prevLocale: _this.props.intl.locale
124
+ };
125
+ _this.jumpMonth = function (amount) {
126
+ let dayToHighlight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
127
+ _this.setState(prevState => {
128
+ const nextDate = calendarUtils.changeMonth(prevState.calendarDate, amount);
129
+ return {
130
+ calendarDate: nextDate,
131
+ highlightedIndex: dayToHighlight
132
+ };
133
+ });
134
+ };
135
+ _this.showToday = () => {
136
+ const today = calendarUtils.getToday();
137
+ _this.setState(prevState => ({
138
+ calendarDate: today,
139
+ highlightedIndex: prevState.suggestedItems.length + calendarUtils.getDateInMonth(today) - 1
140
+ }), () => _this.inputRef.current?.focus());
141
+ };
142
+ _this.handleBlur = () => {
143
+ if (_this.props.onBlur) _this.props.onBlur({
144
+ target: {
145
+ id: _this.props.id,
146
+ name: _this.props.name
147
+ }
148
+ });
149
+ };
150
+ _this.emit = unsortedRange => {
151
+ _this.props.onChange?.({
152
+ target: {
153
+ id: _this.props.id,
154
+ name: _this.props.name,
155
+ value: _sortInstanceProperty__default["default"](unsortedRange).call(unsortedRange)
156
+ }
157
+ });
158
+ };
159
+ return _this;
160
+ }
161
+ _inherits(DateRangeInput, _Component);
162
+ return _createClass(DateRangeInput, [{
163
+ key: "render",
164
+ value: function render() {
165
+ return jsxRuntime.jsx(Constraints__default["default"].Horizontal, {
166
+ max: this.props.horizontalConstraint,
167
+ children: jsxRuntime.jsx(Downshift__default["default"], {
168
+ inputId: this.props.id,
169
+ itemToString: calendarUtils.createItemRangeToString(this.props.intl.locale),
170
+ inputValue: this.state.inputValue,
171
+ selectedItem: null,
172
+ highlightedIndex: this.state.highlightedIndex,
173
+ onInputValueChange: (inputValue, changes) => {
174
+ // only attempt to parse input when the user typed into the input
175
+ // field
176
+ // @ts-ignore
177
+ if (changes.type !== Downshift__default["default"].stateChangeTypes.changeInput) return;
178
+ this.setState(() => {
179
+ const parsedRange = parseRangeText(inputValue, this.props.intl.locale);
180
+ if (parsedRange.length === 0) return {
181
+ suggestedItems: [],
182
+ highlightedIndex: null,
183
+ inputValue,
184
+ startDate: null
185
+ };
186
+ if (parsedRange.length === 1) {
187
+ const calendarDate = parsedRange[0];
188
+ return {
189
+ suggestedItems: [],
190
+ highlightedIndex: calendarUtils.getDateInMonth(calendarDate) - 1,
191
+ inputValue,
192
+ startDate: parsedRange[0],
193
+ calendarDate
194
+ };
195
+ }
196
+ if (parsedRange.length === 2) {
197
+ const calendarDate = parsedRange[1];
198
+ return {
199
+ suggestedItems: [],
200
+ highlightedIndex: calendarUtils.getDateInMonth(calendarDate) - 1,
201
+ inputValue,
202
+ startDate: parsedRange[0],
203
+ calendarDate
204
+ };
205
+ }
206
+ return null;
207
+ });
208
+ },
209
+ onStateChange: changes => {
210
+ this.setState(prevState => {
211
+ if (changes.type === Downshift__default["default"].stateChangeTypes.mouseUp || changes.type === Downshift__default["default"].stateChangeTypes.blurInput) {
212
+ return {
213
+ highlightedIndex: null,
214
+ isOpen: false,
215
+ inputValue: calendarUtils.formatRange(this.props.value, this.props.intl.locale)
216
+ };
217
+ }
218
+ if (changes.hasOwnProperty('selectedItem')) {
219
+ const hasStartedRangeSelection = Boolean(!prevState.startDate && changes.selectedItem);
220
+ const hasFinishedRangeSelection = Boolean(prevState.startDate && changes.selectedItem);
221
+ return {
222
+ highlightedIndex: prevState.highlightedIndex,
223
+ startDate: prevState.startDate ? null : changes.selectedItem,
224
+ calendarDate: changes.selectedItem,
225
+ isOpen: !hasFinishedRangeSelection,
226
+ inputValue: (() => {
227
+ if (hasFinishedRangeSelection) {
228
+ return calendarUtils.formatRange([prevState.startDate, changes.selectedItem], this.props.intl.locale);
229
+ }
230
+ if (hasStartedRangeSelection) {
231
+ return calendarUtils.formatRange([changes.selectedItem], this.props.intl.locale);
232
+ }
233
+ return '';
234
+ })()
235
+ };
236
+ }
237
+ if (changes.hasOwnProperty('isOpen')) {
238
+ return {
239
+ isOpen: changes.isOpen,
240
+ highlightedIndex: changes.highlightedIndex || null,
241
+ inputValue: changes.inputValue || prevState.inputValue,
242
+ // Reset range selection progress when menu opens/closes
243
+ startDate: null,
244
+ // Ensure calendar opens on selected date.
245
+ // Open on the current day as a fallback.
246
+ calendarDate: this.props.value.length === 2 ? this.props.value[0] : calendarUtils.getToday()
247
+ };
248
+ }
249
+ if (changes.hasOwnProperty('highlightedIndex')) {
250
+ return {
251
+ highlightedIndex: changes.highlightedIndex
252
+ };
253
+ }
254
+ return null;
255
+ });
256
+ },
257
+ onChange: selectedItem => {
258
+ if (this.state.startDate && selectedItem) {
259
+ this.emit([this.state.startDate, selectedItem]);
260
+ } else {
261
+ this.emit([]);
262
+ }
263
+ },
264
+ isOpen: this.state.isOpen,
265
+ children: _ref2 => {
266
+ var _context3;
267
+ let getInputProps = _ref2.getInputProps,
268
+ getMenuProps = _ref2.getMenuProps,
269
+ getItemProps = _ref2.getItemProps,
270
+ getToggleButtonProps = _ref2.getToggleButtonProps,
271
+ clearSelection = _ref2.clearSelection,
272
+ highlightedIndex = _ref2.highlightedIndex,
273
+ openMenu = _ref2.openMenu,
274
+ setHighlightedIndex = _ref2.setHighlightedIndex,
275
+ isOpen = _ref2.isOpen,
276
+ inputValue = _ref2.inputValue;
277
+ const calendarItems = calendarUtils.createCalendarItems(this.state.calendarDate);
278
+ const allItems = [...this.state.suggestedItems, ...calendarItems];
279
+ const paddingDayCount = calendarUtils.getPaddingDayCount(this.state.calendarDate, this.props.intl.locale);
280
+ const paddingDays = _fillInstanceProperty__default["default"](_context3 = Array(paddingDayCount)).call(_context3, undefined);
281
+ const weekdays = calendarUtils.getWeekdayNames(this.props.intl.locale);
282
+ const today = calendarUtils.getToday();
283
+ return jsxRuntime.jsxs("div", {
284
+ onFocus: this.props.onFocus,
285
+ onBlur: this.handleBlur,
286
+ children: [jsxRuntime.jsx(calendarUtils.CalendarBody, {
287
+ inputRef: this.inputRef,
288
+ inputProps: getInputProps(_objectSpread({
289
+ /* ARIA */
290
+ 'aria-invalid': this.props['aria-invalid'],
291
+ 'aria-errormessage': this.props['aria-errormessage'],
292
+ // Unset the aria-labelledby as it interfers with the link
293
+ // between the <label for> and the <input id>.
294
+ 'aria-labelledby': undefined,
295
+ name: this.props.name,
296
+ placeholder: typeof this.props.placeholder === 'string' ? this.props.placeholder : `${calendarTimeUtils.getLocalizedDateTimeFormatPattern(this.props.intl.locale)} - ${calendarTimeUtils.getLocalizedDateTimeFormatPattern(this.props.intl.locale)}`,
297
+ onMouseEnter: () => {
298
+ // we remove the highlight so that the user can use the
299
+ // arrow keys to move the cursor when hovering
300
+ // @ts-ignore
301
+ if (isOpen) setHighlightedIndex(null);
302
+ },
303
+ onKeyDown: event => {
304
+ if (this.props.isReadOnly) {
305
+ preventDownshiftDefault(event);
306
+ return;
307
+ }
308
+ if (event.key === 'Enter' && inputValue?.trim() === '' &&
309
+ // do not clear value when user presses Enter to
310
+ // select the end date (so only clear when there is no
311
+ // startDate)
312
+ !this.state.startDate && this.props.isClearable) {
313
+ clearSelection();
314
+ this.emit([]);
315
+ }
316
+ // ArrowDown
317
+ if (event.key === 'ArrowDown') {
318
+ if (highlightedIndex + 1 >= calendarItems.length) {
319
+ // if it's the end of the month
320
+ // then bypass normal arrow navigation
321
+ preventDownshiftDefault(event);
322
+ // then jump to start of next month
323
+ this.jumpMonth(1, 0);
324
+ }
325
+ }
326
+ // ArrowUp
327
+ if (event.key === 'ArrowUp') {
328
+ const previousDay = calendarUtils.getPreviousDay(calendarItems[highlightedIndex]);
329
+ if (highlightedIndex <= 0) {
330
+ // if it's the start of the month
331
+ // then bypass normal arrow navigation
332
+ preventDownshiftDefault(event);
333
+ const numberOfDaysOfPrevMonth = calendarUtils.getDaysInMonth(previousDay);
334
+ // then jump to the last day of the previous month
335
+ this.jumpMonth(-1, numberOfDaysOfPrevMonth - 1);
336
+ }
337
+ }
338
+ },
339
+ // we only do this for readOnly because the input
340
+ // doesn't ignore these events, unlike when its disabled
341
+ onClick: this.props.isReadOnly ? undefined : openMenu
342
+ }, utils.filterDataAttributes(this.props))),
343
+ hasSelection: this.props.value.length === 2,
344
+ isClearable: this.props.isClearable,
345
+ onClear: () => {
346
+ this.setState({
347
+ startDate: null
348
+ });
349
+ this.emit([]);
350
+ clearSelection();
351
+ },
352
+ isOpen: isOpen,
353
+ isDisabled: this.props.isDisabled,
354
+ isReadOnly: this.props.isReadOnly,
355
+ isCondensed: this.props.isCondensed,
356
+ toggleButtonProps: getToggleButtonProps(),
357
+ hasError: this.props.hasError,
358
+ hasWarning: this.props.hasWarning
359
+ }), isOpen && !this.props.isDisabled && jsxRuntime.jsxs(calendarUtils.CalendarMenu, _objectSpread(_objectSpread({}, getMenuProps()), {}, {
360
+ hasError: this.props.hasError,
361
+ hasWarning: this.props.hasWarning,
362
+ children: [jsxRuntime.jsx(calendarUtils.CalendarHeader, {
363
+ monthLabel: calendarUtils.getMonthCalendarLabel(this.state.calendarDate, this.props.intl.locale),
364
+ yearLabel: calendarUtils.getYearCalendarLabel(this.state.calendarDate, this.props.intl.locale),
365
+ onPrevMonthClick: () => this.jumpMonth(-1),
366
+ onTodayClick: this.showToday,
367
+ onNextMonthClick: () => this.jumpMonth(1),
368
+ onPrevYearClick: () => this.jumpMonth(-12),
369
+ onNextYearClick: () => this.jumpMonth(12)
370
+ }), jsxRuntime.jsxs(calendarUtils.CalendarContent, {
371
+ children: [_mapInstanceProperty__default["default"](weekdays).call(weekdays, weekday => jsxRuntime.jsx(calendarUtils.CalendarDay, {
372
+ type: "heading",
373
+ children: weekday
374
+ }, weekday)), _mapInstanceProperty__default["default"](paddingDays).call(paddingDays, (_, index) => jsxRuntime.jsx(calendarUtils.CalendarDay, {
375
+ type: "spacing"
376
+ }, index)), _mapInstanceProperty__default["default"](calendarItems).call(calendarItems, (item, index) => {
377
+ const isHighlighted = this.state.suggestedItems.length + index === highlightedIndex;
378
+ const _getRange = getRange({
379
+ item,
380
+ value: this.props.value,
381
+ startDate: this.state.startDate,
382
+ highlightedItem: allItems[this.state.highlightedIndex || 0]
383
+ }),
384
+ isRangeStart = _getRange.isRangeStart,
385
+ isRangeBetween = _getRange.isRangeBetween,
386
+ isRangeEnd = _getRange.isRangeEnd;
387
+ return jsxRuntime.jsx(calendarUtils.CalendarDay, _objectSpread(_objectSpread({
388
+ isToday: calendarUtils.isSameDay(today, item)
389
+ }, getItemProps({
390
+ disabled: this.props.isDisabled,
391
+ item,
392
+ onMouseOut: () => {
393
+ // @ts-ignore
394
+ setHighlightedIndex(null);
395
+ }
396
+ })), {}, {
397
+ isHighlighted: isHighlighted,
398
+ isRangeStart: isRangeStart,
399
+ isRangeBetween: isRangeBetween,
400
+ isRangeEnd: isRangeEnd,
401
+ children: calendarUtils.getCalendarDayLabel(item)
402
+ }), item);
403
+ })]
404
+ })]
405
+ }))]
406
+ });
407
+ }
408
+ }, this.props.intl.locale)
409
+ });
410
+ }
411
+ }], [{
412
+ key: "getDerivedStateFromProps",
413
+ value: function getDerivedStateFromProps(props, state) {
414
+ // We need to update the input value string in case so that is is formatted
415
+ // according to the locale and holds the current value in case the value
416
+ // changes or when the locale changes
417
+ const shouldUpdateInputValue = !isSameRange(props.value, state.prevValue) || props.intl.locale !== state.prevLocale;
418
+ if (!shouldUpdateInputValue) return null;
419
+ return {
420
+ prevLocale: props.intl.locale,
421
+ // This is not the input value but the actual value passed to
422
+ // DateRangeInput
423
+ prevValue: props.value,
424
+ inputValue: calendarUtils.formatRange(props.value, props.intl.locale)
425
+ };
426
+ }
427
+ }]);
428
+ }(react.Component);
429
+ DateRangeInput.displayName = 'DateRangeInput';
430
+ DateRangeInput.defaultProps = {
431
+ isClearable: true
432
+ };
433
+ DateRangeInput.isEmpty = range => range.length === 0;
434
+ DateRangeInput.propTypes = {};
435
+ var dateRangeInput = reactIntl.injectIntl(DateRangeInput);
436
+
437
+ // NOTE: This string will be replaced on build time with the package version.
438
+ var version = "19.25.1";
439
+
440
+ exports["default"] = dateRangeInput;
441
+ exports.version = version;