@instructure/ui-date-input 9.6.1-snapshot-2 → 9.7.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.
@@ -44,45 +44,75 @@ var _IconCalendarMonthLin, _IconArrowOpenEndSoli, _IconArrowOpenStartSo;
44
44
  * SOFTWARE.
45
45
  */
46
46
  /** @jsx jsx */
47
- /*
48
- * Tries parsing a date in a given timezone, if it's not possible, returns an empty string
49
- * If parsing is successful an ISO formatted datetime string is returned in UTC timezone
50
- */
51
- function timezoneDateToUtc(dateString, timezone) {
52
- // Don't try to parse short dateString, they are incomplete
53
- if (dateString.length < 10) {
54
- return '';
55
- }
47
+ function parseLocaleDate(dateString = '', locale, timeZone) {
48
+ // This function may seem complicated but it basically does one thing:
49
+ // Given a dateString, a locale and a timeZone. The dateString is assumed to be formatted according
50
+ // to the locale. So if the locale is `en-us` the dateString is expected to be in the format of M/D/YYYY.
51
+ // The dateString is also assumed to be in the given timeZone, so "1/1/2020" in "America/Los_Angeles" timezone is
52
+ // expected to be "2020-01-01T08:00:00.000Z" in UTC time.
53
+ // This function tries to parse the dateString taking these variables into account and return a javascript Date object
54
+ // that is adjusted to be in UTC.
55
+
56
+ // Split string on '.', whitespace, '/', ',' or '-' using regex: /[.\s/.-]+/.
57
+ // The '+' allows splitting on consecutive delimiters.
58
+ // `.filter(Boolean)` is needed because some locales have a delimeter at the end (e.g.: hungarian dates are formatted as `2024. 09. 19.`)
59
+ const splitDate = dateString.split(/[,.\s/.-]+/).filter(Boolean);
60
+
61
+ // create a locale formatted new date to later extract the order and delimeter information
62
+ const localeDate = new Intl.DateTimeFormat(locale).formatToParts(new Date());
63
+ let index = 0;
64
+ let day, month, year;
65
+ localeDate.forEach(part => {
66
+ if (part.type === 'month') {
67
+ month = parseInt(splitDate[index], 10);
68
+ index++;
69
+ } else if (part.type === 'day') {
70
+ day = parseInt(splitDate[index], 10);
71
+ index++;
72
+ } else if (part.type === 'year') {
73
+ year = parseInt(splitDate[index], 10);
74
+ index++;
75
+ }
76
+ });
56
77
 
57
- // Create a Date object from the input date string
58
- const date = new Date(dateString);
78
+ // sensible limitations
79
+ if (!year || !month || !day || year < 1000 || year > 9999) return null;
59
80
 
60
- // Check if the date is valid
61
- if (isNaN(date.getTime())) {
62
- return '';
81
+ // create utc date from year, month (zero indexed) and day
82
+ const date = new Date(Date.UTC(year, month - 1, day));
83
+ if (date.getMonth() !== month - 1 || date.getDate() !== day) {
84
+ // Check if the Date object adjusts the values. If it does, the input is invalid.
85
+ return null;
63
86
  }
64
87
 
65
- // snippet from https://stackoverflow.com/a/57842203
66
- // but it might need to be improved:
67
- // "This produces incorrect datetimes for several hours surrounding daylight saving times if the
68
- // computer running the code is in a zone that doesn't obey the same daylight saving shifts as the target zone."
69
- const utcDate = new Date(date.toLocaleString('en-US', {
70
- timeZone: 'UTC'
71
- }));
72
- const tzDate = new Date(date.toLocaleString('en-US', {
73
- timeZone: timezone
74
- }));
75
- const offset = utcDate.getTime() - tzDate.getTime();
76
- const newDate = new Date(date.getTime() + offset);
77
- return newDate.toISOString();
78
- }
79
- function defaultDateFormatter(dateString, locale, timezone) {
80
- return new Date(dateString).toLocaleDateString(locale, {
81
- month: 'long',
88
+ // Format date string in the provided timezone. The locale here is irrelevant, we only care about how to time is adjusted for the timezone.
89
+ const parts = new Intl.DateTimeFormat('en-US', {
90
+ timeZone,
82
91
  year: 'numeric',
83
- day: 'numeric',
84
- timeZone: timezone
85
- });
92
+ month: '2-digit',
93
+ day: '2-digit',
94
+ hour: '2-digit',
95
+ minute: '2-digit',
96
+ second: '2-digit',
97
+ hour12: false
98
+ }).formatToParts(date);
99
+
100
+ // Extract the date and time parts from the formatted string
101
+ const dateStringInTimezone = parts.reduce((acc, part) => {
102
+ return part.type === 'literal' ? acc : {
103
+ ...acc,
104
+ [part.type]: part.value
105
+ };
106
+ }, {});
107
+
108
+ // Create a date string in the format 'YYYY-MM-DDTHH:mm:ss'
109
+ const dateInTimezone = `${dateStringInTimezone.year}-${dateStringInTimezone.month}-${dateStringInTimezone.day}T${dateStringInTimezone.hour}:${dateStringInTimezone.minute}:${dateStringInTimezone.second}`;
110
+
111
+ // Calculate time difference for timezone offset
112
+ const timeDiff = new Date(dateInTimezone + 'Z').getTime() - date.getTime();
113
+ const utcTime = new Date(date.getTime() - timeDiff);
114
+ // Return the UTC Date corresponding to the time in the specified timezone
115
+ return utcTime;
86
116
  }
87
117
 
88
118
  /**
@@ -97,7 +127,6 @@ const DateInput2 = ({
97
127
  screenReaderLabels,
98
128
  isRequired = false,
99
129
  interaction = 'enabled',
100
- size = 'medium',
101
130
  isInline = false,
102
131
  value,
103
132
  messages,
@@ -105,12 +134,12 @@ const DateInput2 = ({
105
134
  onChange,
106
135
  onBlur,
107
136
  withYearPicker,
108
- onRequestValidateDate,
109
137
  invalidDateErrorMessage,
110
138
  locale,
111
139
  timezone,
112
140
  placeholder,
113
- formatDate = defaultDateFormatter,
141
+ dateFormat,
142
+ onRequestValidateDate,
114
143
  // margin, TODO enable this prop
115
144
  ...rest
116
145
  }) => {
@@ -133,75 +162,111 @@ const DateInput2 = ({
133
162
  // default to the system's timezone
134
163
  return Intl.DateTimeFormat().resolvedOptions().timeZone;
135
164
  };
136
- const _useState = (0, _react.useState)(value ? formatDate(value, getLocale(), getTimezone()) : ''),
165
+ const _useState = (0, _react.useState)(messages || []),
137
166
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
138
- inputValue = _useState2[0],
139
- setInputValue = _useState2[1];
140
- const _useState3 = (0, _react.useState)(messages || []),
167
+ inputMessages = _useState2[0],
168
+ setInputMessages = _useState2[1];
169
+ const _useState3 = (0, _react.useState)(false),
141
170
  _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
142
- inputMessages = _useState4[0],
143
- setInputMessages = _useState4[1];
144
- const _useState5 = (0, _react.useState)(false),
145
- _useState6 = (0, _slicedToArray2.default)(_useState5, 2),
146
- showPopover = _useState6[0],
147
- setShowPopover = _useState6[1];
171
+ showPopover = _useState4[0],
172
+ setShowPopover = _useState4[1];
148
173
  (0, _react.useEffect)(() => {
149
- setInputMessages(messages || []);
174
+ // don't set input messages if there is an error set already
175
+ if (!inputMessages) {
176
+ setInputMessages(messages || []);
177
+ }
150
178
  }, [messages]);
151
179
  (0, _react.useEffect)(() => {
152
- validateInput(true);
153
- }, [inputValue]);
180
+ const _parseDate = parseDate(value),
181
+ _parseDate2 = (0, _slicedToArray2.default)(_parseDate, 2),
182
+ utcIsoDate = _parseDate2[1];
183
+ // clear error messages if date becomes valid
184
+ if (utcIsoDate || !value) {
185
+ setInputMessages(messages || []);
186
+ }
187
+ }, [value]);
188
+ const parseDate = (dateString = '') => {
189
+ let date = null;
190
+ if (dateFormat) {
191
+ if (typeof dateFormat === 'string') {
192
+ // use dateFormat instead of the user locale
193
+ date = parseLocaleDate(dateString, dateFormat, getTimezone());
194
+ } else if (dateFormat.parser) {
195
+ date = dateFormat.parser(dateString);
196
+ }
197
+ } else {
198
+ // no dateFormat prop passed, use locale for formatting
199
+ date = parseLocaleDate(dateString, getLocale(), getTimezone());
200
+ }
201
+ return date ? [formatDate(date), date.toISOString()] : ['', ''];
202
+ };
203
+ const formatDate = date => {
204
+ // use formatter function if provided
205
+ if (typeof dateFormat !== 'string' && dateFormat !== null && dateFormat !== void 0 && dateFormat.formatter) {
206
+ return dateFormat.formatter(date);
207
+ }
208
+ // if dateFormat set to a locale, use that, otherwise default to the user's locale
209
+ return date.toLocaleDateString(typeof dateFormat === 'string' ? dateFormat : getLocale(), {
210
+ timeZone: getTimezone(),
211
+ calendar: 'gregory',
212
+ numberingSystem: 'latn'
213
+ });
214
+ };
215
+ const getDateFromatHint = () => {
216
+ const exampleDate = new Date('2024-09-01');
217
+ const formattedDate = formatDate(exampleDate);
218
+
219
+ // Create a regular expression to find the exact match of the number
220
+ const regex = n => {
221
+ return new RegExp(`(?<!\\d)0*${n}(?!\\d)`, 'g');
222
+ };
223
+
224
+ // Replace the matched number with the same number of dashes
225
+ const year = `${exampleDate.getFullYear()}`;
226
+ const month = `${exampleDate.getMonth() + 1}`;
227
+ const day = `${exampleDate.getDate()}`;
228
+ return formattedDate.replace(regex(year), match => 'Y'.repeat(match.length)).replace(regex(month), match => 'M'.repeat(match.length)).replace(regex(day), match => 'D'.repeat(match.length));
229
+ };
154
230
  const handleInputChange = (e, newValue) => {
155
- setInputValue(newValue);
156
- const parsedInput = timezoneDateToUtc(newValue, getTimezone());
157
- onChange === null || onChange === void 0 ? void 0 : onChange(e, parsedInput, newValue);
231
+ const _parseDate3 = parseDate(newValue),
232
+ _parseDate4 = (0, _slicedToArray2.default)(_parseDate3, 2),
233
+ utcIsoDate = _parseDate4[1];
234
+ onChange === null || onChange === void 0 ? void 0 : onChange(e, newValue, utcIsoDate);
158
235
  };
159
236
  const handleDateSelected = (dateString, _momentDate, e) => {
160
- const formattedDate = formatDate(dateString, getLocale(), getTimezone());
161
- setInputValue(formattedDate);
162
237
  setShowPopover(false);
163
- onChange === null || onChange === void 0 ? void 0 : onChange(e, dateString, formattedDate);
164
- onRequestValidateDate === null || onRequestValidateDate === void 0 ? void 0 : onRequestValidateDate(dateString, true);
238
+ const newValue = formatDate(new Date(dateString));
239
+ onChange === null || onChange === void 0 ? void 0 : onChange(e, newValue, dateString);
240
+ onRequestValidateDate === null || onRequestValidateDate === void 0 ? void 0 : onRequestValidateDate(e, newValue, dateString);
165
241
  };
166
-
167
- // onlyRemoveError is used to remove the error msg immediately when the user inputs a valid date (and don't wait for blur event)
168
- const validateInput = (onlyRemoveError = false) => {
169
- // don't validate empty input
170
- if (!inputValue || timezoneDateToUtc(inputValue, getTimezone()) || value) {
171
- setInputMessages(messages || []);
172
- return true;
173
- }
174
- // only show error if there is no user provided validation callback
175
- if (!onlyRemoveError && typeof invalidDateErrorMessage === 'string' && !onRequestValidateDate) {
242
+ const handleBlur = e => {
243
+ const _parseDate5 = parseDate(value),
244
+ _parseDate6 = (0, _slicedToArray2.default)(_parseDate5, 2),
245
+ localeDate = _parseDate6[0],
246
+ utcIsoDate = _parseDate6[1];
247
+ if (localeDate) {
248
+ if (localeDate !== value) {
249
+ onChange === null || onChange === void 0 ? void 0 : onChange(e, localeDate, utcIsoDate);
250
+ }
251
+ } else if (value && invalidDateErrorMessage) {
176
252
  setInputMessages([{
177
253
  type: 'error',
178
254
  text: invalidDateErrorMessage
179
255
  }]);
180
256
  }
181
- return false;
182
- };
183
- const handleBlur = e => {
184
- if (value) {
185
- const formattedDate = formatDate(value, getLocale(), getTimezone());
186
- if (formattedDate !== inputValue) {
187
- setInputValue(formattedDate);
188
- onChange === null || onChange === void 0 ? void 0 : onChange(e, value, formattedDate);
189
- }
190
- }
191
- validateInput(false);
192
- onRequestValidateDate === null || onRequestValidateDate === void 0 ? void 0 : onRequestValidateDate(value, !!value);
193
- onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
257
+ onRequestValidateDate === null || onRequestValidateDate === void 0 ? void 0 : onRequestValidateDate(e, value || '', utcIsoDate);
258
+ onBlur === null || onBlur === void 0 ? void 0 : onBlur(e, value || '', utcIsoDate);
194
259
  };
260
+ const selectedDate = parseDate(value)[1];
195
261
  return (0, _emotion.jsx)(_TextInput.TextInput, Object.assign({}, (0, _passthroughProps.passthroughProps)(rest), {
196
262
  // margin={'large'} TODO add this prop to TextInput
197
263
  renderLabel: renderLabel,
198
264
  onChange: handleInputChange,
199
265
  onBlur: handleBlur,
200
266
  isRequired: isRequired,
201
- value: inputValue,
202
- placeholder: placeholder,
267
+ value: value,
268
+ placeholder: placeholder !== null && placeholder !== void 0 ? placeholder : getDateFromatHint(),
203
269
  width: width,
204
- size: size,
205
270
  display: isInline ? 'inline-block' : 'block',
206
271
  messages: inputMessages,
207
272
  interaction: interaction,
@@ -211,7 +276,6 @@ const DateInput2 = ({
211
276
  withBorder: false,
212
277
  screenReaderLabel: screenReaderLabels.calendarIcon,
213
278
  shape: "circle",
214
- size: size,
215
279
  interaction: interaction
216
280
  }, _IconCalendarMonthLin || (_IconCalendarMonthLin = (0, _emotion.jsx)(_IconCalendarMonthLine.IconCalendarMonthLine, null))),
217
281
  isShowingContent: showPopover,
@@ -224,8 +288,8 @@ const DateInput2 = ({
224
288
  }, (0, _emotion.jsx)(_Calendar.Calendar, {
225
289
  withYearPicker: withYearPicker,
226
290
  onDateSelected: handleDateSelected,
227
- selectedDate: value,
228
- visibleMonth: value,
291
+ selectedDate: selectedDate,
292
+ visibleMonth: selectedDate,
229
293
  locale: getLocale(),
230
294
  timezone: getTimezone(),
231
295
  role: "listbox",
@@ -36,7 +36,6 @@ const propTypes = exports.propTypes = {
36
36
  renderLabel: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.func]).isRequired,
37
37
  screenReaderLabels: _propTypes.default.object.isRequired,
38
38
  value: (0, _controllable.controllable)(_propTypes.default.string),
39
- size: _propTypes.default.oneOf(['small', 'medium', 'large']),
40
39
  placeholder: _propTypes.default.string,
41
40
  onChange: _propTypes.default.func,
42
41
  onBlur: _propTypes.default.func,
@@ -45,10 +44,10 @@ const propTypes = exports.propTypes = {
45
44
  isInline: _propTypes.default.bool,
46
45
  width: _propTypes.default.string,
47
46
  messages: _propTypes.default.arrayOf(_FormPropTypes.FormPropTypes.message),
48
- onRequestValidateDate: _propTypes.default.func,
49
47
  invalidDateErrorMessage: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
50
48
  locale: _propTypes.default.string,
51
49
  timezone: _propTypes.default.string,
52
50
  withYearPicker: _propTypes.default.object,
53
- formatDate: _propTypes.default.func
51
+ dateFormat: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.object]),
52
+ onRequestValidateDate: _propTypes.default.func
54
53
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-date-input",
3
- "version": "9.6.1-snapshot-2",
3
+ "version": "9.7.2",
4
4
  "description": "A UI component library made by Instructure Inc.",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -23,11 +23,11 @@
23
23
  },
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@instructure/ui-axe-check": "9.6.1-snapshot-2",
27
- "@instructure/ui-babel-preset": "9.6.1-snapshot-2",
28
- "@instructure/ui-buttons": "9.6.1-snapshot-2",
29
- "@instructure/ui-scripts": "9.6.1-snapshot-2",
30
- "@instructure/ui-test-utils": "9.6.1-snapshot-2",
26
+ "@instructure/ui-axe-check": "9.7.2",
27
+ "@instructure/ui-babel-preset": "9.7.2",
28
+ "@instructure/ui-buttons": "9.7.2",
29
+ "@instructure/ui-scripts": "9.7.2",
30
+ "@instructure/ui-test-utils": "9.7.2",
31
31
  "@testing-library/jest-dom": "^6.4.6",
32
32
  "@testing-library/react": "^15.0.7",
33
33
  "@testing-library/user-event": "^14.5.2",
@@ -35,20 +35,20 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@babel/runtime": "^7.24.5",
38
- "@instructure/emotion": "9.6.1-snapshot-2",
39
- "@instructure/shared-types": "9.6.1-snapshot-2",
40
- "@instructure/ui-calendar": "9.6.1-snapshot-2",
41
- "@instructure/ui-form-field": "9.6.1-snapshot-2",
42
- "@instructure/ui-i18n": "9.6.1-snapshot-2",
43
- "@instructure/ui-icons": "9.6.1-snapshot-2",
44
- "@instructure/ui-popover": "9.6.1-snapshot-2",
45
- "@instructure/ui-position": "9.6.1-snapshot-2",
46
- "@instructure/ui-prop-types": "9.6.1-snapshot-2",
47
- "@instructure/ui-react-utils": "9.6.1-snapshot-2",
48
- "@instructure/ui-selectable": "9.6.1-snapshot-2",
49
- "@instructure/ui-testable": "9.6.1-snapshot-2",
50
- "@instructure/ui-text-input": "9.6.1-snapshot-2",
51
- "@instructure/ui-utils": "9.6.1-snapshot-2",
38
+ "@instructure/emotion": "9.7.2",
39
+ "@instructure/shared-types": "9.7.2",
40
+ "@instructure/ui-calendar": "9.7.2",
41
+ "@instructure/ui-form-field": "9.7.2",
42
+ "@instructure/ui-i18n": "9.7.2",
43
+ "@instructure/ui-icons": "9.7.2",
44
+ "@instructure/ui-popover": "9.7.2",
45
+ "@instructure/ui-position": "9.7.2",
46
+ "@instructure/ui-prop-types": "9.7.2",
47
+ "@instructure/ui-react-utils": "9.7.2",
48
+ "@instructure/ui-selectable": "9.7.2",
49
+ "@instructure/ui-testable": "9.7.2",
50
+ "@instructure/ui-text-input": "9.7.2",
51
+ "@instructure/ui-utils": "9.7.2",
52
52
  "moment-timezone": "^0.5.45",
53
53
  "prop-types": "^15.8.1"
54
54
  },
@@ -2,7 +2,7 @@
2
2
  describes: DateInput
3
3
  ---
4
4
 
5
- > **Important:** You can now use are updated version [`DateInput2`](/#DateInput2) which is easier to configure for developers, has a better UX, better accessibility features and a year picker. We recommend using that instead of `DateInput` which will be deprecated in the future.
5
+ > *Note:* you can now try the updated (but still experimental) [`DateInput2`](/#DateInput2) which is easier to configure for developers, has a better UX, better accessibility features and a year picker. We recommend using that instead of `DateInput` which will be deprecated in the future.
6
6
 
7
7
  The `DateInput` component provides a visual interface for inputting date data.
8
8