@atlaskit/datetime-picker 14.0.2 → 14.0.4

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,92 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.parseDate = exports.isDateDisabled = exports.getPlaceholder = exports.getParsedISO = exports.formatDate = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _dateFns = require("date-fns");
10
+ var _parseTokens = require("./parse-tokens");
11
+ var _index = require("./index");
12
+ /**
13
+ * Everything in this file is to smooth out the migration of the new date picker
14
+ * (https://product-fabric.atlassian.net/browse/DSP-20682). When that ticket is
15
+ * complete, all of these functions will ilkely be merged back into the date
16
+ * picker. Please do not pre-optimize and put these back into the date picker
17
+ * unless you are working on the DTP Refresh and you have a good reason to do
18
+ * so, thank you!
19
+ *
20
+ * All variables within the `di` objects are dependency injections. They should
21
+ * be read from within the component at the end of the day. But because we are
22
+ * extracting them, we have to inject them in every place manually. When we
23
+ * re-introduce them to the components, we can likely remove the `di` variables
24
+ * and instead use internal variables.
25
+ *
26
+ * If component _only_ has injected variables, it is fully internal and was
27
+ * broken out to be it's own function.
28
+ */
29
+
30
+ var isDateDisabled = exports.isDateDisabled = function isDateDisabled(date, di) {
31
+ var disabled = di.disabled;
32
+ return disabled.indexOf(date) > -1;
33
+ };
34
+ var getParsedISO = exports.getParsedISO = function getParsedISO(di) {
35
+ var iso = di.iso;
36
+ var _iso$split = iso.split('-'),
37
+ _iso$split2 = (0, _slicedToArray2.default)(_iso$split, 3),
38
+ year = _iso$split2[0],
39
+ month = _iso$split2[1],
40
+ date = _iso$split2[2];
41
+ var newIso = iso;
42
+ var parsedDate = parseInt(date, 10);
43
+ var parsedMonth = parseInt(month, 10);
44
+ var parsedYear = parseInt(year, 10);
45
+ var lastDayInMonth = (0, _dateFns.lastDayOfMonth)(new Date(parsedYear, parsedMonth - 1) // This needs to be -1, because the Date constructor expects an index of the given month
46
+ ).getDate();
47
+ if (lastDayInMonth < parsedDate) {
48
+ newIso = "".concat(year, "-").concat((0, _index.padToTwo)(parsedMonth), "-").concat((0, _index.padToTwo)(lastDayInMonth));
49
+ } else {
50
+ newIso = "".concat(year, "-").concat((0, _index.padToTwo)(parsedMonth), "-").concat((0, _index.padToTwo)(parsedDate));
51
+ }
52
+ return newIso;
53
+ };
54
+
55
+ /**
56
+ * There are two props that can change how the date is parsed.
57
+ * The priority of props used is:
58
+ * 1. `parseInputValue`
59
+ * 2. `locale`
60
+ */
61
+ var parseDate = exports.parseDate = function parseDate(date, di) {
62
+ var parseInputValue = di.parseInputValue,
63
+ dateFormat = di.dateFormat,
64
+ l10n = di.l10n;
65
+ if (parseInputValue) {
66
+ return parseInputValue(date, dateFormat || _index.defaultDateFormat);
67
+ }
68
+ return l10n.parseDate(date);
69
+ };
70
+
71
+ /**
72
+ * There are multiple props that can change how the date is formatted.
73
+ * The priority of props used is:
74
+ * 1. `formatDisplayLabel`
75
+ * 2. `dateFormat`
76
+ * 3. `locale`
77
+ */
78
+ var formatDate = exports.formatDate = function formatDate(value, di) {
79
+ var formatDisplayLabel = di.formatDisplayLabel,
80
+ dateFormat = di.dateFormat,
81
+ l10n = di.l10n;
82
+ if (formatDisplayLabel) {
83
+ return formatDisplayLabel(value, dateFormat || _index.defaultDateFormat);
84
+ }
85
+ var date = (0, _dateFns.parseISO)(value);
86
+ return dateFormat ? (0, _dateFns.format)(date, (0, _parseTokens.convertTokens)(dateFormat)) : l10n.formatDate(date);
87
+ };
88
+ var getPlaceholder = exports.getPlaceholder = function getPlaceholder(di) {
89
+ var placeholder = di.placeholder,
90
+ l10n = di.l10n;
91
+ return placeholder || l10n.formatDate(_index.placeholderDatetime);
92
+ };
@@ -8,18 +8,18 @@ import { Component } from 'react';
8
8
 
