-
Notifications
You must be signed in to change notification settings - Fork 32
/
date-textbox.tsx
227 lines (199 loc) · 7.79 KB
/
date-textbox.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import React, { ChangeEvent, FC, FocusEvent, MouseEvent, useEffect, useRef, useState } from 'react'
import Expander from 'makeup-expander'
import EbayCalendar, { EbayCalendarProps } from '../ebay-calendar/calendar'
import { EbayTextbox, EbayTextboxPostfixIcon } from '../ebay-textbox'
import { DayISO, dateArgToISO, toISO } from '../ebay-calendar/date-utils'
import { EbayChangeEventHandler, EbayFocusEventHandler, EbayMouseEventHandler } from '../common/event-utils/types'
import { isControlled } from '../ebay-textbox/textbox'
import classNames from 'classnames'
type EventData = {
selected?: string;
rangeStart?: string;
rangeEnd?: string;
}
export type EbayDateTextboxProps = Omit<EbayCalendarProps, 'interactive' | 'navigable' | 'numMonths' | 'selected'> & {
className?: string;
value?: string;
rangeEnd?: string;
defaultValue?: string;
defaultRangeEnd?: string;
range?: boolean;
collapseOnSelect?: boolean;
inputPlaceholderText?: string | string[];
a11yOpenPopoverText?: string;
onChange?: EbayChangeEventHandler<HTMLInputElement, EventData>
& EbayMouseEventHandler<HTMLInputElement, EventData>
& EbayFocusEventHandler<HTMLInputElement, EventData>;
onInputChange?: EbayChangeEventHandler<HTMLInputElement>;
onInputRangeEndChange?: EbayChangeEventHandler<HTMLInputElement>;
}
const MIN_WIDTH_FOR_DOUBLE_PANE = 600
const EbayDateTextbox: FC<EbayDateTextboxProps> = ({
className,
inputPlaceholderText = 'YYYY-MM-DD',
a11yOpenPopoverText = 'open calendar',
range,
value: controlledValue,
rangeEnd: controlledRangeEnd,
defaultValue,
defaultRangeEnd,
collapseOnSelect,
onChange = () => {},
onInputChange = () => {},
onInputRangeEndChange = () => {},
...rest
}) => {
const expander = useRef<typeof Expander>()
const containerRef = useRef<HTMLSpanElement>(null)
const [internalValue, setInternalValue] = useState<string>(defaultValue || '')
const [internalRangeEnd, setInternalRangeEnd] = useState<string>(defaultRangeEnd || '')
const valueToRender = isControlled(controlledValue) ? controlledValue : internalValue
const rangeEndToRender = isControlled(controlledRangeEnd) ? controlledRangeEnd : internalRangeEnd
const [firstSelected, setFirstSelected] = useState(() =>
dateArgToISO(valueToRender)
)
const [secondSelected, setSecondSelected] = useState(() =>
dateArgToISO(rangeEndToRender)
)
const [isPopoverOpen, setIsPopoverOpen] = useState<boolean>(false)
const [numMonths, setNumMonths] = useState(1)
const openPopover = () => {
setIsPopoverOpen(true)
}
const closePopover = () => {
setIsPopoverOpen(false)
}
useEffect(() => {
if (!containerRef.current) {
return
}
expander.current = new Expander(containerRef.current, {
hostSelector: '.ebay-date-textbox--main > .icon-btn',
contentSelector: '.date-textbox__popover',
expandOnClick: true,
autoCollapse: true
})
containerRef.current.addEventListener('expander-expand', openPopover)
containerRef.current.addEventListener('expander-collapse', closePopover)
const calculateNumMonths = () => {
setNumMonths(document.documentElement.clientWidth < MIN_WIDTH_FOR_DOUBLE_PANE ? 1 : 2)
}
calculateNumMonths()
window.addEventListener('resize', calculateNumMonths)
return () => {
expander.current?.destroy()
window.removeEventListener('resize', calculateNumMonths)
}
}, [])
const handleInputChange = (event: FocusEvent<HTMLInputElement>, index: number) => {
const date = new Date(event.target.value)
const iso = isNaN(date.getTime()) ? null : toISO(date)
if (index === 0) {
setFirstSelected(iso)
setInternalValue(iso || '')
} else {
setSecondSelected(iso)
setInternalRangeEnd(iso || '')
}
if (range) {
onChange(event, {
rangeStart: index === 0 ? iso : firstSelected,
rangeEnd: index === 1 ? iso : secondSelected
})
} else {
onChange(event, {
selected: iso
})
}
}
const handlePopoverSelect = (
event: MouseEvent<HTMLInputElement>,
{ iso }: { iso: DayISO }
) => {
setFirstSelected(iso)
setInternalValue(iso)
if (range) {
const selected = firstSelected || secondSelected
const eventData: EventData = {
rangeStart: iso,
rangeEnd: selected
}
if (firstSelected && secondSelected) {
// both were selected reset selection
setInternalRangeEnd('')
setSecondSelected(null)
eventData.rangeEnd = null
} else if (selected) {
// exactly one was selected; fiture out the order
if (selected < iso) {
setFirstSelected(selected)
setInternalValue(selected)
setInternalRangeEnd(iso)
setSecondSelected(iso)
eventData.rangeStart = selected
eventData.rangeEnd = iso
} else {
setFirstSelected(iso)
setInternalValue(iso)
setInternalRangeEnd(selected)
setSecondSelected(selected)
eventData.rangeStart = iso
eventData.rangeEnd = selected
}
}
onChange(event, eventData)
} else {
onChange(event, {
selected: iso
})
}
if (collapseOnSelect) {
expander.current.expanded = false
}
}
const handleInternalChange = (event: ChangeEvent<HTMLInputElement>, index: number) => {
if (index === 0) {
setInternalValue(event.target.value)
onInputChange(event)
} else {
setInternalRangeEnd(event.target.value)
onInputRangeEndChange(event)
}
}
const [rangeStartPlaceholder, mainPlaceholder] = Array.isArray(inputPlaceholderText)
? inputPlaceholderText
: [inputPlaceholderText, inputPlaceholderText]
return (
<span className={classNames('date-textbox', className)} ref={containerRef}>
{range && (
<EbayTextbox
value={valueToRender}
placeholder={rangeStartPlaceholder}
onInputChange={(event) => handleInternalChange(event, 0)}
onBlur={(event) => handleInputChange(event, 0)}
/>
)}
<EbayTextbox
className="ebay-date-textbox--main"
placeholder={mainPlaceholder}
value={range ? rangeEndToRender : valueToRender}
onInputChange={(event) => handleInternalChange(event, range ? 1 : 0)}
onBlur={(event) => handleInputChange(event, range ? 1 : 0)}>
<EbayTextboxPostfixIcon name="calendar24" buttonAriaLabel={a11yOpenPopoverText} />
</EbayTextbox>
<div hidden={!isPopoverOpen} className="date-textbox__popover">
<EbayCalendar
{...rest}
range={range}
interactive
navigable
numMonths={numMonths}
selected={firstSelected && secondSelected
? [firstSelected, secondSelected]
: firstSelected || secondSelected || undefined}
onSelect={handlePopoverSelect} />
</div>
</span>
)
}
export default EbayDateTextbox