-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
dateInput.tsx
448 lines (399 loc) · 17.2 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
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
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the terms of the LICENSE file distributed with this project.
*/
import classNames from "classnames";
import * as React from "react";
import { DayPickerProps } from "react-day-picker/types/props";
import {
AbstractPureComponent,
DISPLAYNAME_PREFIX,
HTMLInputProps,
IInputGroupProps,
InputGroup,
Intent,
IPopoverProps,
IProps,
Keys,
Popover,
Utils,
} from "@blueprintjs/core";
import * as Classes from "./common/classes";
import { isDateValid, isDayInRange } from "./common/dateUtils";
import { getFormattedDateString, IDateFormatProps } from "./dateFormat";
import { DatePicker } from "./datePicker";
import { getDefaultMaxDate, getDefaultMinDate, IDatePickerBaseProps } from "./datePickerCore";
export interface IDateInputProps extends IDatePickerBaseProps, IDateFormatProps, IProps {
/**
* Allows the user to clear the selection by clicking the currently selected day.
* Passed to `DatePicker` component.
* @default true
*/
canClearSelection?: boolean;
/**
* Whether the calendar popover should close when a date is selected.
* @default true
*/
closeOnSelection?: boolean;
/**
* Props to pass to ReactDayPicker. See API documentation
* [here](http://react-day-picker.js.org/docs/api-daypicker.html).
*
* The following props are managed by the component and cannot be configured:
* `canChangeMonth`, `captionElement`, `fromMonth` (use `minDate`), `month` (use
* `initialMonth`), `toMonth` (use `maxDate`).
*/
dayPickerProps?: DayPickerProps;
/**
* Whether the date input is non-interactive.
* @default false
*/
disabled?: boolean;
/**
* The default date to be used in the component when uncontrolled.
*/
defaultValue?: Date;
/**
* Props to pass to the [input group](#core/components/text-inputs.input-group).
* `disabled` and `value` will be ignored in favor of the top-level props on this component.
* `type` is fixed to "text" and `ref` is not supported; use `inputRef` instead.
*/
inputProps?: HTMLInputProps & IInputGroupProps;
/**
* Called when the user selects a new valid date through the `DatePicker` or by typing
* in the input. The second argument is true if the user clicked on a date in the
* calendar, changed the input value, or cleared the selection; it will be false if the date
* was changed by choosing a new month or year.
*/
onChange?: (selectedDate: Date, isUserChange: boolean) => void;
/**
* Called when the user finishes typing in a new date and the date causes an error state.
* If the date is invalid, `new Date(undefined)` will be returned. If the date is out of range,
* the out of range date will be returned (`onChange` is not called in this case).
*/
onError?: (errorDate: Date) => void;
/**
* Props to pass to the `Popover`.
* Note that `content`, `autoFocus`, and `enforceFocus` cannot be changed.
*/
popoverProps?: Partial<IPopoverProps> & object;
/**
* Element to render on right side of input.
*/
rightElement?: JSX.Element;
/**
* Whether the bottom bar displaying "Today" and "Clear" buttons should be shown below the calendar.
* @default false
*/
showActionsBar?: boolean;
/**
* The currently selected day. If this prop is provided, the component acts in a controlled manner.
* To display no date in the input field, pass `null` to the value prop. To display an invalid date error
* in the input field, pass `new Date(undefined)` to the value prop.
*/
value?: Date | null;
}
export interface IDateInputState {
value: Date;
valueString: string;
isInputFocused: boolean;
isOpen: boolean;
}
export class DateInput extends AbstractPureComponent<IDateInputProps, IDateInputState> {
public static displayName = `${DISPLAYNAME_PREFIX}.DateInput`;
public static defaultProps: Partial<IDateInputProps> = {
closeOnSelection: true,
dayPickerProps: {},
disabled: false,
invalidDateMessage: "Invalid date",
maxDate: getDefaultMaxDate(),
minDate: getDefaultMinDate(),
outOfRangeMessage: "Out of range",
reverseMonthAndYearMenus: false,
};
public state: IDateInputState = {
isInputFocused: false,
isOpen: false,
value: this.props.value !== undefined ? this.props.value : this.props.defaultValue,
valueString: null,
};
private inputEl: HTMLInputElement = null;
private popoverContentEl: HTMLElement = null;
private lastElementInPopover: HTMLElement = null;
private refHandlers = {
input: (el: HTMLInputElement) => {
this.inputEl = el;
},
popoverContent: (el: HTMLElement) => {
this.popoverContentEl = el;
},
};
public componentWillUnmount() {
super.componentWillUnmount();
this.unregisterPopoverBlurHandler();
}
public render() {
const { value, valueString } = this.state;
const dateString = this.state.isInputFocused ? valueString : getFormattedDateString(value, this.props);
const dateValue = isDateValid(value) ? value : null;
const dayPickerProps = {
...this.props.dayPickerProps,
// dom elements for the updated month is not available when
// onMonthChange is called. setTimeout is necessary to wait
// for the updated month to be rendered
onMonthChange: (month: Date) => {
Utils.safeInvoke(this.props.dayPickerProps.onMonthChange, month);
this.setTimeout(this.registerPopoverBlurHandler);
},
};
const wrappedPopoverContent = (
<div ref={this.refHandlers.popoverContent}>
<DatePicker
{...this.props}
dayPickerProps={dayPickerProps}
onChange={this.handleDateChange}
value={dateValue}
/>
</div>
);
// assign default empty object here to prevent mutation
const { popoverProps = {} } = this.props;
const inputProps = this.getInputPropsWithDefaults();
const isErrorState = value != null && (!isDateValid(value) || !this.isDateInRange(value));
return (
<Popover
isOpen={this.state.isOpen && !this.props.disabled}
{...popoverProps}
autoFocus={false}
className={classNames(popoverProps.className, this.props.className)}
content={wrappedPopoverContent}
enforceFocus={false}
onClose={this.handleClosePopover}
popoverClassName={classNames(Classes.DATEINPUT_POPOVER, popoverProps.popoverClassName)}
>
<InputGroup
autoComplete="off"
intent={isErrorState ? Intent.DANGER : Intent.NONE}
placeholder={this.props.placeholder}
rightElement={this.props.rightElement}
{...inputProps}
disabled={this.props.disabled}
onBlur={this.handleInputBlur}
onChange={this.handleInputChange}
onClick={this.handleInputClick}
onFocus={this.handleInputFocus}
onKeyDown={this.handleInputKeyDown}
type="text"
value={dateString}
/>
</Popover>
);
}
public componentWillReceiveProps(nextProps: IDateInputProps) {
super.componentWillReceiveProps(nextProps);
if (nextProps.value !== this.props.value) {
this.setState({ value: nextProps.value });
}
}
private getInputPropsWithDefaults() {
const { inputProps = {} } = this.props;
if (Utils.isFunction(inputProps.inputRef)) {
return {
...inputProps,
inputRef: (el: HTMLInputElement) => {
this.refHandlers.input(el);
inputProps.inputRef(el);
},
};
} else {
return {
...inputProps,
inputRef: this.refHandlers.input,
};
}
}
private isDateInRange(value: Date) {
return isDayInRange(value, [this.props.minDate, this.props.maxDate]);
}
private handleClosePopover = (e?: React.SyntheticEvent<HTMLElement>) => {
const { popoverProps = {} } = this.props;
Utils.safeInvoke(popoverProps.onClose, e);
this.setState({ isOpen: false });
};
private handleDateChange = (newDate: Date | null, isUserChange: boolean, didSubmitWithEnter = false) => {
const prevDate = this.state.value;
// this change handler was triggered by a change in month, day, or (if
// enabled) time. for UX purposes, we want to close the popover only if
// the user explicitly clicked a day within the current month.
const isOpen =
!isUserChange ||
!this.props.closeOnSelection ||
(prevDate != null && (this.hasMonthChanged(prevDate, newDate) || this.hasTimeChanged(prevDate, newDate)));
// if selecting a date via click or Tab, the input will already be
// blurred by now, so sync isInputFocused to false. if selecting via
// Enter, setting isInputFocused to false won't do anything by itself,
// plus we want the field to retain focus anyway.
// (note: spelling out the ternary explicitly reads more clearly.)
const isInputFocused = didSubmitWithEnter ? true : false;
if (this.props.value === undefined) {
const valueString = getFormattedDateString(newDate, this.props);
this.setState({ isInputFocused, isOpen, value: newDate, valueString });
} else {
this.setState({ isInputFocused, isOpen });
}
Utils.safeInvoke(this.props.onChange, newDate, isUserChange);
};
private hasMonthChanged(prevDate: Date | null, nextDate: Date | null) {
return (prevDate == null) !== (nextDate == null) || nextDate.getMonth() !== prevDate.getMonth();
}
private hasTimeChanged(prevDate: Date | null, nextDate: Date | null) {
if (this.props.timePrecision == null) {
return false;
}
return (
(prevDate == null) !== (nextDate == null) ||
nextDate.getHours() !== prevDate.getHours() ||
nextDate.getMinutes() !== prevDate.getMinutes() ||
nextDate.getSeconds() !== prevDate.getSeconds() ||
nextDate.getMilliseconds() !== prevDate.getMilliseconds()
);
}
private handleInputFocus = (e: React.FocusEvent<HTMLInputElement>) => {
const valueString = this.state.value == null ? "" : this.formatDate(this.state.value);
this.setState({ isInputFocused: true, isOpen: true, valueString });
this.safeInvokeInputProp("onFocus", e);
};
private handleInputClick = (e: React.SyntheticEvent<HTMLInputElement>) => {
// stop propagation to the Popover's internal handleTargetClick handler;
// otherwise, the popover will flicker closed as soon as it opens.
e.stopPropagation();
this.safeInvokeInputProp("onClick", e);
};
private handleInputChange = (e: React.SyntheticEvent<HTMLInputElement>) => {
const valueString = (e.target as HTMLInputElement).value;
const value = this.parseDate(valueString);
if (isDateValid(value) && this.isDateInRange(value)) {
if (this.props.value === undefined) {
this.setState({ value, valueString });
} else {
this.setState({ valueString });
}
Utils.safeInvoke(this.props.onChange, value, true);
} else {
if (valueString.length === 0) {
Utils.safeInvoke(this.props.onChange, null, true);
}
this.setState({ valueString });
}
this.safeInvokeInputProp("onChange", e);
};
private handleInputBlur = (e: React.FocusEvent<HTMLInputElement>) => {
const { valueString } = this.state;
const date = this.parseDate(valueString);
if (
valueString.length > 0 &&
valueString !== getFormattedDateString(this.state.value, this.props) &&
(!isDateValid(date) || !this.isDateInRange(date))
) {
if (this.props.value === undefined) {
this.setState({ isInputFocused: false, value: date, valueString: null });
} else {
this.setState({ isInputFocused: false });
}
if (isNaN(date.valueOf())) {
Utils.safeInvoke(this.props.onError, new Date(undefined));
} else if (!this.isDateInRange(date)) {
Utils.safeInvoke(this.props.onError, date);
} else {
Utils.safeInvoke(this.props.onChange, date, true);
}
} else {
if (valueString.length === 0) {
this.setState({ isInputFocused: false, value: null, valueString: null });
} else {
this.setState({ isInputFocused: false });
}
}
this.registerPopoverBlurHandler();
this.safeInvokeInputProp("onBlur", e);
};
private handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.which === Keys.ENTER) {
const nextDate = this.parseDate(this.state.valueString);
this.handleDateChange(nextDate, true, true);
} else if (e.which === Keys.TAB && e.shiftKey) {
// close the popover if focus will move to the previous element on
// the page. tabbing forward should *not* close the popover, because
// focus will be moving into the popover itself.
this.setState({ isOpen: false });
} else if (e.which === Keys.ESCAPE) {
this.setState({ isOpen: false });
this.inputEl.blur();
}
this.safeInvokeInputProp("onKeyDown", e);
};
// focus DOM event listener (not React event)
private handlePopoverBlur = (e: FocusEvent) => {
const relatedTarget = e.relatedTarget as HTMLElement;
if (relatedTarget == null || !this.popoverContentEl.contains(relatedTarget)) {
this.handleClosePopover();
} else if (relatedTarget != null) {
this.unregisterPopoverBlurHandler();
this.lastElementInPopover = relatedTarget;
this.lastElementInPopover.addEventListener("blur", this.handlePopoverBlur);
}
};
private registerPopoverBlurHandler = () => {
if (this.popoverContentEl != null) {
// If current activeElement exists inside popover content, a month
// change has triggered and this element should be lastTabbableElement
let lastTabbableElement = this.popoverContentEl.contains(document.activeElement)
? document.activeElement
: undefined;
// Popover contents are well structured, but the selector will need
// to be updated if more focusable components are added in the future
if (lastTabbableElement == null) {
const tabbableElements = this.popoverContentEl.querySelectorAll(
"input, [tabindex]:not([tabindex='-1'])",
);
const numOfElements = tabbableElements.length;
if (numOfElements > 0) {
// Keep track of the last focusable element in popover and add
// a blur handler, so that when:
// * user tabs to the next element, popover closes
// * focus moves to element within popover, popover stays open
lastTabbableElement = tabbableElements[numOfElements - 1];
}
}
this.unregisterPopoverBlurHandler();
this.lastElementInPopover = lastTabbableElement as HTMLElement;
this.lastElementInPopover.addEventListener("blur", this.handlePopoverBlur);
}
};
private unregisterPopoverBlurHandler = () => {
if (this.lastElementInPopover != null) {
this.lastElementInPopover.removeEventListener("blur", this.handlePopoverBlur);
}
};
/** safe wrapper around invoking input props event handler (prop defaults to undefined) */
private safeInvokeInputProp(name: keyof HTMLInputProps, e: React.SyntheticEvent<HTMLElement>) {
const { inputProps = {} } = this.props;
Utils.safeInvoke(inputProps[name], e);
}
private parseDate(dateString: string): Date | null {
if (dateString === this.props.outOfRangeMessage || dateString === this.props.invalidDateMessage) {
return null;
}
const { locale, parseDate } = this.props;
const newDate = parseDate(dateString, locale);
return newDate === false ? new Date(undefined) : newDate;
}
private formatDate(date: Date): string {
if (!isDateValid(date) || !this.isDateInRange(date)) {
return "";
}
const { locale, formatDate } = this.props;
return formatDate(date, locale);
}
}