9
9
  // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
10
10
  import { jsx } from '@emotion/react';
11
- import { format, isValid, lastDayOfMonth, parseISO } from 'date-fns';
11
+ import { isValid, parseISO } from 'date-fns';
12
12
  import { createAndFireEvent, withAnalyticsContext, withAnalyticsEvents } from '@atlaskit/analytics-next';
13
13
  import CalendarIcon from '@atlaskit/icon/glyph/calendar';
14
14
  import { createLocalizationProvider } from '@atlaskit/locale';
15
15
  import Select, { mergeStyles } from '@atlaskit/select';
16
- import { defaultDateFormat, EmptyComponent, padToTwo, placeholderDatetime } from '../internal';
16
+ import { EmptyComponent } from '../internal';
17
+ import { formatDate, getParsedISO, getPlaceholder, isDateDisabled, parseDate } from '../internal/date-picker-migration';
17
18
  import { Menu } from '../internal/menu';
18
19
  import { getSafeCalendarValue, getShortISOString } from '../internal/parse-date';
19
- import { convertTokens } from '../internal/parse-tokens';
20
20
  import { makeSingleValue } from '../internal/single-value';
21
21
  const packageName = "@atlaskit/datetime-picker";
22
- const packageVersion = "14.0.2";
22
+ const packageVersion = "14.0.4";
23
23
  const datePickerDefaultProps = {
24
24
  appearance: 'default',
25
25
  autoFocus: false,
@@ -62,26 +62,13 @@ class DatePickerComponent extends Component {
62
62
  var _this$props$isOpen;
63
63
  return (_this$props$isOpen = this.props.isOpen) !== null && _this$props$isOpen !== void 0 ? _this$props$isOpen : this.state.isOpen;
64
64
  });
65
- _defineProperty(this, "isDateDisabled", date => {
66
- return this.props.disabled.indexOf(date) > -1;
67
- });
68
65
  _defineProperty(this, "onCalendarChange", ({
69
66
  iso
70
67
  }) => {
71
- const [year, month, date] = iso.split('-');
72
- let newIso = iso;
73
- const parsedDate = parseInt(date, 10);
74
- const parsedMonth = parseInt(month, 10);
75
- const parsedYear = parseInt(year, 10);
76
- const lastDayInMonth = lastDayOfMonth(new Date(parsedYear, parsedMonth - 1) // This needs to be -1, because the Date constructor expects an index of the given month
77
- ).getDate();
78
- if (lastDayInMonth < parsedDate) {
79
- newIso = `${year}-${padToTwo(parsedMonth)}-${padToTwo(lastDayInMonth)}`;
80
- } else {
81
- newIso = `${year}-${padToTwo(parsedMonth)}-${padToTwo(parsedDate)}`;
82
- }
83
68
  this.setState({
84
- calendarValue: newIso
69
+ calendarValue: getParsedISO({
70
+ iso
71
+ })
85
72
  });
86
73
  });
