-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateInput.tsx
179 lines (156 loc) · 5.37 KB
/
DateInput.tsx
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { useRef } from 'react'
import moment from 'moment-timezone'
import DatePicker from 'react-datepicker'
import Popper from '@popperjs/core'
import {
getGuid,
Validator,
UseValidationProps,
useControlledValue,
useValidation,
} from '@interface-technologies/iti-react-core'
import { getValidationClass, ValidationFeedback } from '../../validation'
import { getInvalidFeedback, getTimeZone, timeFormat } from './dateInputUtil'
// date-fns format strings (used by react-datepicker)
const fnsDateInputFormat = 'M/d/yyyy'
const fnsTimeFormat = 'h:mm a'
const fnsDateTimeInputFormat = `${fnsDateInputFormat} ${fnsTimeFormat}`
/** This moment doesn't have to be a specific time zone. UTC is fine, for example. */
export type DateInputValue = moment.Moment | null
/**
* @internal
*/
export function convertJsDateToTimeZone(date: Date, timeZone: string): Date {
const str = date.toLocaleString('en-US', { timeZone })
return new Date(str)
}
/**
* @internal
*
* The offset of `date` must correspond to the browser's time zone for this to work.
*
* Example:
* - assuming DST is in effect
* - date = 9:00 am, UTC-4 = 1:00 pm UTC
* - timeZone = America/Los_Angeles
* - return value = 9:00 am, UTC -7 = 4:00 pm UTC
*/
export function parseJsDateIgnoringTimeZone(date: Date, timeZone: string): moment.Moment {
const str = date.toLocaleString('en-US')
return moment.tz(str, 'M/D/YYYY, h:mm:ss A', timeZone)
}
//
// Validators
//
function formatValidator(includesTime: boolean): Validator<DateInputValue> {
return (value) => {
if (value && !value.isValid()) return getInvalidFeedback(includesTime)
return undefined
}
}
function required(options: { includesTime: boolean }): Validator<DateInputValue> {
return (value) => {
if (!value) return getInvalidFeedback(options.includesTime)
return undefined
}
}
/** Validators for use with [[`DateInput`]]. */
export const DateValidators = {
required,
}
export interface DateInputProps extends UseValidationProps<DateInputValue> {
id?: string
placeholder?: string
className?: string
/**
* This class name will be used **in addition to** `form-control` and the
* validation feedback class.
*/
inputClassName?: string
popperPlacement?: Popper.Placement
includesTime?: boolean
timeIntervals?: number
filterDate?(date: Date): boolean
enabled?: boolean
readOnly?: boolean
/** Pass `'local'` to use `moment.tz.guess()` as the time zone */
timeZone: string
}
/**
* A datetime input with a datepicker popover. See also [[`DateInputNoPicker`]].
*
* It is implemented using `react-datepicker`.
*/
export const DateInput = React.memo<DateInputProps>(
({
placeholder,
includesTime = false,
popperPlacement,
timeIntervals,
enabled = true,
readOnly = false,
filterDate,
showValidation,
name,
className,
...otherProps
}) => {
const timeZone = getTimeZone(otherProps.timeZone)
const idGuidRef = useRef(getGuid())
const id = otherProps.id ?? idGuidRef.current
const { value, onChange } = useControlledValue<DateInputValue>({
value: otherProps.value,
onChange: otherProps.onChange,
defaultValue: otherProps.defaultValue,
fallbackValue: null,
})
const validatorOutput = useValidation<DateInputValue>({
value,
name,
onValidChange: otherProps.onValidChange,
validators: [formatValidator(includesTime), ...otherProps.validators],
validationKey: otherProps.validationKey,
asyncValidator: otherProps.asyncValidator,
onAsyncError: otherProps.onAsyncError,
formLevelValidatorOutput: otherProps.formLevelValidatorOutput,
})
const fnsFormat = includesTime ? fnsDateTimeInputFormat : fnsDateInputFormat
function datePickerOnChange(date: Date | null): void {
const d = date ? parseJsDateIgnoringTimeZone(date, timeZone) : null
onChange(d)
}
const classes = [
'form-control',
getValidationClass(!validatorOutput, showValidation),
]
if (otherProps.inputClassName) classes.push(otherProps.inputClassName)
return (
<ValidationFeedback
validatorOutput={validatorOutput}
showValidation={showValidation}
className={className}
>
<DatePicker
id={id}
name={name}
selected={
value ? convertJsDateToTimeZone(value.toDate(), timeZone) : null
}
onChange={datePickerOnChange}
className={classes.join(' ')}
dateFormat={fnsFormat}
placeholderText={placeholder}
popperPlacement={popperPlacement}
disabledKeyboardNavigation
showTimeSelect={includesTime}
timeIntervals={timeIntervals}
timeFormat={timeFormat}
filterDate={filterDate}
disabled={!enabled}
readOnly={readOnly}
autoComplete="off"
/>
</ValidationFeedback>
)
}
)