@instructure/ui-date-input 11.6.0 → 11.6.1-snapshot-129

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 (63) hide show
  1. package/CHANGELOG.md +48 -309
  2. package/es/DateInput/{index.js → v1/index.js} +4 -4
  3. package/es/DateInput/v2/index.js +407 -0
  4. package/es/DateInput/v2/props.js +26 -0
  5. package/es/DateInput/v2/styles.js +46 -0
  6. package/es/DateInput2/{index.js → v1/index.js} +13 -11
  7. package/es/{index.js → exports/a.js} +2 -2
  8. package/{src/index.ts → es/exports/b.js} +2 -5
  9. package/lib/DateInput/{index.js → v1/index.js} +9 -9
  10. package/lib/DateInput/v2/index.js +416 -0
  11. package/lib/DateInput/v2/props.js +31 -0
  12. package/lib/DateInput/v2/styles.js +52 -0
  13. package/lib/DateInput2/{index.js → v1/index.js} +19 -19
  14. package/lib/{index.js → exports/a.js} +4 -4
  15. package/lib/exports/b.js +19 -0
  16. package/package.json +45 -23
  17. package/src/DateInput/{index.tsx → v1/index.tsx} +10 -7
  18. package/src/DateInput/{props.ts → v1/props.ts} +2 -2
  19. package/src/DateInput/v2/README.md +315 -0
  20. package/src/DateInput/v2/index.tsx +498 -0
  21. package/src/DateInput/v2/props.ts +316 -0
  22. package/src/DateInput/v2/styles.ts +52 -0
  23. package/src/DateInput2/{README.md → v1/README.md} +9 -9
  24. package/src/DateInput2/{index.tsx → v1/index.tsx} +11 -11
  25. package/src/DateInput2/{props.ts → v1/props.ts} +1 -1
  26. package/src/exports/a.ts +28 -0
  27. package/src/exports/b.ts +28 -0
  28. package/tsconfig.build.tsbuildinfo +1 -1
  29. package/types/DateInput/{index.d.ts → v1/index.d.ts} +4 -4
  30. package/types/DateInput/v1/index.d.ts.map +1 -0
  31. package/types/DateInput/{props.d.ts → v1/props.d.ts} +2 -2
  32. package/types/DateInput/v1/props.d.ts.map +1 -0
  33. package/types/DateInput/v1/styles.d.ts.map +1 -0
  34. package/types/DateInput/v2/index.d.ts +109 -0
  35. package/types/DateInput/v2/index.d.ts.map +1 -0
  36. package/types/DateInput/v2/props.d.ts +234 -0
  37. package/types/DateInput/v2/props.d.ts.map +1 -0
  38. package/types/DateInput/v2/styles.d.ts +13 -0
  39. package/types/DateInput/v2/styles.d.ts.map +1 -0
  40. package/types/DateInput2/{index.d.ts → v1/index.d.ts} +2 -2
  41. package/types/DateInput2/v1/index.d.ts.map +1 -0
  42. package/types/DateInput2/{props.d.ts → v1/props.d.ts} +1 -1
  43. package/types/DateInput2/v1/props.d.ts.map +1 -0
  44. package/types/exports/a.d.ts +5 -0
  45. package/types/exports/a.d.ts.map +1 -0
  46. package/types/exports/b.d.ts +5 -0
  47. package/types/exports/b.d.ts.map +1 -0
  48. package/types/DateInput/index.d.ts.map +0 -1
  49. package/types/DateInput/props.d.ts.map +0 -1
  50. package/types/DateInput/styles.d.ts.map +0 -1
  51. package/types/DateInput2/index.d.ts.map +0 -1
  52. package/types/DateInput2/props.d.ts.map +0 -1
  53. package/types/index.d.ts +0 -5
  54. package/types/index.d.ts.map +0 -1
  55. /package/es/DateInput/{props.js → v1/props.js} +0 -0
  56. /package/es/DateInput/{styles.js → v1/styles.js} +0 -0
  57. /package/es/DateInput2/{props.js → v1/props.js} +0 -0
  58. /package/lib/DateInput/{props.js → v1/props.js} +0 -0
  59. /package/lib/DateInput/{styles.js → v1/styles.js} +0 -0
  60. /package/lib/DateInput2/{props.js → v1/props.js} +0 -0
  61. /package/src/DateInput/{README.md → v1/README.md} +0 -0
  62. /package/src/DateInput/{styles.ts → v1/styles.ts} +0 -0
  63. /package/types/DateInput/{styles.d.ts → v1/styles.d.ts} +0 -0
