UNPKG

16.7 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DateInput = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var core_1 = require("@blueprintjs/core");
23var Classes = tslib_1.__importStar(require("./common/classes"));
24var dateUtils_1 = require("./common/dateUtils");
25var dateFormat_1 = require("./dateFormat");
26var datePicker_1 = require("./datePicker");
27var datePickerCore_1 = require("./datePickerCore");
28var DateInput = /** @class */ (function (_super) {
29 tslib_1.__extends(DateInput, _super);
30 function DateInput() {
31 var _this = this;
32 var _a;
33 _this = _super.apply(this, arguments) || this;
34 _this.state = {
35 isInputFocused: false,
36 isOpen: false,
37 value: _this.props.value !== undefined ? _this.props.value : _this.props.defaultValue,
38 valueString: null,
39 };
40 _this.inputElement = null;
41 _this.popoverContentElement = null;
42 _this.handleInputRef = (0, core_1.refHandler)(_this, "inputElement", (_a = _this.props.inputProps) === null || _a === void 0 ? void 0 : _a.inputRef);
43 _this.handlePopoverContentRef = (0, core_1.refHandler)(_this, "popoverContentElement");
44 _this.handleClosePopover = function (e) {
45 var _a;
46 var _b = _this.props.popoverProps, popoverProps = _b === void 0 ? {} : _b;
47 (_a = popoverProps.onClose) === null || _a === void 0 ? void 0 : _a.call(popoverProps, e);
48 _this.setState({ isOpen: false });
49 };
50 _this.handleDateChange = function (newDate, isUserChange, didSubmitWithEnter) {
51 var _a, _b;
52 if (didSubmitWithEnter === void 0) { didSubmitWithEnter = false; }
53 var prevDate = _this.state.value;
54 // this change handler was triggered by a change in month, day, or (if
55 // enabled) time. for UX purposes, we want to close the popover only if
56 // the user explicitly clicked a day within the current month.
57 var isOpen = !isUserChange ||
58 !_this.props.closeOnSelection ||
59 (prevDate != null && (_this.hasMonthChanged(prevDate, newDate) || _this.hasTimeChanged(prevDate, newDate)));
60 // if selecting a date via click or Tab, the input will already be
61 // blurred by now, so sync isInputFocused to false. if selecting via
62 // Enter, setting isInputFocused to false won't do anything by itself,
63 // plus we want the field to retain focus anyway.
64 // (note: spelling out the ternary explicitly reads more clearly.)
65 var isInputFocused = didSubmitWithEnter ? true : false;
66 if (_this.props.value === undefined) {
67 var valueString = (0, dateFormat_1.getFormattedDateString)(newDate, _this.props);
68 _this.setState({ isInputFocused: isInputFocused, isOpen: isOpen, value: newDate, valueString: valueString });
69 }
70 else {
71 _this.setState({ isInputFocused: isInputFocused, isOpen: isOpen });
72 }
73 (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, newDate, isUserChange);
74 };
75 _this.handleInputFocus = function (e) {
76 var valueString = _this.state.value == null ? "" : _this.formatDate(_this.state.value);
77 _this.setState({ isInputFocused: true, isOpen: true, valueString: valueString });
78 _this.safeInvokeInputProp("onFocus", e);
79 };
80 _this.handleInputClick = function (e) {
81 // stop propagation to the Popover's internal handleTargetClick handler;
82 // otherwise, the popover will flicker closed as soon as it opens.
83 e.stopPropagation();
84 _this.safeInvokeInputProp("onClick", e);
85 };
86 _this.handleInputChange = function (e) {
87 var _a, _b, _c, _d;
88 var valueString = e.target.value;
89 var value = _this.parseDate(valueString);
90 if ((0, dateUtils_1.isDateValid)(value) && _this.isDateInRange(value)) {
91 if (_this.props.value === undefined) {
92 _this.setState({ value: value, valueString: valueString });
93 }
94 else {
95 _this.setState({ valueString: valueString });
96 }
97 (_b = (_a = _this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, value, true);
98 }
99 else {
100 if (valueString.length === 0) {
101 (_d = (_c = _this.props).onChange) === null || _d === void 0 ? void 0 : _d.call(_c, null, true);
102 }
103 _this.setState({ valueString: valueString });
104 }
105 _this.safeInvokeInputProp("onChange", e);
106 };
107 _this.handleInputBlur = function (e) {
108 var _a, _b, _c, _d, _e, _f;
109 var valueString = _this.state.valueString;
110 var date = _this.parseDate(valueString);
111 if (valueString.length > 0 &&
112 valueString !== (0, dateFormat_1.getFormattedDateString)(_this.state.value, _this.props) &&
113 (!(0, dateUtils_1.isDateValid)(date) || !_this.isDateInRange(date))) {
114 if (_this.props.value === undefined) {
115 _this.setState({ isInputFocused: false, value: date, valueString: null });
116 }
117 else {
118 _this.setState({ isInputFocused: false });
119 }
120 if (isNaN(date.valueOf())) {
121 (_b = (_a = _this.props).onError) === null || _b === void 0 ? void 0 : _b.call(_a, new Date(undefined));
122 }
123 else if (!_this.isDateInRange(date)) {
124 (_d = (_c = _this.props).onError) === null || _d === void 0 ? void 0 : _d.call(_c, date);
125 }
126 else {
127 (_f = (_e = _this.props).onChange) === null || _f === void 0 ? void 0 : _f.call(_e, date, true);
128 }
129 }
130 else {
131 if (valueString.length === 0) {
132 _this.setState({ isInputFocused: false, value: null, valueString: null });
133 }
134 else {
135 _this.setState({ isInputFocused: false });
136 }
137 }
138 _this.safeInvokeInputProp("onBlur", e);
139 };
140 _this.handleInputKeyDown = function (e) {
141 var _a, _b;
142 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
143 /* eslint-disable deprecation/deprecation */
144 if (e.which === core_1.Keys.ENTER) {
145 var nextDate = _this.parseDate(_this.state.valueString);
146 _this.handleDateChange(nextDate, true, true);
147 }
148 else if (e.which === core_1.Keys.TAB && e.shiftKey) {
149 // close popover on SHIFT+TAB key press
150 _this.handleClosePopover();
151 }
152 else if (e.which === core_1.Keys.TAB && _this.state.isOpen) {
153 (_a = _this.getKeyboardFocusableElements().shift()) === null || _a === void 0 ? void 0 : _a.focus();
154 // necessary to prevent focusing the second focusable element
155 e.preventDefault();
156 }
157 else if (e.which === core_1.Keys.ESCAPE) {
158 _this.setState({ isOpen: false });
159 (_b = _this.inputElement) === null || _b === void 0 ? void 0 : _b.blur();
160 }
161 _this.safeInvokeInputProp("onKeyDown", e);
162 };
163 _this.getKeyboardFocusableElements = function () {
164 var _a;
165 var elements = Array.from((_a = _this.popoverContentElement) === null || _a === void 0 ? void 0 : _a.querySelectorAll("button:not([disabled]),input,[tabindex]:not([tabindex='-1'])"));
166 // Remove focus boundary div elements
167 elements.pop();
168 elements.shift();
169 return elements;
170 };
171 _this.handleStartFocusBoundaryFocusIn = function (e) {
172 var _a, _b;
173 if (_this.popoverContentElement.contains(_this.getRelatedTarget(e))) {
174 // Not closing Popover to allow user to freely switch between manually entering a date
175 // string in the input and selecting one via the Popover
176 (_a = _this.inputElement) === null || _a === void 0 ? void 0 : _a.focus();
177 }
178 else {
179 (_b = _this.getKeyboardFocusableElements().shift()) === null || _b === void 0 ? void 0 : _b.focus();
180 }
181 };
182 _this.handleEndFocusBoundaryFocusIn = function (e) {
183 var _a, _b;
184 if (_this.popoverContentElement.contains(_this.getRelatedTarget(e))) {
185 (_a = _this.inputElement) === null || _a === void 0 ? void 0 : _a.focus();
186 _this.handleClosePopover();
187 }
188 else {
189 (_b = _this.getKeyboardFocusableElements().pop()) === null || _b === void 0 ? void 0 : _b.focus();
190 }
191 };
192 _this.handleShortcutChange = function (_, selectedShortcutIndex) {
193 _this.setState({ selectedShortcutIndex: selectedShortcutIndex });
194 };
195 return _this;
196 }
197 DateInput.prototype.render = function () {
198 var _this = this;
199 var _a = this.state, value = _a.value, valueString = _a.valueString;
200 var dateString = this.state.isInputFocused ? valueString : (0, dateFormat_1.getFormattedDateString)(value, this.props);
201 var dateValue = (0, dateUtils_1.isDateValid)(value) ? value : null;
202 var dayPickerProps = tslib_1.__assign(tslib_1.__assign({}, this.props.dayPickerProps), { onDayKeyDown: function (day, modifiers, e) {
203 var _a, _b;
204 (_b = (_a = _this.props.dayPickerProps).onDayKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, day, modifiers, e);
205 }, onMonthChange: function (month) {
206 var _a, _b;
207 (_b = (_a = _this.props.dayPickerProps).onMonthChange) === null || _b === void 0 ? void 0 : _b.call(_a, month);
208 } });
209 // React's onFocus prop listens to the focusin browser event under the hood, so it's safe to
210 // provide it the focusIn event handlers instead of using a ref and manually adding the
211 // event listeners ourselves.
212 var wrappedPopoverContent = (React.createElement("div", { ref: this.handlePopoverContentRef },
213 React.createElement("div", { onFocus: this.handleStartFocusBoundaryFocusIn, tabIndex: 0 }),
214 React.createElement(datePicker_1.DatePicker, tslib_1.__assign({}, this.props, { dayPickerProps: dayPickerProps, onChange: this.handleDateChange, value: dateValue, onShortcutChange: this.handleShortcutChange, selectedShortcutIndex: this.state.selectedShortcutIndex })),
215 React.createElement("div", { onFocus: this.handleEndFocusBoundaryFocusIn, tabIndex: 0 })));
216 // assign default empty object here to prevent mutation
217 var _b = this.props, _c = _b.inputProps, inputProps = _c === void 0 ? {} : _c, _d = _b.popoverProps, popoverProps = _d === void 0 ? {} : _d;
218 var isErrorState = value != null && (!(0, dateUtils_1.isDateValid)(value) || !this.isDateInRange(value));
219 return (
220 /* eslint-disable-next-line deprecation/deprecation */
221 React.createElement(core_1.Popover, tslib_1.__assign({ isOpen: this.state.isOpen && !this.props.disabled, fill: this.props.fill }, popoverProps, { autoFocus: false, className: (0, classnames_1.default)(popoverProps.className, this.props.className), content: wrappedPopoverContent, enforceFocus: false, onClose: this.handleClosePopover, popoverClassName: (0, classnames_1.default)(Classes.DATEINPUT_POPOVER, popoverProps.popoverClassName) }),
222 React.createElement(core_1.InputGroup, tslib_1.__assign({ autoComplete: "off", intent: isErrorState ? core_1.Intent.DANGER : core_1.Intent.NONE, placeholder: this.props.placeholder, rightElement: this.props.rightElement, type: "text" }, inputProps, { disabled: this.props.disabled, inputRef: this.handleInputRef, onBlur: this.handleInputBlur, onChange: this.handleInputChange, onClick: this.handleInputClick, onFocus: this.handleInputFocus, onKeyDown: this.handleInputKeyDown, value: dateString }))));
223 };
224 DateInput.prototype.componentDidUpdate = function (prevProps, prevState) {
225 var _a, _b, _c, _d, _e;
226 _super.prototype.componentDidUpdate.call(this, prevProps, prevState);
227 if (((_a = prevProps.inputProps) === null || _a === void 0 ? void 0 : _a.inputRef) !== ((_b = this.props.inputProps) === null || _b === void 0 ? void 0 : _b.inputRef)) {
228 (0, core_1.setRef)((_c = prevProps.inputProps) === null || _c === void 0 ? void 0 : _c.inputRef, null);
229 this.handleInputRef = (0, core_1.refHandler)(this, "inputElement", (_d = this.props.inputProps) === null || _d === void 0 ? void 0 : _d.inputRef);
230 (0, core_1.setRef)((_e = this.props.inputProps) === null || _e === void 0 ? void 0 : _e.inputRef, this.inputElement);
231 }
232 if (prevProps.value !== this.props.value) {
233 this.setState({ value: this.props.value });
234 }
235 };
236 DateInput.prototype.isDateInRange = function (value) {
237 return (0, dateUtils_1.isDayInRange)(value, [this.props.minDate, this.props.maxDate]);
238 };
239 DateInput.prototype.hasMonthChanged = function (prevDate, nextDate) {
240 return (prevDate == null) !== (nextDate == null) || nextDate.getMonth() !== prevDate.getMonth();
241 };
242 DateInput.prototype.hasTimeChanged = function (prevDate, nextDate) {
243 if (this.props.timePrecision == null) {
244 return false;
245 }
246 return ((prevDate == null) !== (nextDate == null) ||
247 nextDate.getHours() !== prevDate.getHours() ||
248 nextDate.getMinutes() !== prevDate.getMinutes() ||
249 nextDate.getSeconds() !== prevDate.getSeconds() ||
250 nextDate.getMilliseconds() !== prevDate.getMilliseconds());
251 };
252 DateInput.prototype.getRelatedTarget = function (e) {
253 var _a;
254 // Support IE11 (#2924)
255 return ((_a = e.relatedTarget) !== null && _a !== void 0 ? _a : document.activeElement);
256 };
257 /** safe wrapper around invoking input props event handler (prop defaults to undefined) */
258 DateInput.prototype.safeInvokeInputProp = function (name, e) {
259 var _a;
260 var _b = this.props.inputProps, inputProps = _b === void 0 ? {} : _b;
261 (_a = inputProps[name]) === null || _a === void 0 ? void 0 : _a.call(inputProps, e);
262 };
263 DateInput.prototype.parseDate = function (dateString) {
264 if (dateString === this.props.outOfRangeMessage || dateString === this.props.invalidDateMessage) {
265 return null;
266 }
267 var _a = this.props, locale = _a.locale, parseDate = _a.parseDate;
268 var newDate = parseDate(dateString, locale);
269 return newDate === false ? new Date(undefined) : newDate;
270 };
271 DateInput.prototype.formatDate = function (date) {
272 if (!(0, dateUtils_1.isDateValid)(date) || !this.isDateInRange(date)) {
273 return "";
274 }
275 var _a = this.props, locale = _a.locale, formatDate = _a.formatDate;
276 return formatDate(date, locale);
277 };
278 DateInput.displayName = "".concat(core_1.DISPLAYNAME_PREFIX, ".DateInput");
279 DateInput.defaultProps = {
280 closeOnSelection: true,
281 dayPickerProps: {},
282 disabled: false,
283 invalidDateMessage: "Invalid date",
284 maxDate: (0, datePickerCore_1.getDefaultMaxDate)(),
285 minDate: (0, datePickerCore_1.getDefaultMinDate)(),
286 outOfRangeMessage: "Out of range",
287 reverseMonthAndYearMenus: false,
288 };
289 return DateInput;
290}(core_1.AbstractPureComponent2));
291exports.DateInput = DateInput;
292//# sourceMappingURL=dateInput.js.map
\No newline at end of file