@instructure/ui-date-time-input 8.52.1-snapshot-11 → 8.53.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-date-time-input",
3
- "version": "8.52.1-snapshot-11",
3
+ "version": "8.53.0",
4
4
  "description": "A UI component that allows to select date and time.",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -23,26 +23,30 @@
23
23
  },
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@instructure/ui-babel-preset": "8.52.1-snapshot-11",
27
- "@instructure/ui-test-locator": "8.52.1-snapshot-11",
28
- "@instructure/ui-test-utils": "8.52.1-snapshot-11"
26
+ "@instructure/ui-babel-preset": "8.53.0",
27
+ "@instructure/ui-test-locator": "8.53.0",
28
+ "@instructure/ui-test-utils": "8.53.0",
29
+ "@testing-library/jest-dom": "^6.1.4",
30
+ "@testing-library/react": "^14.0.0",
31
+ "@testing-library/user-event": "^14.5.1"
29
32
  },
30
33
  "dependencies": {
31
34
  "@babel/runtime": "^7.23.2",
32
- "@instructure/debounce": "8.52.1-snapshot-11",
33
- "@instructure/emotion": "8.52.1-snapshot-11",
34
- "@instructure/ui-a11y-content": "8.52.1-snapshot-11",
35
- "@instructure/ui-buttons": "8.52.1-snapshot-11",
36
- "@instructure/ui-calendar": "8.52.1-snapshot-11",
37
- "@instructure/ui-date-input": "8.52.1-snapshot-11",
38
- "@instructure/ui-form-field": "8.52.1-snapshot-11",
39
- "@instructure/ui-i18n": "8.52.1-snapshot-11",
40
- "@instructure/ui-icons": "8.52.1-snapshot-11",
41
- "@instructure/ui-prop-types": "8.52.1-snapshot-11",
42
- "@instructure/ui-react-utils": "8.52.1-snapshot-11",
43
- "@instructure/ui-testable": "8.52.1-snapshot-11",
44
- "@instructure/ui-time-select": "8.52.1-snapshot-11",
45
- "@instructure/ui-utils": "8.52.1-snapshot-11",
35
+ "@instructure/console": "8.53.0",
36
+ "@instructure/debounce": "8.53.0",
37
+ "@instructure/emotion": "8.53.0",
38
+ "@instructure/ui-a11y-content": "8.53.0",
39
+ "@instructure/ui-buttons": "8.53.0",
40
+ "@instructure/ui-calendar": "8.53.0",
41
+ "@instructure/ui-date-input": "8.53.0",
42
+ "@instructure/ui-form-field": "8.53.0",
43
+ "@instructure/ui-i18n": "8.53.0",
44
+ "@instructure/ui-icons": "8.53.0",
45
+ "@instructure/ui-prop-types": "8.53.0",
46
+ "@instructure/ui-react-utils": "8.53.0",
47
+ "@instructure/ui-testable": "8.53.0",
48
+ "@instructure/ui-time-select": "8.53.0",
49
+ "@instructure/ui-utils": "8.53.0",
46
50
  "prop-types": "^15.8.1"
47
51
  },
