UNPKG

5.64 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2015 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.DatePickerCaption = void 0;
19var tslib_1 = require("tslib");
20var React = tslib_1.__importStar(require("react"));
21var core_1 = require("@blueprintjs/core");
22var Classes = tslib_1.__importStar(require("./common/classes"));
23var dateUtils_1 = require("./common/dateUtils");
24var utils_1 = require("./common/utils");
25var DatePickerCaption = /** @class */ (function (_super) {
26 tslib_1.__extends(DatePickerCaption, _super);
27 function DatePickerCaption() {
28 var _this = _super !== null && _super.apply(this, arguments) || this;
29 _this.state = { monthRightOffset: 0 };
30 _this.handleMonthSelectChange = _this.dateChangeHandler(function (d, month) { return d.setMonth(month); }, _this.props.onMonthChange);
31 _this.handleYearSelectChange = _this.dateChangeHandler(function (d, year) { return d.setFullYear(year); }, _this.props.onYearChange);
32 return _this;
33 }
34 DatePickerCaption.prototype.render = function () {
35 var _this = this;
36 var _a = this.props, date = _a.date, locale = _a.locale, localeUtils = _a.localeUtils, minDate = _a.minDate, maxDate = _a.maxDate, _b = _a.months, months = _b === void 0 ? localeUtils.getMonths(locale) : _b;
37 var minYear = minDate.getFullYear();
38 var maxYear = maxDate.getFullYear();
39 var displayMonth = date.getMonth();
40 var displayYear = date.getFullYear();
41 // build the list of available months, limiting based on minDate and maxDate as necessary
42 var startMonth = displayYear === minYear ? minDate.getMonth() : 0;
43 var endMonth = displayYear === maxYear ? maxDate.getMonth() + 1 : undefined;
44 var monthOptionElements = months
45 .map(function (month, i) { return ({ label: month, value: i }); })
46 .slice(startMonth, endMonth);
47 var years = [minYear];
48 for (var year = minYear + 1; year <= maxYear; ++year) {
49 years.push(year);
50 }
51 // allow out-of-bounds years but disable the option. this handles the Dec 2016 case in #391.
52 if (displayYear > maxYear) {
53 years.push({ value: displayYear, disabled: true });
54 }
55 this.displayedMonthText = months[displayMonth];
56 var monthSelect = (React.createElement(core_1.HTMLSelect, { "aria-label": "Month", iconProps: { style: { right: this.state.monthRightOffset } }, className: Classes.DATEPICKER_MONTH_SELECT, key: "month", minimal: true, onChange: this.handleMonthSelectChange, value: displayMonth, options: monthOptionElements }));
57 var yearSelect = (React.createElement(core_1.HTMLSelect, { "aria-label": "Year", className: Classes.DATEPICKER_YEAR_SELECT, key: "year", minimal: true, onChange: this.handleYearSelectChange, value: displayYear, options: years }));
58 var orderedSelects = this.props.reverseMonthAndYearMenus
59 ? [yearSelect, monthSelect]
60 : [monthSelect, yearSelect];
61 return (React.createElement("div", { className: this.props.classNames.caption },
62 React.createElement("div", { className: Classes.DATEPICKER_CAPTION, ref: function (ref) { return (_this.containerElement = ref); } }, orderedSelects),
63 React.createElement(core_1.Divider, null)));
64 };
65 DatePickerCaption.prototype.componentDidMount = function () {
66 var _this = this;
67 this.requestAnimationFrame(function () { return _this.positionArrows(); });
68 };
69 DatePickerCaption.prototype.componentDidUpdate = function () {
70 this.positionArrows();
71 };
72 DatePickerCaption.prototype.positionArrows = function () {
73 // measure width of text as rendered inside our container element.
74 var monthTextWidth = (0, utils_1.measureTextWidth)(this.displayedMonthText, Classes.DATEPICKER_CAPTION_MEASURE, this.containerElement);
75 var monthSelectWidth = this.containerElement == null ? 0 : this.containerElement.firstElementChild.clientWidth;
76 var rightOffset = Math.max(2, monthSelectWidth - monthTextWidth - core_1.IconSize.STANDARD - 2);
77 this.setState({ monthRightOffset: rightOffset });
78 };
79 DatePickerCaption.prototype.dateChangeHandler = function (updater, handler) {
80 var _this = this;
81 return function (e) {
82 var _a, _b;
83 var value = parseInt(e.target.value, 10);
84 // ignore change events with invalid values to prevent crash on iOS Safari (#4178)
85 if (isNaN(value)) {
86 return;
87 }
88 var newDate = (0, dateUtils_1.clone)(_this.props.date);
89 updater(newDate, value);
90 (_b = (_a = _this.props).onDateChange) === null || _b === void 0 ? void 0 : _b.call(_a, newDate);
91 handler === null || handler === void 0 ? void 0 : handler(value);
92 };
93 };
94 return DatePickerCaption;
95}(core_1.AbstractPureComponent2));
96exports.DatePickerCaption = DatePickerCaption;
97//# sourceMappingURL=datePickerCaption.js.map
\No newline at end of file