-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathDatePicker.tsx
More file actions
142 lines (126 loc) · 4.24 KB
/
DatePicker.tsx
File metadata and controls
142 lines (126 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use client'
import type { DatePickerProps } from 'react-datepicker'
import React from 'react'
import ReactDatePickerDefaultImport, { registerLocale, setDefaultLocale } from 'react-datepicker'
const ReactDatePicker =
'default' in ReactDatePickerDefaultImport
? ReactDatePickerDefaultImport.default
: ReactDatePickerDefaultImport
import type { Props } from './types.js'
import { CalendarIcon } from '../../icons/Calendar/index.js'
import { useTranslation } from '../../providers/Translation/index.js'
import './index.css'
import { getFormattedLocale } from './getFormattedLocale.js'
const baseClass = 'date-time-picker'
const DatePicker: React.FC<Props> = (props) => {
const {
id,
displayFormat: customDisplayFormat,
maxDate,
maxTime,
minDate,
minTime,
monthsToShow = 1,
onChange: onChangeFromProps,
overrides,
pickerAppearance = 'default',
placeholder: placeholderText,
readOnly,
timeFormat = 'h:mm aa',
timeIntervals = 30,
value,
} = props
// Use the user's AdminUI language preference for the locale
const { i18n } = useTranslation()
let dateFormat = customDisplayFormat
if (!customDisplayFormat) {
// when no displayFormat is provided, determine format based on the picker appearance
if (pickerAppearance === 'default') {
dateFormat = 'MM/dd/yyyy'
} else if (pickerAppearance === 'dayAndTime') {
dateFormat = 'MMM d, yyy h:mm a'
} else if (pickerAppearance === 'timeOnly') {
dateFormat = 'h:mm a'
} else if (pickerAppearance === 'dayOnly') {
dateFormat = 'MMM dd'
} else if (pickerAppearance === 'monthOnly') {
dateFormat = 'MMMM'
}
}
const onChange: Extract<
DatePickerProps,
{ selectsMultiple?: never; selectsRange?: never }
>['onChange'] = (incomingDate) => {
const newDate = incomingDate
if (newDate instanceof Date && ['dayOnly', 'default', 'monthOnly'].includes(pickerAppearance)) {
const tzOffset = incomingDate.getTimezoneOffset() / 60
newDate.setHours(12 - tzOffset, 0)
}
if (newDate instanceof Date && !dateFormat.includes('SSS')) {
// Unless the dateFormat includes milliseconds, set milliseconds to 0
// This is to ensure that the timestamp is consistent with the displayFormat
newDate.setMilliseconds(0)
}
if (typeof onChangeFromProps === 'function') {
onChangeFromProps(newDate)
}
}
const dateTimePickerProps: Extract<
DatePickerProps,
{ selectsMultiple?: never; selectsRange?: never }
> = {
customInputRef: 'ref',
dateFormat,
disabled: readOnly,
maxDate,
maxTime,
minDate,
minTime,
monthsShown: Math.min(2, monthsToShow),
onChange,
placeholderText,
popperPlacement: 'bottom-start',
selected: value && new Date(value),
showMonthYearPicker: pickerAppearance === 'monthOnly',
showPopperArrow: false,
showTimeSelect: pickerAppearance === 'dayAndTime' || pickerAppearance === 'timeOnly',
showTimeSelectOnly: pickerAppearance === 'timeOnly',
timeFormat,
timeIntervals,
...(overrides as Extract<
DatePickerProps,
{ selectsMultiple?: never; selectsRange?: never } // to satisfy TypeScript. Overrides can enable selectsMultiple or selectsRange but then it's up to the user to ensure they pass in the correct onChange
>),
}
const classes = [baseClass, `${baseClass}__appearance--${pickerAppearance}`]
.filter(Boolean)
.join(' ')
React.useEffect(() => {
if (i18n.dateFNS) {
try {
const datepickerLocale = getFormattedLocale(i18n.language)
registerLocale(datepickerLocale, i18n.dateFNS)
setDefaultLocale(datepickerLocale)
} catch (e) {
// eslint-disable-next-line no-console
console.warn(`Could not find DatePicker locale for ${i18n.language}`)
}
}
}, [i18n.language, i18n.dateFNS])
return (
<div className={classes} id={id}>
<div className={`${baseClass}__input-wrapper`}>
<ReactDatePicker
{...dateTimePickerProps}
dropdownMode="select"
showMonthDropdown
showYearDropdown
/>
{/* TODO: update icon */}
<CalendarIcon />
</div>
</div>
)
}
// eslint-disable-next-line no-restricted-exports
export default DatePicker