48
52
  "peerDependencies": {
@@ -0,0 +1,211 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2018 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { fireEvent, render, screen } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { userEvent } from '@testing-library/user-event'
29
+ import DateTimeInput from '../index'
30
+
31
+ describe('<DateTimeInput />', () => {
32
+ it("should change value of TimeSelect to initialTimeForNewDate prop's value", async () => {
33
+ const locale = 'en-US'
34
+ const timezone = 'US/Eastern'
35
+
36
+ render(
37
+ <DateTimeInput
38
+ description="date time"
39
+ prevMonthLabel="Previous month"
40
+ nextMonthLabel="Next month"
41
+ dateRenderLabel="date"
42
+ timeRenderLabel="time"
43
+ invalidDateTimeMessage="whoops"
44
+ locale={locale}
45
+ timezone={timezone}
46
+ initialTimeForNewDate="05:05"
47
+ />
48
+ )
49
+
50
+ const input = screen.getAllByRole('combobox')[0]
51
+
52
+ fireEvent.click(input)
53
+
54
+ const firstDay = screen.getByText('15')
55
+
56
+ await userEvent.click(firstDay)
57
+
58
+ const allInputs = screen.getAllByRole('combobox')
59
+ const targetInput = allInputs.find(
60
+ (input) => (input as HTMLInputElement).value === '5:05 AM'
61
+ )
62
+ expect(targetInput).toBeInTheDocument()
63
+ })
64
+
65
+ it("should throw warning if initialTimeForNewDate prop's value is not HH:MM", async () => {
66
+ const locale = 'en-US'
67
+ const timezone = 'US/Eastern'
68
+
69
+ const consoleError = jest
70
+ .spyOn(console, 'error')
71
+ .mockImplementation(() => {})
72
+
73
+ const initialTimeForNewDate = 'WRONG_FORMAT'
74
+
75
+ render(
76
+ <DateTimeInput
77
+ description="date time"
78
+ prevMonthLabel="Previous month"
79
+ nextMonthLabel="Next month"
80
+ dateRenderLabel="date"
81
+ timeRenderLabel="time"
82
+ invalidDateTimeMessage="whoops"
83
+ locale={locale}
84
+ timezone={timezone}
85
+ initialTimeForNewDate={initialTimeForNewDate}
86
+ />
87
+ )
88
+
89
+ expect(consoleError.mock.calls[0][2]).toContain(
90
+ `Invalid prop \`initialTimeForNewDate\` \`${initialTimeForNewDate}\` supplied to \`DateTimeInput\`, expected a HH:MM formatted string.`
91
+ )
92
+
93
+ const input = screen.getAllByRole('combobox')[0]
94
+
95
+ fireEvent.click(input)
96
+
97
+ const firstDay = screen.getByText('15')
98
+
99
+ await userEvent.click(firstDay)
100
+
101
+ expect(consoleError.mock.calls[1][0]).toBe(
102
+ `Warning: [DateTimeInput] initialTimeForNewDate prop is not in the correct format. Please use HH:MM format.`
103
+ )
104
+ })
105
+
106
+ it('should throw warning if initialTimeForNewDate prop hour and minute values are not in interval', async () => {
107
+ const locale = 'en-US'
108
+ const timezone = 'US/Eastern'
109
+
110
+ const consoleError = jest
111
+ .spyOn(console, 'error')
112
+ .mockImplementation(() => {})
113
+
114
+ const initialTimeForNewDate = '99:99'
115
+
116
+ render(
117
+ <DateTimeInput
118
+ description="date time"
119
+ prevMonthLabel="Previous month"
120
+ nextMonthLabel="Next month"
121
+ dateRenderLabel="date"
122
+ timeRenderLabel="time"
123
+ invalidDateTimeMessage="whoops"
124
+ locale={locale}
125
+ timezone={timezone}
126
+ initialTimeForNewDate={initialTimeForNewDate}
127
+ />
128
+ )
129
+
130
+ const input = screen.getAllByRole('combobox')[0]
131
+
132
+ fireEvent.click(input)
133
+
134
+ const firstDay = screen.getByText('15')
135
+
136
+ await userEvent.click(firstDay)
137
+
138
+ expect(consoleError.mock.calls[0][0]).toContain(
139
+ `Warning: [DateTimeInput] 0 <= hour < 24 and 0 <= minute < 60 for initialTimeForNewDate prop.`
140
+ )
141
+ })
142
+
143
+ /*
144
+ * TODO write this test with Cypress
145
+ * Steps:
146
+ * 0. Set initialTimeForNewDate and defaultValue props -> check if defaultValue is rendered in the date input and time input fields
147
+ * 1. Clear date input field
148
+ * 2. Click on an area outside of the component
149
+ * 3. Check if time input field got cleared as well
150
+ * 4. Click on the date input and select a date
151
+ * 5. Observe if the time input has the value of initialTimeForNewDate
152
+ */
153
+
154
+ // it('should merge defaultValue and initialTimeForNewDate when the user clears date input', async () => {
155
+ // const locale = 'en-US'
156
+ // const timezone = 'US/Eastern'
157
+ //
158
+ // // const consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {})
159
+ //
160
+ // const initialTimeForNewDate = '16:16'
161
+ // const defaultValue = '2018-01-18T13:30'
162
+ //
163
+ // const {container} = render(
164
+ // <DateTimeInput
165
+ // description="date time"
166
+ // prevMonthLabel="Previous month"
167
+ // nextMonthLabel="Next month"
168
+ // dateRenderLabel="date"
169
+ // timeRenderLabel="time"
170
+ // invalidDateTimeMessage="whoops"
171
+ // defaultValue={defaultValue}
172
+ // locale={locale}
173
+ // timezone={timezone}
174
+ // initialTimeForNewDate={initialTimeForNewDate}
175
+ // />
176
+ // )
177
+ //
178
+ // const dateInput = container.querySelector('input')
179
+ //
180
+ // const timeInput = container.querySelectorAll('input')[1]
181
+ //
182
+ // fireEvent.change(dateInput as Element, { target: { value: '' } })
183
+ //
184
+ //
185
+ // await waitFor(() => {
186
+ // fireEvent.keyDown(dateInput as Element, { key: 'Escape' })
187
+ //
188
+ // expect(timeInput).toHaveValue('')
189
+ // })
190
+ //
191
+ // // fireEvent.
192
+ // // fireEvent.change(timeInput as Element, { target: { value: '' } })
193
+ //
194
+ //
195
+ // const firstDay = screen.getByText('15')
196
+ //
197
+ // await userEvent.click(firstDay)
198
+ //
199
+ // const time = screen.getByText('4:16 PM')
200
+ //
201
+ // expect(time).toBeInTheDocument()
202
+ //
203
+ // // expect(consoleWarn.mock.calls[0][0]).toContain(
204
+ // // `Warning: [DateTimeInput] if defaultValue is set, initialTimeForNewDate will be ignored.`
205
+ // // )
206
+ // })
207
+
208
+ afterEach(() => {
209
+ jest.resetAllMocks()
210
+ })
211
+ })
@@ -40,6 +40,7 @@ import {
40
40
  } from '@instructure/ui-icons'
41
41
  import type { DateTimeInputProps, DateTimeInputState } from './props'
42
42
  import { propTypes, allowedProps } from './props'
43
+ import { error } from '@instructure/console'
43
44
 
44
45
  /**
45
46
  ---
@@ -134,6 +135,23 @@ class DateTimeInput extends Component<DateTimeInputProps, DateTimeInputState> {
134
135
  .month(this.state.iso.month())
135
136
  .year(this.state.iso.year())
136
137
  }
138
+ if (this.props.initialTimeForNewDate && !this.state?.timeSelectValue) {
139
+ const hour = Number(this.props.initialTimeForNewDate.slice(0, 2))
140
+ const minute = Number(this.props.initialTimeForNewDate.slice(3, 5))
141
+ if (isNaN(hour) || isNaN(minute)) {
142
+ error(
143
+ false,
144
+ `[DateTimeInput] initialTimeForNewDate prop is not in the correct format. Please use HH:MM format.`
145
+ )
146
+ } else if (hour < 0 || hour > 23 || minute > 59 || minute < 0) {
147
+ error(
148
+ false,
149
+ `[DateTimeInput] 0 <= hour < 24 and 0 <= minute < 60 for initialTimeForNewDate prop.`
150
+ )
151
+ } else {
152
+ parsed.hour(hour).minute(minute)
153
+ }
154
+ }
137
155
  const newTimeSelectValue = parsed.toISOString()
138
156
  if (this.isDisabledDate(parsed)) {
139
157
  let text =
@@ -28,7 +28,7 @@ import type { FormMessage } from '@instructure/ui-form-field'
28
28
  import type { InteractionType } from '@instructure/ui-react-utils'
29
29
  import { I18nPropTypes } from '@instructure/ui-i18n'
30
30
  import type { Moment } from '@instructure/ui-i18n'
31
- import PropTypes from 'prop-types'
31
+ import PropTypes, { Validator } from 'prop-types'
32
32
  import { controllable } from '@instructure/ui-prop-types'
33
33
  import type { PropValidators, Renderable } from '@instructure/shared-types'
34
34
 
@@ -242,6 +242,11 @@ type DateTimeInputProps = {
242
242
  * Default is `undefined` which equals to `false`
243
243
  */
244
244
  allowNonStepInput?: boolean
245
+ /**
246
+ * The default time to be prefilled if a day is selected. The time input has to be empty for this to be applied.
247
+ * An error is thrown if the time format is not HH:MM.
248
+ */
249
+ initialTimeForNewDate?: string
245
250
  }