87
74
  _defineProperty(this, "onCalendarSelect", ({
@@ -169,7 +156,11 @@ class DatePickerComponent extends Component {
169
156
  _defineProperty(this, "onTextInput", event => {
170
157
  const inputValue = event.target.value;
171
158
  if (inputValue) {
172
- const parsed = this.parseDate(inputValue);
159
+ const parsed = parseDate(inputValue, {
160
+ parseInputValue: this.props.parseInputValue,
161
+ dateFormat: this.props.dateFormat,
162
+ l10n: this.state.l10n
163
+ });
173
164
  // Only try to set the date if we have month & day
174
165
  if (parsed && isValid(parsed)) {
175
166
  // We format the parsed date to YYYY-MM-DD here because
@@ -233,7 +224,9 @@ class DatePickerComponent extends Component {
233
224
  // using enter. See https://product-fabric.atlassian.net/browse/DSP-2501
234
225
  // for more details.
235
226
  event.preventDefault();
236
- if (!this.isDateDisabled(calendarValue)) {
227
+ if (!isDateDisabled(calendarValue, {
228
+ disabled: this.props.disabled
229
+ })) {
237
230
  // Get a safe `calendarValue` in case the value exceeds the maximum
238
231
  // allowed by ISO 8601
239
232
  const safeCalendarValue = getSafeCalendarValue(calendarValue);
@@ -303,58 +296,6 @@ class DatePickerComponent extends Component {
303
296
  this.forceUpdate();
304
297
  }
305
298
  });
306
- /**
307
- * There are two props that can change how the date is parsed.
308
- * The priority of props used is:
309
- * 1. `parseInputValue`
310
- * 2. `locale`
311
- */
312
- _defineProperty(this, "parseDate", date => {
313
- const {
314
- parseInputValue,
315
- dateFormat
316
- } = this.props;
317
- if (parseInputValue) {
318
- return parseInputValue(date, dateFormat || defaultDateFormat);
319
- }
320
- const {
321
- l10n
322
- } = this.state;
323
- return l10n.parseDate(date);
324
- });
325
- /**
326
- * There are multiple props that can change how the date is formatted.
327
- * The priority of props used is:
328
- * 1. `formatDisplayLabel`
329
- * 2. `dateFormat`
330
- * 3. `locale`
331
- */
332
- _defineProperty(this, "formatDate", value => {
333
- const {
334
- formatDisplayLabel,
335
- dateFormat
336
- } = this.props;
337
- const {
338
- l10n
339
- } = this.state;
340
- if (formatDisplayLabel) {
341
- return formatDisplayLabel(value, dateFormat || defaultDateFormat);
342
- }
343
- const date = parseISO(value);
344
- return dateFormat ? format(date, convertTokens(dateFormat)) : l10n.formatDate(date);
345
- });
346
- _defineProperty(this, "getPlaceholder", () => {
347
- const {
348
- placeholder
349
- } = this.props;
350
- if (placeholder) {
351
- return placeholder;
352
- }
353
- const {
354
- l10n
355
- } = this.state;
356
- return l10n.formatDate(placeholderDatetime);
357
- });
358
299
  this.state = {
359
300
  isOpen: this.props.defaultIsOpen,
360
301
  isFocused: false,
@@ -457,7 +398,11 @@ class DatePickerComponent extends Component {
457
398
  })
458
399
  });
459
400
  const initialValue = value ? {
460
- label: this.formatDate(value),
401
+ label: formatDate(value, {
402
+ formatDisplayLabel: this.props.formatDisplayLabel,
403
+ dateFormat: this.props.dateFormat,
404
+ l10n: this.state.l10n
405
+ }),
461
406
  value
462
407
  } : null;
463
408
  return (
@@ -493,7 +438,10 @@ class DatePickerComponent extends Component {
493
438
  onChange: this.onSelectChange,
494
439
  onFocus: this.onSelectFocus,
495
440
  onInputChange: this.handleSelectInputChange,
496
- placeholder: this.getPlaceholder(),
441
+ placeholder: getPlaceholder({
442
+ placeholder: this.props.placeholder,
443
+ l10n: this.state.l10n
444
+ }),
497
445
  styles: mergedStyles,
498
446
  value: initialValue
499
447
  }, selectProps, {
@@ -18,7 +18,7 @@ import { convertTokens } from '../internal/parse-tokens';
18
18
  import DatePicker from './date-picker';
19
19
  import TimePicker from './time-picker';
20
20
  const packageName = "@atlaskit/datetime-picker";
21
- const packageVersion = "14.0.2";
21
+ const packageVersion = "14.0.4";
22
22
  // Make DatePicker 50% the width of DateTimePicker
23
23
  // If rendering an icon container, shrink the TimePicker
24
24
  const datePickerContainerStyles = css({