carbon-react 103.2.0 → 104.0.0

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.
Files changed (58) hide show
  1. package/lib/components/date/__internal__/date-formats/index.d.ts +8 -0
  2. package/lib/components/date/__internal__/date-formats/index.js +47 -0
  3. package/lib/components/date/__internal__/date-picker/date-picker.component.d.ts +3 -0
  4. package/lib/components/date/__internal__/date-picker/date-picker.component.js +154 -0
  5. package/lib/components/date/__internal__/date-picker/date-picker.d.ts +25 -0
  6. package/lib/components/date/{day-picker.style.d.ts → __internal__/date-picker/day-picker.style.d.ts} +0 -0
  7. package/lib/components/date/{day-picker.style.js → __internal__/date-picker/day-picker.style.js} +5 -1
  8. package/lib/components/date/__internal__/date-picker/index.d.ts +1 -0
  9. package/lib/{__internal__/date → components/date/__internal__/date-picker}/index.js +2 -2
  10. package/lib/components/date/{navbar → __internal__/navbar}/button.style.d.ts +0 -0
  11. package/lib/components/date/{navbar → __internal__/navbar}/button.style.js +1 -1
  12. package/lib/components/date/{navbar → __internal__/navbar}/index.d.ts +0 -0
  13. package/lib/components/date/{navbar → __internal__/navbar}/index.js +0 -0
  14. package/lib/components/date/{navbar → __internal__/navbar}/navbar.component.d.ts +0 -0
  15. package/lib/components/date/{navbar → __internal__/navbar}/navbar.component.js +10 -12
  16. package/lib/components/date/__internal__/navbar/navbar.d.ts +8 -0
  17. package/lib/components/date/{navbar → __internal__/navbar}/navbar.style.d.ts +0 -0
  18. package/lib/components/date/{navbar → __internal__/navbar}/navbar.style.js +1 -1
  19. package/lib/components/date/__internal__/utils.d.ts +19 -0
  20. package/lib/components/date/__internal__/utils.js +182 -0
  21. package/lib/components/date/{weekday → __internal__/weekday}/abbr.style.d.ts +0 -0
  22. package/lib/components/date/{weekday → __internal__/weekday}/abbr.style.js +0 -0
  23. package/lib/components/date/{weekday → __internal__/weekday}/index.d.ts +0 -0
  24. package/lib/components/date/{weekday → __internal__/weekday}/index.js +0 -0
  25. package/lib/components/date/{weekday → __internal__/weekday}/weekday.component.d.ts +0 -0
  26. package/lib/components/date/{weekday → __internal__/weekday}/weekday.component.js +0 -0
  27. package/lib/components/date/__internal__/weekday/weekday.d.ts +13 -0
  28. package/lib/components/date/{weekday → __internal__/weekday}/weekday.style.d.ts +0 -0
  29. package/lib/components/date/{weekday → __internal__/weekday}/weekday.style.js +1 -1
  30. package/lib/components/date/date.component.d.ts +27 -55
  31. package/lib/components/date/date.component.js +291 -661
  32. package/lib/components/date/date.d.ts +28 -20
  33. package/lib/components/date/date.style.js +3 -2
  34. package/lib/components/date/index.js +2 -10
  35. package/lib/components/date-range/date-range-context.d.ts +17 -0
  36. package/lib/components/date-range/date-range.component.d.ts +3 -5
  37. package/lib/components/date-range/date-range.component.js +123 -119
  38. package/lib/components/date-range/date-range.context.d.ts +3 -0
  39. package/lib/components/date-range/date-range.context.js +14 -0
  40. package/lib/components/date-range/date-range.d.ts +5 -7
  41. package/lib/components/date-range/date-range.style.js +1 -0
  42. package/lib/components/date-range/index.d.ts +1 -0
  43. package/lib/components/i18n-provider/i18n-provider.component.d.ts +1 -4
  44. package/lib/components/i18n-provider/i18n-provider.component.js +1 -4
  45. package/lib/locales/date-fns-locales/index.d.ts +1 -0
  46. package/lib/locales/date-fns-locales/index.js +18 -0
  47. package/lib/locales/en-gb.js +3 -4
  48. package/lib/locales/locale.d.ts +3 -4
  49. package/lib/locales/pl-pl.d.ts +1 -1
  50. package/lib/locales/pl-pl.js +5 -0
  51. package/package.json +3 -3
  52. package/lib/__internal__/date/date.d.ts +0 -34
  53. package/lib/__internal__/date/date.js +0 -216
  54. package/lib/__internal__/date/index.d.ts +0 -1
  55. package/lib/__spec_helper__/mock-date-helper.d.ts +0 -1
  56. package/lib/__spec_helper__/mock-date-helper.js +0 -37
  57. package/lib/components/date/date-picker.component.d.ts +0 -22
  58. package/lib/components/date/date-picker.component.js +0 -218
@@ -3,703 +3,349 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = exports.BaseDateInput = exports.defaultDateFormat = void 0;
6
+ exports.default = void 0;
7
7
 
8
- var _react = _interopRequireDefault(require("react"));
8
+ var _react = _interopRequireWildcard(require("react"));
9
9
 
10
10
  var _propTypes = _interopRequireDefault(require("prop-types"));
11
11
 
12
- var _invariant = _interopRequireDefault(require("invariant"));
13
-
14
12
  var _propTypes2 = _interopRequireDefault(require("@styled-system/prop-types"));