246
251
 
247
252
  type DateTimeInputState = {
@@ -269,6 +274,26 @@ type DateTimeInputState = {
269
274
  type PropKeys = keyof DateTimeInputProps
270
275
  type AllowedPropKeys = Readonly<Array<PropKeys>>
271
276
 
277
+ const hourMinuteValidator: Validator<string> = function (
278
+ props,
279
+ propName,
280
+ componentName,
281
+ location
282
+ ) {
283
+ const propValue = props[propName]
284
+ if (typeof propValue === 'undefined' || propValue === '') return null
285
+
286
+ const hourMinuteRegex = /^\d{2}:\d{2}$/
287
+
288
+ if (typeof propValue === 'string' && !propValue.match(hourMinuteRegex)) {
289
+ return new Error(
290
+ `Invalid ${location} \`${propName}\` \`${propValue}\` supplied to \`${componentName}\`, expected ` +
291
+ `a HH:MM formatted string.`
292
+ )
293
+ }
294
+ return null
295
+ }
296
+
272
297
  const propTypes: PropValidators<PropKeys> = {
273
298
  description: PropTypes.node.isRequired,
274
299
  dateRenderLabel: PropTypes.oneOfType([PropTypes.func, PropTypes.node])
@@ -313,7 +338,8 @@ const propTypes: PropValidators<PropKeys> = {
313
338
  PropTypes.string,
314
339
  PropTypes.func
315
340
  ]),
316
- allowNonStepInput: PropTypes.bool
341
+ allowNonStepInput: PropTypes.bool,
342
+ initialTimeForNewDate: hourMinuteValidator
317
343
  }
318
344
 
319
345
  const allowedProps: AllowedPropKeys = [
@@ -23,6 +23,7 @@
23
23
  { "path": "../ui-react-utils/tsconfig.build.json" },
24
24
  { "path": "../ui-testable/tsconfig.build.json" },
25
25
  { "path": "../ui-time-select/tsconfig.build.json" },
26
- { "path": "../ui-utils/tsconfig.build.json" }
26
+ { "path": "../ui-utils/tsconfig.build.json" },
27
+ { "path": "../console/tsconfig.build.json" }
27
28
  ]
28
29
  }