forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatepicker.js
543 lines (496 loc) · 16.7 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import React, { useState, useRef, useEffect } from 'react';
import { FormattedMessage, injectIntl } from 'react-intl';
import PropTypes from 'prop-types';
import moment from 'moment-timezone';
import contains from 'dom-helpers/query/contains';
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';
const pickDataProps = (props) => pick(props, (v, key) => key.indexOf('data-test') !== -1);
export function getMomentLocalizedFormat(intl) {
moment.locale(intl.locale);
const format = moment.localeData()._longDateFormat.L;
return format;
}
// Returns a localized format.
export const getLocaleDateFormat = ({ intl }) => {
const tempDate = new Date('Thu May 14 2020 14:39:25 GMT-0500');
let format = '';
// set up a locally formatted array of parts...
if (Intl?.DateTimeFormat()?.formatToParts) {
const intlFormatter = new Intl.DateTimeFormat(intl.locale, {
day: '2-digit',
year: 'numeric',
month: '2-digit'
});
const formatted = intlFormatter.formatToParts(tempDate);
// pull the localized format from the array of formatted parts.
formatted.forEach((p) => {
switch (p.type) {
case 'month':
format += 'MM';
break;
case 'day':
format += 'DD';
break;
case 'year':
format += 'YYYY';
break;
case 'literal':
format += p.value;
break;
default:
break;
}
});
} else {
format = getMomentLocalizedFormat(intl);
}
return format;
};
// Controls the formatting from the value prop to what displays in the UI.
// need to judge the breakage factor in adopting a spread syntax for these parameters...
const defaultParser = (value, timeZone, uiFormat, outputFormats) => {
if (!value || value === '') { return value; }
const offsetRegex = /T[\d.:]+[+-][\d]+$/;
const offsetRE2 = /T[\d:]+[-+][\d:]+\d{2}$/; // sans milliseconds
let inputMoment;
// if date string contains a utc offset, we can parse it as utc time and convert it to selected timezone.
if (offsetRegex.test(value) || offsetRE2.test(value)) {
inputMoment = moment.tz(value, timeZone);
} else {
inputMoment = moment.tz(value, [uiFormat, ...outputFormats], timeZone);
}
const inputValue = inputMoment.format(uiFormat);
return inputValue;
};
/**
* defaultOutputFormatter
* Controls the formatting from the value prop/input to what is relayed in the onChange event.
* This function has two responsibilities:
* 1. use `backendDateStandard` to format `value`
* 2. convert value to Arabic/Latn digits (0-9)
*
* The first responsibility is pretty obvious, but the second one is subtle,
* implied but never clearly stated in API documentation. Dates are passed
* as strings in API requests and are then interpreted by the backend as Dates.
* To be so interpretable, they must conform to the expected formatted AND use
* the expected numeral system.
*
* This function allows the format to be changed with `backendDateStandard`.
* To change the numeral system, pass a function as `outputFormatter`, which
* gives you control over both the format and the numeral system.
*
* @returns {string} 7-bit ASCII
*/
export const defaultOutputFormatter = ({ backendDateStandard, value, uiFormat, outputFormats, timeZone }) => {
if (!value || value === '') { return value; }
const parsed = new moment.tz(value, [uiFormat, ...outputFormats], timeZone); // eslint-disable-line
if (/8601/.test(backendDateStandard)) {
return parsed.toISOString();
}
// Use `.locale('en')` before `.format(...)` to get Arabic/"Latn" numerals.
// otherwise, a locale like ar-SA or any locale with a "-u-nu-..." subtag
// can give us non-Arabic (non-"Latn") numerals, and in such a locale the
// formatter "YYYY-MM-DD" can give us output like this: ١٦/٠٧/٢٠٢١
// i.e. we get year-month-day but in non-Arabic numerals.
//
// Additional details about numbering systems at
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/numberingSystem
// and about how the locale string may be parsed at
// https://www.rfc-editor.org/rfc/rfc5646.html
// for support of the RFC2822 format (rare thus far and support may soon be deprecated.)
if (/2822/.test(backendDateStandard)) {
const DATE_RFC2822 = 'ddd, DD MMM YYYY HH:mm:ss ZZ';
return parsed.locale('en').format(DATE_RFC2822);
}
// if a localized string dateformat has been passed, normalize the date first...
// otherwise, localized strings could be submitted to the backend.
const normalizedDate = moment.utc(value, [uiFormat, ...outputFormats]);
return new moment(normalizedDate, 'YYYY-MM-DD').locale('en').format(backendDateStandard); // eslint-disable-line
};
const propTypes = {
autoFocus: PropTypes.bool,
backendDateStandard: PropTypes.string,
date: PropTypes.object,
dateFormat: PropTypes.string,
disabled: PropTypes.bool,
exclude: PropTypes.func,
hideOnChoose: PropTypes.bool,
id: PropTypes.string,
input: PropTypes.object,
inputRef: PropTypes.oneOfType([PropTypes.object, 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 getBackendDateStandard = (standard, use) => {
if (!use) return undefined;
if (standard === 'ISO8601') return ['YYYY-MM-DDTHH:mm:ss.sssZ', 'YYYY-MM-DDTHH:mm:ssZ'];
if (standard === 'RFC2822') return ['ddd, DD MMM YYYY HH:mm:ss ZZ'];
return [standard, 'YYYY-MM-DDTHH:mm:ss.sssZ', 'ddd, DD MMM YYYY HH:mm:ss ZZ'];
};
const defaultProps = {
autoFocus: false,
backendDateStandard: 'ISO8601',
hideOnChoose: true,
modifiers: {},
outputFormatter: defaultOutputFormatter,
outputBackendValue: true,
parser: defaultParser,
placement: 'bottom',
screenReaderMessage: '',
useFocus: true,
};
const Datepicker = (
{ backendDateStandard, // eslint-disable-line no-unused-vars
disabled,
dateFormat,
exclude,
hideOnChoose,
id,
intl,
locale,
modifiers,
onBlur,
onChange,
onFocus,
outputBackendValue,
outputFormatter,
parser,
placement,
readOnly,
screenReaderMessage, // eslint-disable-line no-unused-vars
showCalendar: showCalendarProp,
timeZone: timeZoneProp, // eslint-disable-line no-unused-vars
useFocus, // eslint-disable-line no-unused-vars
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;
}
// use strict mode to check validity - incomplete dates, anything not conforming to the format will be invalid
const backendStandard = getBackendDateStandard(backendDateStandard, outputBackendValue);
const valueMoment = new moment(// eslint-disable-line new-cap
value,
[format, ...backendStandard], // pass array of possible formats ()
true
);
const isValid = valueMoment.isValid();
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) { // if the date's not valid, we just update the datestring...
dates = {
dateString: value,
};
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 (contains(container.current, document.activeElement) &&
document.activeElement !== document.body) {
if (pickerRef.current) {
return (contains(pickerRef.current, 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 (!contains(container.current, e.target) || !contains(pickerRef.current, 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}`}
tabIndex="-1"
icon="times-circle-solid"
/>
)}
</FormattedMessage>
)}
<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;
Datepicker.defaultProps = defaultProps;
export default formField(
injectIntl(Datepicker),
({ input, meta }) => ({
onBlur: input?.onBlur,
onFocus: input?.onFocus,
error: meta?.touched ? meta.error : undefined,
useInput: true
})
);