15
13
 
16
- var _utils = require("../../style/utils");
14
+ var _utils = require("./__internal__/utils");
15
+
16
+ var _useLocale = _interopRequireDefault(require("../../hooks/__internal__/useLocale"));
17
17
 
18
18
  var _events = _interopRequireDefault(require("../../__internal__/utils/helpers/events"));
19
19
 
20
- var _date = _interopRequireDefault(require("../../__internal__/date"));
20
+ var _utils2 = require("../../style/utils");
21
21
 
22
- var _datePicker = _interopRequireDefault(require("./date-picker.component"));
22
+ var _dateFormats = _interopRequireDefault(require("./__internal__/date-formats"));
23
23
 
24
- var _date2 = _interopRequireDefault(require("./date.style"));
24
+ var _date = _interopRequireDefault(require("./date.style"));
25
25
 
26
26
  var _textbox = _interopRequireDefault(require("../textbox"));
27
27
 
28
- var _withUniqueIdProps = _interopRequireDefault(require("../../__internal__/utils/helpers/with-unique-id-props"));
29
-
30
- var _browserTypeCheck = require("../../__internal__/utils/helpers/browser-type-check");
28
+ var _datePicker = _interopRequireDefault(require("./__internal__/date-picker"));
31
29
 
32
- var _i18nContext = _interopRequireDefault(require("../../__internal__/i18n-context"));
30
+ var _dateRange = _interopRequireDefault(require("../date-range/date-range.context"));
33
31
 
34
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
33
 