@@ -0,0 +1,498 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - 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 { Children, Component, ReactElement } from 'react'
26
+
27
+ import { Calendar } from '@instructure/ui-calendar/latest'
28
+ import type {
29
+ CalendarProps,
30
+ CalendarDayProps
31
+ } from '@instructure/ui-calendar/latest'
32
+ import { CalendarInstUIIcon } from '@instructure/ui-icons'
33
+ import { Popover } from '@instructure/ui-popover/latest'
34
+ import { Selectable } from '@instructure/ui-selectable'
35
+ import type {
36
+ SelectableProps,
37
+ SelectableRender
38
+ } from '@instructure/ui-selectable'
39
+ import { TextInput } from '@instructure/ui-text-input/latest'
40
+ import type { TextInputProps } from '@instructure/ui-text-input/latest'
41
+ import { createChainedFunction } from '@instructure/ui-utils'
42
+ import {
43
+ getInteraction,
44
+ callRenderProp,
45
+ safeCloneElement,
46
+ passthroughProps
47
+ } from '@instructure/ui-react-utils'
48
+
49
+ import { DateTime, ApplyLocaleContext, Locale } from '@instructure/ui-i18n'
50
+
51
+ import { withStyle } from '@instructure/emotion'
52
+
53
+ import generateStyle from './styles'
54
+
55
+ import { allowedProps } from './props'
56
+ import type { DateInputProps, DateInputState } from './props'
57
+ import type { FormMessage } from '@instructure/ui-form-field/latest'
58
+
59
+ /**
60
+ ---
61
+ category: components
62
+ ---
63
+ **/
64
+ @withStyle(generateStyle)
65
+ class DateInput extends Component<DateInputProps, DateInputState> {
66
+ static readonly componentId = 'DateInput'
67
+ static Day = Calendar.Day
68
+ declare context: React.ContextType<typeof ApplyLocaleContext>
69
+ static allowedProps = allowedProps
70
+ static defaultProps = {
71
+ value: '',
72
+ size: 'medium',
73
+ onBlur: () => {}, // must have a default so createChainedFunction works
74
+ isRequired: false,
75
+ isInline: false,
76
+ layout: 'stacked',
77
+ display: 'inline-block',
78
+ placement: 'bottom center',
79
+ isShowingCalendar: false
80
+ }
81
+
82
+ state = {
83
+ hasInputRef: false,
84
+ isShowingCalendar: false,
85
+ validatedDate: undefined,
86
+ messages: []
87
+ }
88
+ _input?: HTMLInputElement | null = undefined
89
+ ref: Element | null = null
90
+
91
+ locale(): string {
92
+ if (this.props.locale) {
93
+ return this.props.locale
94
+ } else if (this.context && this.context.locale) {
95
+ return this.context.locale
96
+ }
97
+ return Locale.browserLocale()
98
+ }
99
+
100
+ timezone() {
101
+ if (this.props.timezone) {
102
+ return this.props.timezone
103
+ } else if (this.context && this.context.timezone) {
104
+ return this.context.timezone
105
+ }
106
+ return DateTime.browserTimeZone()
107
+ }
108
+
109
+ componentDidMount() {
110
+ this.props.makeStyles?.()
111
+ }
112
+
113
+ componentDidUpdate() {
114
+ this.props.makeStyles?.()
115
+ }
116
+
117
+ get selectedDateId() {
118
+ let selectedDateId: string | undefined
119
+ Children.toArray(this.props.children).forEach((day) => {
120
+ const { date, isSelected } = (day as ReactElement<CalendarDayProps>).props
121
+ if (isSelected) {
122
+ selectedDateId = this.formatDateId(date)
123
+ }
124
+ })
125
+ return selectedDateId
126
+ }
127
+
128
+ get interaction() {
129
+ return getInteraction({ props: this.props })
130
+ }
131
+
132
+ formatDateId = (date: string) => {
133
+ // ISO8601 strings may contain a space. Remove any spaces before using the
134
+ // date as the id.
135
+ return date.replace(/\s/g, '')
136
+ }
137
+
138
+ handleInputRef: TextInputProps['inputRef'] = (el) => {
139
+ // Ensures that we position the Calendar with respect to the input correctly
140
+ // if the Calendar is open on mount
141
+ if (!this.state.hasInputRef) {
142
+ this.setState({ hasInputRef: true })
143
+ }
144
+ this._input = el
145
+ this.props.inputRef?.(el)
146
+ }
147
+
148
+ handleInputChange: TextInputProps['onChange'] = (event, value) => {
149
+ this.props.onChange?.(event, { value })
150
+ this.handleShowCalendar(event)
151
+ }
152
+
153
+ handleShowCalendar = (event: React.SyntheticEvent) => {
154
+ if (!this.props.children) {
155
+ this.setState({ isShowingCalendar: true })
156
+ } else if (this.interaction === 'enabled' && this.props.children) {
157
+ this.props.onRequestShowCalendar?.(event)
158
+ }
159
+ }
160
+
161
+ validateDate = (date: string) => {
162
+ const { invalidDateErrorMessage } = this.props
163
+ const disabledDateErrorMessage =
164
+ this.props.disabledDateErrorMessage || invalidDateErrorMessage
165
+ const messages: FormMessage[] = []
166
+ // check if date is enabled
167
+ const { disabledDates } = this.props
168
+ if (
169
+ (typeof disabledDates === 'function' && disabledDates(date)) ||
170
+ (Array.isArray(disabledDates) &&
171
+ disabledDates.find((dateString) =>
172
+ DateTime.parse(dateString, this.locale(), this.timezone()).isSame(
173
+ DateTime.parse(date, this.locale(), this.timezone()),
174
+ 'day'
175
+ )
176
+ ))
177
+ ) {
178
+ messages.push(
179
+ typeof disabledDateErrorMessage === 'function'
180
+ ? disabledDateErrorMessage(date)
181
+ : { type: 'error', text: disabledDateErrorMessage }
182
+ )
183
+ }
184
+
185
+ // check if date is valid
186
+ if (
187
+ !DateTime.parse(
188
+ date,
189
+ this.locale(),
190
+ this.timezone(),
191
+ [
192
+ DateTime.momentISOFormat,
193
+ 'llll',
194
+ 'LLLL',
195
+ 'lll',
196
+ 'LLL',
197
+ 'll',
198
+ 'LL',
199
+ 'l',
200
+ 'L'
201
+ ],
202
+ true
203
+ ).isValid()
204
+ ) {
205
+ messages.push(
206
+ typeof invalidDateErrorMessage === 'function'
207
+ ? invalidDateErrorMessage(date)
208
+ : { type: 'error', text: invalidDateErrorMessage }
209
+ )
210
+ }
211
+
212
+ return messages
213
+ }
214
+
215
+ handleHideCalendar = (event: React.SyntheticEvent, setectedDate?: string) => {
216
+ if (!this.props.children) {
217
+ const dateString = setectedDate || this.props.value
218
+ const messages: FormMessage[] = []
219
+ if (this.props.onRequestValidateDate) {
220
+ const userValidatedDate = this.props.onRequestValidateDate?.(
221
+ event,
222
+ dateString || '',
223
+ this.validateDate(dateString || '')
224
+ )
225
+ messages.push(...(userValidatedDate || []))
226
+ } else {
227
+ if (dateString) {
228
+ messages.push(...this.validateDate(dateString))
229
+ }
230
+ }
231
+ this.setState({ messages, isShowingCalendar: false })
232
+ } else {
233
+ this.props.onRequestValidateDate?.(event)
234
+ this.props.onRequestHideCalendar?.(event)
235
+ }
236
+ }
237
+
238
+ handleHighlightOption: SelectableProps['onRequestHighlightOption'] = (
239
+ event,
240
+ { direction }
241
+ ) => {
242
+ const {
243
+ onRequestSelectNextDay,
244
+ onRequestSelectPrevDay,
245
+ onChange,
246
+ value,
247
+ currentDate
248
+ } = this.props
249
+
250
+ const isValueValid =
251
+ value && DateTime.parse(value, this.locale(), this.timezone()).isValid()
252
+
253
+ if (direction === -1) {
254
+ if (onRequestSelectPrevDay) {
255
+ onRequestSelectPrevDay?.(event)
256
+ } else {
257
+ // @ts-expect-error TODO
258
+ onChange(event, {
259
+ value: DateTime.parse(
260
+ isValueValid ? value : currentDate!,
261
+ this.locale(),
262
+ this.timezone()
263
+ )
264
+ .subtract(1, 'day')
265
+ .format('MMMM D, YYYY')
266
+ })
267
+ this.setState({ messages: [] })
268
+ }
269
+ }
270
+ if (direction === 1) {
271
+ if (onRequestSelectNextDay) {
272
+ onRequestSelectNextDay?.(event)
273
+ } else {
274
+ // @ts-expect-error TODO
275
+ onChange(event, {
276
+ value: DateTime.parse(
277
+ isValueValid ? value : currentDate!,
278
+ this.locale(),
279
+ this.timezone()
280
+ )
281
+ .add(1, 'day')
282
+ .format('MMMM D, YYYY')
283
+ })
284
+ this.setState({ messages: [] })
285
+ }
286
+ }
287
+ }
288
+
289
+ renderMonthNavigationButton(type = 'prev') {
290
+ const { renderPrevMonthButton, renderNextMonthButton } = this.props
291
+ const button =
292
+ type === 'prev' ? renderPrevMonthButton : renderNextMonthButton
293
+ return button && safeCloneElement(callRenderProp(button), { tabIndex: -1 })
294
+ }
295
+
296
+ renderDays(getOptionProps: SelectableRender['getOptionProps']) {
297
+ const children = this.props.children as ReactElement<CalendarDayProps>[]
298
+ if (!children) return
299
+ return Children.map(children, (day) => {
300
+ const { date, isOutsideMonth } = day.props
301
+ const props = { tabIndex: -1, id: this.formatDateId(date) }
302
+ const optionProps = getOptionProps(props)
303
+
304
+ const propsAdded = isOutsideMonth
305
+ ? {
306
+ ...props,
307
+ onClick: optionProps.onClick,
308
+ role: 'presentation'
309
+ }
310
+ : optionProps
311
+
312
+ return safeCloneElement(day, propsAdded)
313
+ })
314
+ }
315
+
316
+ renderCalendar({
317
+ getListProps,
318
+ getOptionProps
319
+ }: {
320
+ getListProps: SelectableRender['getListProps']
321
+ getOptionProps: SelectableRender['getOptionProps']
322
+ }) {
323
+ const {
324
+ onRequestRenderNextMonth,
325
+ onRequestRenderPrevMonth,
326
+ renderNavigationLabel,
327
+ renderWeekdayLabels,
328
+ value,
329
+ onChange,
330
+ disabledDates,
331
+ currentDate
332
+ } = this.props
333
+
334
+ const isValidDate = value
335
+ ? DateTime.parse(value, this.locale(), this.timezone()).isValid()
336
+ : false
337
+
338
+ const noChildrenProps = this.props.children
339
+ ? {}
340
+ : {
341
+ disabledDates,
342
+ currentDate,
343
+ selectedDate: isValidDate ? value : undefined,
344
+ visibleMonth: isValidDate ? value : undefined,
345
+ onDateSelected: (
346
+ dateString: string,
347
+ momentDate: any,
348
+ e: React.MouseEvent
349
+ ) => {
350
+ // @ts-expect-error TODO
351
+ onChange?.(e, {
352
+ value: `${momentDate.format('MMMM')} ${momentDate.format(
353
+ 'D'
354
+ )}, ${momentDate.format('YYYY')}`
355
+ })
356
+ this.handleHideCalendar(e, dateString)
357
+ }
358
+ }
359
+
360
+ return (
361
+ <Calendar
362
+ {...(getListProps({
363
+ onRequestRenderNextMonth,
364
+ onRequestRenderPrevMonth,
365
+ renderNavigationLabel,
366
+ renderWeekdayLabels,
367
+ renderNextMonthButton: this.renderMonthNavigationButton('next'),
368
+ renderPrevMonthButton: this.renderMonthNavigationButton('prev')
369
+ }) as CalendarProps)}
370
+ {...noChildrenProps}
371
+ >
372
+ {this.renderDays(getOptionProps)}
373
+ </Calendar>
374
+ )
375
+ }
376
+
377
+ renderInput({
378
+ getInputProps,
379
+ getTriggerProps
380
+ }: {
381
+ getInputProps: SelectableRender['getInputProps']
382
+ getTriggerProps: SelectableRender['getInputProps']
383
+ }) {
384
+ const {
385
+ renderLabel,
386
+ value,
387
+ placeholder,
388
+ onBlur,
389
+ isRequired,
390
+ size,
391
+ isInline,
392
+ layout,
393
+ width,
394
+ onRequestValidateDate,
395
+ onRequestShowCalendar,
396
+ onRequestHideCalendar,
397
+ onRequestSelectNextDay,
398
+ onRequestSelectPrevDay,
399
+ onRequestRenderNextMonth,
400
+ onRequestRenderPrevMonth,
401
+ ...rest
402
+ } = this.props
403
+
404
+ const { interaction } = this
405
+
406
+ const {
407
+ ref, // Apply this to the actual inputRef
408
+ ...triggerProps
409
+ } = getTriggerProps()
410
+ const messages = this.props.messages || this.state.messages
411
+ return (
412
+ <TextInput
413
+ {...triggerProps}
414
+ {...passthroughProps(rest)}
415
+ {...getInputProps({
416
+ renderLabel: callRenderProp(renderLabel),
417
+ value,
418
+ placeholder,
419
+ size,
420
+ layout,
421
+ width,
422
+ messages,
423
+ onChange: this.handleInputChange,
424
+ onBlur: createChainedFunction(onBlur, this.handleHideCalendar),
425
+ inputRef: createChainedFunction(ref, this.handleInputRef),
426
+ interaction,
427
+ isRequired,
428
+ display: isInline ? 'inline-block' : 'block',
429
+ renderAfterInput: <CalendarInstUIIcon />
430
+ })}
431
+ onKeyDown={(e) => {
432
+ if (!this.props.children) {
433
+ if (e.key === 'Enter') {
434
+ // @ts-expect-error TODO
435
+ this.handleHideCalendar(e)
436
+ }
437
+ }
438
+ triggerProps.onKeyDown?.(e)
439
+ }}
440
+ />
441
+ )
442
+ }
443
+
444
+ shouldShowCalendar = () =>
445
+ this.props.children
446
+ ? this.props.isShowingCalendar
447
+ : this.state.isShowingCalendar
448
+
449
+ render() {
450
+ const { placement, assistiveText, styles } = this.props
451
+ const isShowingCalendar = this.shouldShowCalendar()
452
+ return (
453
+ <Selectable
454
+ isShowingOptions={isShowingCalendar}
455
+ onRequestShowOptions={this.handleShowCalendar}
456
+ onRequestHideOptions={this.handleHideCalendar}
457
+ onRequestHighlightOption={this.handleHighlightOption}
458
+ onRequestSelectOption={(e) => this.handleHideCalendar(e)}
459
+ selectedOptionId={this.selectedDateId}
460
+ highlightedOptionId={this.selectedDateId}
461
+ >
462
+ {({
463
+ getRootProps,
464
+ getInputProps,
465
+ getTriggerProps,
466
+ getListProps,
467
+ getOptionProps,
468
+ getDescriptionProps
469
+ }) => (
470
+ <span
471
+ {...getRootProps({ css: styles?.dateInput })}
472
+ ref={(el) => {
473
+ this.ref = el
474
+ }}
475
+ data-cid="DateInput"
476
+ >
477
+ {this.renderInput({ getInputProps, getTriggerProps })}
478
+ <span {...getDescriptionProps()} css={styles?.assistiveText}>
479
+ {assistiveText}
480
+ </span>
481
+ <Popover
482
+ placement={placement}
483
+ isShowingContent={isShowingCalendar}
484
+ positionTarget={this._input}
485
+ shouldReturnFocus={false}
486
+ shouldFocusContentOnTriggerBlur
487
+ >
488
+ {this.renderCalendar({ getListProps, getOptionProps })}
489
+ </Popover>
490
+ </span>
491
+ )}
492
+ </Selectable>
493
+ )
494
+ }
495
+ }
496
+
497
+ export default DateInput
498
+ export { DateInput }