-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathDatepicker.js
412 lines (383 loc) · 11.9 KB
/
Datepicker.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import React, { useState, useRef, useEffect } from 'react';
import { FormattedMessage, injectIntl } from 'react-intl';
import PropTypes from 'prop-types';
import uniqueId from 'lodash/uniqueId';
import pick from 'lodash/pick';
import RootCloseWrapper from '../../util/RootCloseWrapper';
import nativeChangeField from '../../util/nativeChangeFieldValue';
import formField from '../FormField';
import Popper, { AVAILABLE_PLACEMENTS } from '../Popper';
import IconButton from '../IconButton';
import TextField from '../TextField';
import Calendar from './Calendar';
import css from './Calendar.css';
import {
defaultParser,
defaultInputValidator,
defaultOutputFormatter,
getBackendDateStandard
} from './datepicker-util';
import { getLocaleDateFormat } from '../../util/dateTimeUtils';
const pickDataProps = (props) => pick(props, (v, key) => key.indexOf('data-test') !== -1);
const propTypes = {
autoFocus: PropTypes.bool,
backendDateStandard: PropTypes.string,
date: PropTypes.object,
dateFormat: PropTypes.string,
disabled: PropTypes.bool,
exclude: PropTypes.func,
hideCalendarButton: PropTypes.bool,
hideOnChoose: PropTypes.bool,
id: PropTypes.string,
input: PropTypes.object,
inputRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
inputValidator: PropTypes.func,
intl: PropTypes.object,
label: PropTypes.node,
locale: PropTypes.string,
meta: PropTypes.object,
modifiers: PropTypes.object,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
onSetDate: PropTypes.func,
outputBackendValue: PropTypes.bool,
outputFormatter: PropTypes.func,
parser: PropTypes.func,
placement: PropTypes.oneOf(AVAILABLE_PLACEMENTS),
readOnly: PropTypes.bool,
required: PropTypes.bool,
screenReaderMessage: PropTypes.string,
showCalendar: PropTypes.bool,
timeZone: PropTypes.string,
useFocus: PropTypes.bool,
useInput: PropTypes.bool,
usePortal: PropTypes.bool,
value: PropTypes.string,
};
const Datepicker = (
{ autoFocus = false,
backendDateStandard = 'ISO8601',
disabled,
dateFormat,
exclude,
hideCalendarButton = false,
hideOnChoose = true,
id,
inputValidator = defaultInputValidator,
intl,
locale,
modifiers = {},
onBlur,
onChange,
onFocus,
outputBackendValue = true,
outputFormatter = defaultOutputFormatter,
parser = defaultParser,
placement = 'bottom',
readOnly,
screenReaderMessage = '',
showCalendar: showCalendarProp,
timeZone: timeZoneProp, // eslint-disable-line no-unused-vars
useFocus = true,
useInput,
usePortal,
value: valueProp,
inputRef,
...props }
) => {
const format = useRef(
dateFormat || getLocaleDateFormat({ intlLocale: intl.locale, localeProp: locale, intl })
).current;
const [datePair, updateDatePair] = useState({
dateString: typeof valueProp !== 'undefined' ? parser(
valueProp, // value
timeZoneProp || intl.timeZone, // timezone
format, // uiFormat
getBackendDateStandard(backendDateStandard, true) // outputFormats
) : null,
formatted: typeof valueProp !== 'undefined' ? outputFormatter({
backendDateStandard,
value: valueProp,
timeZone: timeZoneProp || intl.timeZone,
uiFormat: format,
outputFormats: getBackendDateStandard(backendDateStandard, true)
}) : null
});
// since updating the Datepair object isn't quite enough to prompt a re-render when its only partially
// updated, need to maintain a 2nd field containing only the displayed value.
// this resolves issue with the clearIcon not showing up.
const [displayedValue, updateDisplayed] = useState(datePair.dateString);
const [showCalendar, setShowCalendar] = useState(showCalendarProp);
const input = useRef(null);
const pickerRef = useRef(null);
const blurTimeout = useRef(null);
const hiddenInput = useRef(null);
const testId = useRef(id || uniqueId('dp-')).current;
const calendarFirstField = useRef(null);
const container = useRef(null);
const payload = useRef(datePair);
let maybeUpdateValue;
// handle value changes that originate outside of the component.
useEffect(() => {
if (typeof valueProp !== 'undefined' && valueProp !== datePair.dateString && valueProp !== datePair.formatted) {
payload.current = Object.assign(payload.current, maybeUpdateValue(valueProp));
nativeChangeField(input, false, payload.current.dateString);
}
}, [valueProp, maybeUpdateValue, datePair.dateString, datePair.formatted]);
maybeUpdateValue = (value) => {
if (value === '') {
const blankDates = {
dateString: '',
formatted: ''
};
updateDatePair(blankDates);
updateDisplayed('');
return blankDates;
}
// if we output the value according to backendDateStandard, we will probably get it back
// in that format, so include that format in validation.
const backendStandard = getBackendDateStandard(backendDateStandard, outputBackendValue);
const isValid = inputValidator({ value, format, backendStandard });
let dates;
// otherwise parse the value and update the datestring and the formatted date...
if (isValid) {
const parsed = parser(
value,
timeZoneProp || intl.timeZone,
format, // uiFormat
backendStandard, // outputFormat
);
if (parsed !== datePair.dateString) {
const hiddenDate = outputFormatter({
backendDateStandard,
value: parsed,
uiFormat: format,
outputFormats: backendStandard,
timeZone: timeZoneProp || intl.timeZone
});
dates = { dateString: parsed, formatted: hiddenDate };
updateDatePair(current => {
const newDatePair = Object.assign(current, dates);
return newDatePair;
});
updateDisplayed(dates.dateString);
return dates;
}
return {};
} else if (value !== datePair.dateString) {
dates = {
dateString: value,
formatted: ''
};
updateDatePair(current => {
const newDatePair = Object.assign(current, dates);
return newDatePair;
});
updateDisplayed(dates.dateString);
return dates;
}
return {};
};
const setFromCalendar = (value) => {
nativeChangeField(input, hideOnChoose, value);
if (hideOnChoose) {
setShowCalendar(false);
}
};
// for vanilla react/non-final-form implementations that just get the input value.
const internalHandleChange = (e) => {
payload.current = Object.assign(payload.current, maybeUpdateValue(e.target.value));
if ((!useInput || !outputBackendValue) && onChange) {
onChange(e, e.target.value, payload.current.formatted);
} else if (payload.current.formatted !== hiddenInput.current.value) {
nativeChangeField(hiddenInput, false, payload.current.formatted);
}
};
// for final-form so it can have a native change event rather than a fabricated thing...
const onChangeFormatted = (e) => {
if (useInput && outputBackendValue && onChange) {
const { dateString, formatted } = payload.current;
onChange(e, formatted, dateString);
}
};
const datePickerIsFocused = () => {
if (container.current.contains(document.activeElement) &&
document.activeElement !== document.body) {
if (pickerRef.current) {
return (pickerRef.current.contains(document.activeElement));
}
return true;
}
return false;
};
const internalClearDate = () => {
updateDatePair({ dateString: '', formatted: '' });
nativeChangeField(input, true, '');
};
const toggleCalendar = () => {
setShowCalendar(cur => !cur);
};
const queueBlur = (e) => {
blurTimeout.current = setTimeout(() => {
if (onBlur) {
if (useInput) {
onBlur({
target: outputBackendValue ? hiddenInput.current : input.current,
stopPropagation: () => {},
preventDefault: () => {},
defaultPrevented: true,
});
} else {
onBlur(e);
}
}
});
};
const cancelBlur = () => {
clearTimeout(blurTimeout.current);
};
const handleInternalBlur = (e) => {
e.preventDefault();
queueBlur(e);
};
const handleInternalFocus = (e) => {
cancelBlur();
if (onFocus) {
onFocus(e);
}
};
const handleRootClose = (e) => {
if (!container.current.contains(e.target) || !pickerRef.current.contains(e.target)) {
if (!datePickerIsFocused()) {
setShowCalendar(false);
}
}
};
const handleRequestClose = () => {
input.current?.focus(); // eslint-disable-line no-unused-expressions
setShowCalendar(false);
};
const renderCalendar = () => (
<RootCloseWrapper
onRootClose={handleRootClose}
ref={pickerRef}
>
<Calendar
onSetDate={setFromCalendar}
selectedDate={datePair.dateString}
dateFormat={format}
firstFieldRef={calendarFirstField}
onFocus={handleInternalFocus}
onRequestClose={handleRequestClose}
rootRef={pickerRef}
locale={locale || intl.locale}
exclude={exclude}
id={testId}
/>
</RootCloseWrapper>
);
// renders clear button and calendar button
const renderEndElement = () => {
if (readOnly || disabled) return null;
return (
<>
{ displayedValue && (
<FormattedMessage id="stripes-components.clearFieldValue">
{([ariaLabel]) => (
<IconButton
data-test-clear
key="clearButton"
onClick={internalClearDate}
aria-label={ariaLabel}
id={`datepicker-clear-button-${testId}`}
icon="times-circle-solid"
/>
)}
</FormattedMessage>
)}
{!hideCalendarButton && (
<FormattedMessage id="stripes-components.showOrHideDatepicker">
{([ariaLabel]) => (
<IconButton
data-test-calendar-button
onClick={toggleCalendar}
aria-label={ariaLabel}
aria-haspopup="true"
aria-expanded={!!showCalendar}
id={`datepicker-toggle-calendar-button-${testId}`}
icon="calendar"
/>
)}
</FormattedMessage>
)}
</>
);
};
const content = (
<div
className={css.container}
ref={container}
data-test-datepicker-container
onFocus={handleInternalFocus}
onBlur={handleInternalBlur}
>
<TextField
{...props}
id={testId}
readOnly={readOnly}
disabled={disabled}
value={datePair.dateString}
onChange={internalHandleChange}
endControl={renderEndElement()}
hasClearIcon={false}
inputRef={element => {
input.current = element;
if (typeof inputRef === 'object') inputRef.current = element;
if (typeof inputRef === 'function') inputRef(element);
}}
placeholder={format}
/>
<input
data-test-datepicker-hidden-input
type="text"
hidden
value={datePair.formatted}
onChange={onChangeFormatted}
ref={hiddenInput}
/>
</div>
);
const portalElem = usePortal ? document.getElementById('OverlayContainer') : null;
return (
<div className={css.container} {...pickDataProps(props)}>
{content}
<Popper
placement={placement}
isOpen={showCalendar}
anchorRef={container}
onToggle={toggleCalendar}
portal={usePortal && portalElem}
modifiers={{
offset: {
enabled: true,
offset: '0,10',
},
...modifiers
}}
>
{renderCalendar()}
</Popper>
</div>
);
};
Datepicker.propTypes = propTypes;
export default formField(
injectIntl(Datepicker),
({ input, meta }) => ({
onBlur: input?.onBlur,
onFocus: input?.onFocus,
error: meta?.touched ? meta.error : undefined,
useInput: true
})
);