36
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
37
-
38
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
39
-
40
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
41
-
42
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
43
-
44
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
45
-
46
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
47
-
48
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
49
-
50
- function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
51
-
52
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
53
-
54
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
55
-
56
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
57
-
58
- 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; }
59
-
60
- const defaultDateFormat = "DD/MM/YYYY";
61
- exports.defaultDateFormat = defaultDateFormat;
62
- const hiddenDateFormat = "YYYY-MM-DD";
63
- const marginPropTypes = (0, _utils.filterStyledSystemMarginProps)(_propTypes2.default.space);
64
-
65
- let BaseDateInput = /*#__PURE__*/function (_React$Component) {
66
- _inherits(BaseDateInput, _React$Component);
67
-
68
- var _super = _createSuper(BaseDateInput);
69
-
70
- function BaseDateInput(...args) {
71
- var _this;
72
-
73
- _classCallCheck(this, BaseDateInput);
34
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
74
35
 
75
- _this = _super.call(this, ...args);
76
-
77
- _defineProperty(_assertThisInitialized(_this), "localeData", {
78
- locale: _this.context.locale(),
79
- formats: _this.context.date.formats.inputs(),
80
- format: _this.context.date.formats.javascript()
81
- });
82
-
83
- _defineProperty(_assertThisInitialized(_this), "isBlurBlocked", false);
84
-
85
- _defineProperty(_assertThisInitialized(_this), "isOpening", false);
86
-
87
- _defineProperty(_assertThisInitialized(_this), "inputHasFocus", _this.props.autoFocus || false);
88
-
89
- _defineProperty(_assertThisInitialized(_this), "isControlled", _this.props.value !== undefined);
90
-
91
- _defineProperty(_assertThisInitialized(_this), "initialVisibleValue", generateAdjustedValue(_this.props, _this.localeData));
92
-
93
- _defineProperty(_assertThisInitialized(_this), "inputFocusedViaPicker", false);
94
-
95
- _defineProperty(_assertThisInitialized(_this), "hasMounted", false);
96
-
97
- _defineProperty(_assertThisInitialized(_this), "inputPresentationRef", /*#__PURE__*/_react.default.createRef());
98
-
99
- _defineProperty(_assertThisInitialized(_this), "state", {
100
- isDatePickerOpen: false,
101
-
102
- /** Date object to pass to the DatePicker */
103
- selectedDate: _date.default.stringToDate(_date.default.formatValue({
104
- value: _this.initialVisibleValue,
105
- ..._this.localeData
106
- })),
107
-
108
- /** Displayed value, format dependent on a region */
109
- visibleValue: _this.initialVisibleValue,
110
-
111
- /** Stores last valid values to be emitted onBlur if current input is invalid */
112
- lastValidEventValues: {
113
- formattedValue: _this.initialVisibleValue,
114
- rawValue: _date.default.formatValue({
115
- value: _this.initialVisibleValue,
116
- ..._this.localeData
117
- })
118
- },
119
- shouldPickerOpen: false
120
- });
121
-
122
- _defineProperty(_assertThisInitialized(_this), "hasValueChanged", prevProps => {
123
- if (_this.props.allowEmptyValue) {
124
- return prevProps.value !== _this.props.value;
125
- }
126
-
127
- return _this.props.value && prevProps.value !== _this.props.value;
128
- });
129
-
130
- _defineProperty(_assertThisInitialized(_this), "assignInput", input => {
131
- _this.input = input.current;
132
- _this.inputPresentationRef.current = input.current.parentElement;
133
- });
134
-
135
- _defineProperty(_assertThisInitialized(_this), "shouldAllowBlur", () => {
136
- return (0, _browserTypeCheck.isEdge)(navigator) && !_this.inputFocusedViaPicker;
137
- });
138
-
139
- _defineProperty(_assertThisInitialized(_this), "shouldBlockBlur", () => {
140
- const {
141
- disabled,
142
- readOnly
143
- } = _this.props;
144
- const block = _this.isBlurBlocked || _this.inputFocusedViaPicker || _this.input === document.activeElement;
36
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
145
37
 
146
- if (disabled || readOnly || block) {
147
- _this.inputFocusedViaPicker = _this.input === document.activeElement; // needed to block blur properly in Edge
148
-
149
- if (!_this.shouldAllowBlur()) {
150
- return true;
151
- }
152
- }
153
-
154
- return false;
155
- });
156
-
157
- _defineProperty(_assertThisInitialized(_this), "handleBlur", () => {
158
- // needed to make blur work properly in Edge
159
- // https://stackoverflow.com/a/24695316/10894881
160
- setTimeout(() => {
161
- _this.inputHasFocus = false;
162
-
163
- if (_this.shouldBlockBlur()) {
164
- return;
165
- }
166
-
167
- _this.reformatVisibleDate();
168
-
169
- if (_this.props.onBlur && !_this.state.isDatePickerOpen) {
170
- const event = _this.buildCustomEvent({
171
- target: _this.input,
172
- type: "blur"
173
- }, _date.default.formatValue({
174
- value: _this.state.visibleValue,
175
- ..._this.localeData
176
- }));
177
-
178
- _this.props.onBlur(event);
179
- }
180
- }, 0);
181
- });
182
-
183
- _defineProperty(_assertThisInitialized(_this), "handleFocus", ev => {
184
- _this.inputHasFocus = true;
185
- const {
186
- disabled,
187
- readOnly
188
- } = _this.props;
189
- if (disabled || readOnly) return;
190
-
191
- if (_this.isAutoFocused) {
192
- _this.isAutoFocused = false;
193
- } else {
194
- _this.openDatePicker();
195
- }
38
+ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
196
39
 
197
- if (_this.props.onFocus) {
198
- _this.props.onFocus(ev);
40
+ const marginPropTypes = (0, _utils2.filterStyledSystemMarginProps)(_propTypes2.default.space);
41
+
42
+ const DateInput = ({
43
+ adaptiveLabelBreakpoint,
44
+ allowEmptyValue,
45
+ autoFocus,
46
+ "data-component": dataComponent,
47
+ "data-element": dataElement,
48
+ "data-role": dataRole,
49
+ disabled,
50
+ disablePortal = false,
51
+ helpAriaLabel,
52
+ labelInline,
53
+ minDate,
54
+ maxDate,
55
+ onBlur,
56
+ onChange,
57
+ onClick,
58
+ onFocus,
59
+ onKeyDown,
60
+ pickerProps = {},
61
+ readOnly,
62
+ size = "medium",
63
+ tooltipPosition,
64
+ value,
65
+ ...rest
66
+ }) => {
67
+ const wrapperRef = (0, _react.useRef)();
68
+ const parentRef = (0, _react.useRef)();
69
+ const inputRef = (0, _react.useRef)();
70
+ const pickerRef = (0, _react.useRef)();
71
+ const alreadyFocused = (0, _react.useRef)(false);
72
+ const isBlurBlocked = (0, _react.useRef)(false);
73
+ const focusedViaPicker = (0, _react.useRef)(false);
74
+ const l = (0, _useLocale.default)();
75
+ const {
76
+ dateFnsLocale
77
+ } = l.date;
78
+ const {
79
+ format,
80
+ formats
81
+ } = (0, _react.useMemo)(() => (0, _dateFormats.default)(dateFnsLocale()), [dateFnsLocale]);
82
+ const {
83
+ inputRefMap,
84
+ setInputRefMap
85
+ } = (0, _react.useContext)(_dateRange.default);
86
+ const inputName = dataElement === null || dataElement === void 0 ? void 0 : dataElement.split("-")[0];
87
+ const [open, setOpen] = (0, _react.useState)(false);
88
+ const [selectedDays, setSelectedDays] = (0, _react.useState)((0, _utils.checkISOFormatAndLength)(value) ? (0, _utils.parseISODate)(value) : (0, _utils.parseDate)(format, value));
89
+ const isInitialValue = (0, _react.useRef)(true);
90
+
91
+ const computeInvalidRawValue = inputValue => allowEmptyValue && !inputValue.length ? inputValue : null;
92
+
93
+ const buildCustomEvent = ev => {
94
+ const {
95
+ id,
96
+ name
97
+ } = ev.target;
98
+ const [matchedFormat, matchedValue] = (0, _utils.findMatchedFormatAndValue)(ev.target.value, formats);
99
+ const formattedValueString = ev.type === "blur" ? (0, _utils.formattedValue)(format, selectedDays) : ev.target.value;
100
+ const rawValue = (0, _utils.isDateValid)((0, _utils.parseDate)(matchedFormat, matchedValue)) ? (0, _utils.formatToISO)(matchedFormat, matchedValue) : computeInvalidRawValue(ev.target.value);
101
+ ev.target = { ...(name && {
102
+ name
103
+ }),
104
+ ...(id && {
105
+ id
106
+ }),
107
+ value: {
108
+ formattedValue: formattedValueString,
109
+ rawValue
199
110
  }
200
- });
201
-
202
- _defineProperty(_assertThisInitialized(_this), "handleKeyDown", ev => {
203
- if (_this.props.onKeyDown) _this.props.onKeyDown(ev);
204
-
205
- if (_events.default.isTabKey(ev)) {
206
- _this.isOpening = false;
207
- _this.inputFocusedViaPicker = false;
208
-
209
- _this.closeDatePicker();
111
+ };
112
+ return ev;
113
+ };
114
+
115
+ const handleChange = ev => {
116
+ isInitialValue.current = false;
117
+ onChange(buildCustomEvent(ev));
118
+ };
119
+
120
+ const focusInput = () => {
121
+ var _inputRef$current;
122
+
123
+ focusedViaPicker.current = true;
124
+ (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.focus();
125
+ };
126
+
127
+ const handleDayClick = (day, ev) => {
128
+ setSelectedDays(day);
129
+ onChange(buildCustomEvent({ ...ev,
130
+ target: { ...ev.target,
131
+ value: (0, _utils.formattedValue)(format, day)
210
132
  }
211
- });
212
-
213
- _defineProperty(_assertThisInitialized(_this), "openDatePicker", openedOnMount => {
214
- document.addEventListener("click", _this.closeDatePicker);
133
+ }));
134
+ focusInput();
135
+ setOpen(false);
136
+ };
137
+
138
+ const handleBlur = ev => {
139
+ if (disabled || readOnly) {
140
+ return;
141
+ }
215
142
 
216
- if (!openedOnMount && _this.inputFocusedViaPicker) {
217
- _this.isBlurBlocked = true;
218
- return;
219
- }
143
+ let event;
220
144
 
221
- _this.updateSelectedDate(_this.props.value || _date.default.formatValue({
222
- value: _this.state.visibleValue,
223
- ..._this.localeData
224
- }));
145
+ if ((0, _utils.isDateValid)(selectedDays)) {
146
+ event = buildCustomEvent(ev);
225
147
 
226
- _this.setState({
227
- isDatePickerOpen: true
228
- });
229
- });
230
-
231
- _defineProperty(_assertThisInitialized(_this), "updateValidEventValues", value => {
232
- if (_this.hasMounted) {
233
- _this.setState({
234
- visibleValue: _date.default.formatDateToCurrentLocale({
235
- value,
236
- ..._this.localeData
237
- }),
238
- lastValidEventValues: {
239
- formattedValue: _date.default.formatDateToCurrentLocale({
240
- value,
241
- ..._this.localeData
242
- }),
243
- rawValue: _date.default.formatValue({
244
- value,
245
- ..._this.localeData
246
- })
247
- }
248
- });
148
+ if ((0, _utils.formattedValue)(format, selectedDays) !== value) {
149
+ onChange(event);
249
150
  }
250
- });
251
-
252
- _defineProperty(_assertThisInitialized(_this), "reformatVisibleDate", () => {
151
+ } else {
253
152
  const {
254
- lastValidEventValues,
255
- visibleValue
256
- } = _this.state;
257
-
258
- if (_date.default.isValidDate({
259
- value: visibleValue,
260
- ..._this.localeData
261
- }) || _this.canBeEmptyValues(visibleValue)) {
262
- _this.updateValidEventValues(visibleValue);
263
- } else if (!visibleValue.length) {
264
- _this.updateValidEventValues(lastValidEventValues.formattedValue);
265
- }
266
- });
267
-
268
- _defineProperty(_assertThisInitialized(_this), "closeDatePicker", () => {
269
- if (_this.isOpening) {
270
- _this.isOpening = false;
271
- return;
272
- }
273
-
274
- document.removeEventListener("click", _this.closeDatePicker);
275
-
276
- _this.setState({
277
- isDatePickerOpen: false,
278
- shouldPickerOpen: false
279
- }, () => {
280
- _this.isBlurBlocked = false;
281
-
282
- if (_this.input !== document.activeElement) {
283
- _this.handleBlur();
284
- }
285
- });
286
- });
287
-
288
- _defineProperty(_assertThisInitialized(_this), "handleClick", event => {
289
- if (_this.props.disabled || _this.props.readOnly) return;
290
- _this.isOpening = true;
291
-
292
- _this.setState(({
293
- shouldPickerOpen
294
- }) => ({
295
- shouldPickerOpen: !shouldPickerOpen
296
- }), () => {
297
- if (_this.state.shouldPickerOpen) {
298
- _this.isBlurBlocked = true;
299
- _this.inputFocusedViaPicker = false;
300
-
301
- _this.openDatePicker();
302
- } else {
303
- _this.inputFocusedViaPicker = false;
304
- _this.isOpening = false;
305
-
306
- _this.closeDatePicker();
153
+ id,
154
+ name
155
+ } = ev.target;
156
+ ev.target = { ...(name && {
157
+ name
158
+ }),
159
+ ...(id && {
160
+ id
161
+ }),
162
+ value: {
163
+ formattedValue: ev.target.value,
164
+ rawValue: computeInvalidRawValue(ev.target.value)
307
165
  }
308
- });
309
-
310
- if (_this.props.onClick) {
311
- _this.props.onClick(event);
312
- }
313
- });
314
-
315
- _defineProperty(_assertThisInitialized(_this), "handleDateSelect", selectedDate => {
316
- const stringDateIso = _date.default.formatDateString(selectedDate);
317
-
318
- _this.isBlurBlocked = true;
319
- _this.isOpening = false;
320
-
321
- _this.updateVisibleValue(stringDateIso);
322
- });
323
-
324
- _defineProperty(_assertThisInitialized(_this), "updateVisibleValue", date => {
325
- const visibleValue = _date.default.formatDateToCurrentLocale({
326
- value: date,
327
- ..._this.localeData
328
- });
329
-
330
- const newDate = _this.getDateObject(date);
331
-
332
- _this.setState({
333
- selectedDate: newDate
334
- }, () => {
335
- _this.updateValidEventValues(visibleValue);
336
-
337
- const event = {
338
- target: _this.input
339
- };
340
- event.target.value = visibleValue;
341
-
342
- _this.emitOnChangeCallback(event, date);
343
-
344
- _this.focusInput();
345
- });
346
- });
347
-
348
- _defineProperty(_assertThisInitialized(_this), "focusInput", () => {
349
- _this.inputFocusedViaPicker = true;
350
- _this.isOpening = false;
166
+ };
167
+ event = ev;
168
+ }
351
169
 
352
- _this.closeDatePicker();
170
+ if (isBlurBlocked.current) {
171
+ return;
172
+ }
353
173
 
354
- _this.input.focus();
174
+ if (onBlur) {
175
+ onBlur(event);
176
+ }
177
+ };
355
178
 
356
- _this.isBlurBlocked = false;
357
- });
179
+ const handleFocus = ev => {
180
+ if (disabled || readOnly) {
181
+ return;
182
+ }
358
183
 
359
- _defineProperty(_assertThisInitialized(_this), "handleVisibleInputChange", ev => {
360
- const {
361
- disabled,
362
- readOnly
363
- } = _this.props;
364
- const value = ev.target.value.formattedValue || ev.target.value;
365
-
366
- const isValidDate = _date.default.isValidDate({
367
- value,
368
- ..._this.localeData
369
- });
184
+ isBlurBlocked.current = false;
370
185
 
371
- let isoDateString;
372
- if (disabled || readOnly) return;
373
- _this.isBlurBlocked = false;
186
+ if (!open && !alreadyFocused.current) {
187
+ setOpen(true);
188
+ } else {
189
+ alreadyFocused.current = false;
190
+ }
374
191
 
375
- if (isValidDate || _this.canBeEmptyValues(value)) {
376
- isoDateString = _date.default.formatValue({
377
- value,
378
- ..._this.localeData
379
- });
192
+ if (onFocus) {
193
+ onFocus(ev);
194
+ }
195
+ };
380
196
 
381
- _this.updateSelectedDate(isoDateString);
197
+ const handleKeyDown = ev => {
198
+ if (onKeyDown) {
199
+ onKeyDown(ev);
200
+ }
382
201
 
383
- _this.emitOnChangeCallback(ev, isoDateString);
384
- }
202
+ if (_events.default.isTabKey(ev)) {
203
+ setOpen(false);
204
+ alreadyFocused.current = false;
205
+ }
206
+ };
385
207
 
386
- _this.setState({
387
- visibleValue: value
388
- });
389
- });
390
-
391
- _defineProperty(_assertThisInitialized(_this), "updateSelectedDate", newValue => {
392
- let selectedDate;
393
- let visibleValue;
394
-
395
- if (!newValue.length) {
396
- selectedDate = newValue;
397
- visibleValue = newValue;
398
- } else {
399
- selectedDate = _this.getDateObject(newValue);
400
- visibleValue = _date.default.formatDateToCurrentLocale({
401
- value: newValue,
402
- ..._this.localeData
403
- });
404
- }
208
+ const handleClick = ev => {
209
+ if (disabled || readOnly) {
210
+ return;
211
+ }
405
212
 
406
- _this.setState({
407
- selectedDate,
408
- visibleValue
409
- });
410
- });
411
-
412
- _defineProperty(_assertThisInitialized(_this), "getDateObject", newValue => {
413
- if (!_date.default.isValidDate({
414
- value: newValue,
415
- ..._this.localeData
416
- })) {
417
- return _date.default.stringToDate(_date.default.todayFormatted());
418
- }
213
+ if (onClick) {
214
+ onClick(ev);
215
+ }
216
+ };
419
217
 
420
- return _date.default.stringToDate(_date.default.formatValue({
421
- value: newValue,
422
- ..._this.localeData
423
- }));
424
- });
218
+ const handleMouseDown = ev => {
219
+ if (disabled || readOnly) {
220
+ return;
221
+ }
425
222
 
426
- _defineProperty(_assertThisInitialized(_this), "emitOnChangeCallback", (ev, isoFormattedValue) => {
427
- if (_this.props.onChange) {
428
- const event = _this.buildCustomEvent(ev, isoFormattedValue);
223
+ isBlurBlocked.current = true;
429
224
 
430
- _this.props.onChange(event);
431
- }
432
- });
225
+ if (ev.target.type === "text" && !open) {
226
+ setOpen(true);
227
+ } else if (ev.target.type !== "text") {
228
+ alreadyFocused.current = true;
229
+ setOpen(prev => !prev);
230
+ }
231
+ };
433
232
 
434
- _defineProperty(_assertThisInitialized(_this), "canBeEmptyValues", value => {
435
- return _this.props.allowEmptyValue && !value.length;
436
- });
233
+ const handlePickerMouseDown = () => {
234
+ isBlurBlocked.current = true;
235
+ };
437
236
 
438
- _defineProperty(_assertThisInitialized(_this), "buildCustomEvent", (ev, isoFormattedValue) => {
439
- const {
440
- id,
441
- name,
442
- value
443
- } = ev.target;
444
- const {
445
- lastValidEventValues
446
- } = _this.state;
237
+ const assignInput = input => {
238
+ var _inputRefMap$inputNam;
447
239
 
448
- const validRawValue = _date.default.isValidDate({
449
- value: isoFormattedValue,
450
- ..._this.localeData
451
- });
240
+ inputRef.current = input.current;
241
+ parentRef.current = input.current.parentElement;
452
242
 
453
- const validValues = _this.canBeEmptyValues(value) ? {
454
- formattedValue: "",
455
- rawValue: ""
456
- } : lastValidEventValues;
457
- ev.target = { ...(name && {
458
- name
459
- }),
460
- ...(id && {
461
- id
462
- }),
463
- value: { ...validValues,
464
- ...(validRawValue && {
465
- formattedValue: _date.default.formatDateToCurrentLocale({
466
- value,
467
- ..._this.localeData
468
- })
469
- }),
470
- ...(validRawValue && {
471
- rawValue: isoFormattedValue
472
- }),
473
- ...(!validRawValue && {
474
- formattedValue: value,
475
- rawValue: value
476
- })
243
+ if (inputRefMap && ((_inputRefMap$inputNam = inputRefMap[inputName]) === null || _inputRefMap$inputNam === void 0 ? void 0 : _inputRefMap$inputNam.setOpen) !== setOpen) {
244
+ setInputRefMap({
245
+ [inputName]: {
246
+ isBlurBlocked,
247
+ setOpen
477
248
  }
478
- };
479
- return ev;
480
- });
481
-
482
- _defineProperty(_assertThisInitialized(_this), "renderDatePicker", dateRangeProps => {
483
- if (!_this.state.isDatePickerOpen) return null;
484
- const {
485
- visibleValue
486
- } = _this.state;
487
- let isoValue = "";
488
-
489
- if (_date.default.isValidDate({
490
- value: visibleValue,
491
- ..._this.localeData
492
- })) {
493
- isoValue = _date.default.formatValue({
494
- value: visibleValue,
495
- ..._this.localeData
496
- });
497
- }
498
-
499
- return /*#__PURE__*/_react.default.createElement("div", {
500
- onClick: _this.markCurrentDatepicker,
501
- role: "presentation"
502
- }, /*#__PURE__*/_react.default.createElement(_datePicker.default, _extends({
503
- inputElement: _this.inputPresentationRef,
504
- selectedDate: _this.state.selectedDate,
505
- handleDateSelect: _this.handleDateSelect,
506
- inputDate: isoValue,
507
- disablePortal: _this.props.disablePortal
508
- }, dateRangeProps)));
509
- });
510
-
511
- _defineProperty(_assertThisInitialized(_this), "markCurrentDatepicker", () => {
512
- if (_this.props.disabled || _this.props.readOnly) return;
513
- _this.isOpening = true;
514
- _this.inputFocusedViaPicker = false;
515
- _this.isBlurBlocked = true;
516
-
517
- _this.openDatePicker();
518
- });
519
-
520
- _defineProperty(_assertThisInitialized(_this), "hiddenValue", () => {
521
- if (_date.default.isValidDate({
522
- value: _this.state.visibleValue,
523
- ..._this.localeData
524
- })) {
525
- return _date.default.formatValue({
526
- value: _this.state.visibleValue,
527
- formatTo: hiddenDateFormat,
528
- ..._this.localeData
529
- });
530
- }
531
-
532
- return _this.state.lastValidEventValues.rawValue;
533
- });
534
-
535
- _defineProperty(_assertThisInitialized(_this), "renderHiddenInput", () => {
536
- const props = {
537
- name: _this.props.name,
538
- type: "hidden",
539
- "data-element": "hidden-input",
540
- value: _this.hiddenValue()
541
- };
542
- return /*#__PURE__*/_react.default.createElement("input", props);
543
- });
544
-
545
- return _this;
546
- }
547
-
548
- _createClass(BaseDateInput, [{
549
- key: "componentDidMount",
550
- value: function componentDidMount() {
551
- this.hasMounted = true;
552
-
553
- if (this.props.autoFocus) {
554
- this.isAutoFocused = true;
555
- this.openDatePicker(true);
556
- this.setState({
557
- shouldPickerOpen: true
558
- });
559
- }
249
+ });
560
250
  }
561
- }, {
562
- key: "componentDidUpdate",
563
- value: function componentDidUpdate(prevProps) {
564
- const message = "Input elements should not switch from uncontrolled to controlled (or vice versa). " + "Decide between using a controlled or uncontrolled input element for the lifetime of the component";
565
- (0, _invariant.default)(this.isControlled === (this.props.value !== undefined), message);
566
-
567
- if (this.isControlled && !this.inputHasFocus && this.hasValueChanged(prevProps)) {
568
- this.updateSelectedDate(this.props.value);
251
+ };
252
+
253
+ (0, _react.useEffect)(() => {
254
+ const fnClosePicker = ev => {
255
+ if (open && !_events.default.composedPath(ev).includes(parentRef.current) && !_events.default.composedPath(ev).includes(pickerRef.current)) {
256
+ alreadyFocused.current = true;
257
+ inputRef.current.focus();
258
+ isBlurBlocked.current = false;
259
+ inputRef.current.blur();
260
+ setOpen(false);
261
+ alreadyFocused.current = false;
569
262
  }
263
+ };
264
+
265
+ document.addEventListener("mousedown", fnClosePicker);
266
+ return function cleanup() {
267
+ document.removeEventListener("mousedown", fnClosePicker);
268
+ };
269
+ }, [open]);
270
+ (0, _react.useEffect)(() => {
271
+ const [matchedFormat, matchedValue] = (0, _utils.findMatchedFormatAndValue)(value, formats);
272
+
273
+ if (matchedFormat && matchedValue && (0, _utils.isDateValid)((0, _utils.parseDate)(matchedFormat, matchedValue))) {
274
+ setSelectedDays((0, _utils.parseDate)(...(0, _utils.additionalYears)(matchedFormat, matchedValue)));
275
+ } else if ((0, _utils.checkISOFormatAndLength)(value)) {
276
+ setSelectedDays((0, _utils.parseISODate)(value));
277
+ } else {
278
+ setSelectedDays(undefined);
570
279
  }
571
- }, {
572
- key: "componentWillUnmount",
573
- value: function componentWillUnmount() {
574
- this.hasMounted = false;
575
- }
576
- }, {
577
- key: "render",
578
- value: function render() {
579
- const {
580
- minDate,
581
- maxDate,
582
- labelInline,
583
- adaptiveLabelBreakpoint,
584
- disablePortal,
585
- tooltipPosition,
586
- "data-component": dataComponent,
587
- "data-element": dataElement,
588
- "data-role": dataRole,
589
- ...inputProps
590
- } = this.props;
591
- let events = {};
592
- delete inputProps.defaultValue;
593
- delete inputProps.value;
594
- events = {
595
- onBlur: this.handleBlur,
596
- onChange: this.handleVisibleInputChange,
597
- onFocus: this.handleFocus,
598
- onKeyDown: this.handleKeyDown,
599
- onClick: this.handleClick,
600
- iconOnClick: this.handleClick
601
- };
602
- return /*#__PURE__*/_react.default.createElement(_date2.default, _extends({
603
- role: "presentation",
604
- size: inputProps.size,
605
- labelInline: labelInline,
606
- "data-component": dataComponent,
607
- "data-element": dataElement,
608
- "data-role": dataRole
609
- }, (0, _utils.filterStyledSystemMarginProps)(this.props)), /*#__PURE__*/_react.default.createElement(_textbox.default, _extends({}, (0, _utils.filterOutStyledSystemSpacingProps)(inputProps), {
610
- inputIcon: "calendar",
611
- value: this.state.visibleValue,
612
- labelInline: labelInline,
613
- rawValue: _date.default.formatValue({
614
- value: this.state.visibleValue,
615
- ...this.localeData
616
- }),
617
- inputRef: this.assignInput,
618
- adaptiveLabelBreakpoint: adaptiveLabelBreakpoint,
619
- tooltipPosition: tooltipPosition,
620
- helpAriaLabel: this.props.helpAriaLabel
621
- }, events)), this.renderHiddenInput(), this.renderDatePicker({
622
- minDate,
623
- maxDate
624
- }));
625
- }
626
- }]);
280
+ }, [value, formats]);
627
281
 
628
- return BaseDateInput;
629
- }(_react.default.Component);
282
+ const computedValue = () => {
283
+ if ((0, _utils.checkISOFormatAndLength)(value)) {
284
+ return (0, _utils.formattedValue)(format, (0, _utils.parseISODate)(value));
285
+ }
630
286
 
631
- exports.BaseDateInput = BaseDateInput;
287
+ const valueSeparator = (0, _utils.getSeparator)(value);
288
+ const formatSeparator = (0, _utils.getSeparator)(format);
632
289
 
633
- function generateAdjustedValue({
634
- value,
635
- defaultValue,
636
- allowEmptyValue
637
- }, {
638
- locale,
639
- formats,
640
- format
641
- }) {
642
- if (value !== undefined && canReturnValue(value, allowEmptyValue)) {
643
- return _date.default.formatDateToCurrentLocale({
644
- value,
645
- locale,
646
- formats,
647
- format
648
- });
649
- }
650
-
651
- if (canReturnValue(defaultValue, allowEmptyValue)) {
652
- return _date.default.formatDateToCurrentLocale({
653
- value: defaultValue,
654
- locale,
655
- formats,
656
- format
657
- });
658
- }
659
-
660
- return _date.default.formatDateToCurrentLocale({
661
- value: _date.default.todayFormatted(),
662
- locale,
663
- formats,
664
- format
665
- });
666
- }
667
-
668
- function isValidInitialFormat(value) {
669
- return _date.default.isValidDate({
670
- value,
671
- format: hiddenDateFormat
672
- });
673
- }
674
-
675
- function canReturnValue(value, allowEmptyValue) {
676
- if (!allowEmptyValue && value && value.length) {
677
- const message = "The Date component must be initialised with a value in the iso (YYYY-MM-DD) format";
678
- (0, _invariant.default)(isValidInitialFormat(value), message);
679
- }
680
-
681
- return isValidInitialFormat(value) || allowEmptyValue && !value;
682
- }
683
-
684
- const DateInput = (0, _withUniqueIdProps.default)(BaseDateInput);
685
- BaseDateInput.contextType = _i18nContext.default;
686
- BaseDateInput.propTypes = { ..._textbox.default.propTypes,
687
- ...marginPropTypes,
290
+ const replaceSeparators = () => value.split("").map(char => char === valueSeparator ? formatSeparator : char).join("");
688
291
 
689
- /** Identifier used for testing purposes, applied to the root element of the component. */
690
- "data-component": _propTypes.default.string,
691
-
692
- /** Identifier used for testing purposes, applied to the root element of the component. */
693
- "data-element": _propTypes.default.string,
292
+ if (isInitialValue.current && valueSeparator !== formatSeparator && (0, _utils.isDateValid)((0, _utils.parseDate)(format, replaceSeparators()))) {
293
+ const [matchedFormat, matchedValue] = (0, _utils.findMatchedFormatAndValue)(replaceSeparators(), formats);
294
+ return (0, _utils.formattedValue)(format, (0, _utils.parseDate)(...(0, _utils.additionalYears)(matchedFormat, matchedValue)));
295
+ }
694
296
 
695
- /** Identifier used for testing purposes, applied to the root element of the component. */
696
- "data-role": _propTypes.default.string,
297
+ return value;
298
+ };
299
+
300
+ return /*#__PURE__*/_react.default.createElement(_date.default, _extends({
301
+ ref: wrapperRef,
302
+ role: "presentation",
303
+ size: size,
304
+ labelInline: labelInline,
305
+ "data-component": dataComponent || "date",
306
+ "data-element": dataElement,
307
+ "data-role": dataRole
308
+ }, (0, _utils2.filterStyledSystemMarginProps)(rest)), /*#__PURE__*/_react.default.createElement(_textbox.default, _extends({}, (0, _utils2.filterOutStyledSystemSpacingProps)(rest), {
309
+ value: computedValue(),
310
+ onBlur: handleBlur,
311
+ onChange: handleChange,
312
+ onClick: handleClick,
313
+ onFocus: handleFocus,
314
+ onKeyDown: handleKeyDown,
315
+ iconOnClick: handleClick,
316
+ onMouseDown: handleMouseDown,
317
+ iconOnMouseDown: handleMouseDown,
318
+ inputIcon: "calendar",
319
+ labelInline: labelInline,
320
+ inputRef: assignInput,
321
+ adaptiveLabelBreakpoint: adaptiveLabelBreakpoint,
322
+ tooltipPosition: tooltipPosition,
323
+ helpAriaLabel: helpAriaLabel,
324
+ autoFocus: autoFocus,
325
+ size: size,
326
+ disabled: disabled,
327
+ readOnly: readOnly
328
+ })), open && /*#__PURE__*/_react.default.createElement(_datePicker.default, {
329
+ disablePortal: disablePortal,
330
+ inputElement: parentRef,
331
+ pickerProps: pickerProps,
332
+ selectedDays: selectedDays,
333
+ setSelectedDays: setSelectedDays,
334
+ onDayClick: handleDayClick,
335
+ minDate: minDate,
336
+ maxDate: maxDate,
337
+ ref: pickerRef,
338
+ pickerMouseDown: handlePickerMouseDown
339
+ }));
340
+ };
697
341
 
698
- /** Boolean to allow the input to have an empty value */
699
- allowEmptyValue: _propTypes.default.bool,
342
+ DateInput.propTypes = { ..._textbox.default.propTypes,
343
+ ...marginPropTypes,
700
344
 
701
- /** Automatically focus on component mount */
702
- autoFocus: _propTypes.default.bool,
345
+ /** Pass any props that match the [DayPickerProps](https://react-day-picker.js.org/api/DayPicker)
346
+ * interface to override default behaviors
347
+ * */
348
+ pickerProps: _propTypes.default.object,
703
349
 
704
350
  /** Boolean to toggle where DatePicker is rendered in relation to the Date Input */
705
351
  disablePortal: _propTypes.default.bool,
@@ -710,30 +356,14 @@ BaseDateInput.propTypes = { ..._textbox.default.propTypes,
710
356
  /** Maximum possible date YYYY-MM-DD */
711
357
  maxDate: _propTypes.default.string,
712
358
 
713
- /** Specify a callback triggered on blur */
714
- onBlur: _propTypes.default.func,
715
-
716
359
  /** Specify a callback triggered on change */
717
- onChange: _propTypes.default.func,
718
-
719
- /** Specify a callback triggered on focus */
720
- onFocus: _propTypes.default.func,
360
+ onChange: _propTypes.default.func.isRequired,
721
361
 
722
- /** Name of the input */
723
- name: _propTypes.default.string,
362
+ /** The current date string */
363
+ value: _propTypes.default.string.isRequired,
724
364
 
725
- /** The current date YYYY-MM-DD */
726
- value: _propTypes.default.string,
727
-
728
- /** Breakpoint for adaptive label (inline labels change to top aligned). Enables the adaptive behaviour when set */
729
- adaptiveLabelBreakpoint: _propTypes.default.number,
730
-
731
- /** Flag to configure component as mandatory */
732
- required: _propTypes.default.bool
733
- };
734
- BaseDateInput.defaultProps = {
735
- disablePortal: false,
736
- "data-component": "date"
365
+ /** Boolean to allow the input to have an empty value */
366
+ allowEmptyValue: _propTypes.default.bool
737
367
  };
738
368
  var _default = DateInput;
739
369
  exports.default = _default;