semantic-ui-calendar-react
Version:
date/time picker built from semantic-ui elements
186 lines (185 loc) • 8.87 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var moment = require("moment");
var React = require("react");
var MonthView_1 = require("../views/MonthView");
var BasePicker_1 = require("./BasePicker");
var MONTHS_IN_YEAR = 12;
var PAGE_WIDTH = 3;
var MonthPicker = /** @class */ (function (_super) {
__extends(MonthPicker, _super);
/*
Note:
use it like this <MonthPicker key={someInputValue} />
to make react create new instance when input value changes
*/
function MonthPicker(props) {
var _this = _super.call(this, props) || this;
_this.handleChange = function (e, _a) {
var value = _a.value;
var data = __assign({}, _this.props, { value: {
year: parseInt(_this.getCurrentDate(), 10),
month: _this.buildCalendarValues().indexOf(value),
} });
_this.props.onChange(e, data);
};
_this.switchToNextPage = function (e, data, callback) {
_this.setState(function (_a) {
var date = _a.date;
var nextDate = date.clone();
nextDate.add(1, 'year');
return { date: nextDate };
}, callback);
};
_this.switchToPrevPage = function (e, data, callback) {
_this.setState(function (_a) {
var date = _a.date;
var prevDate = date.clone();
prevDate.subtract(1, 'year');
return { date: prevDate };
}, callback);
};
_this.PAGE_WIDTH = PAGE_WIDTH;
return _this;
}
MonthPicker.prototype.render = function () {
var _a = this.props, onChange = _a.onChange, value = _a.value, initializeWith = _a.initializeWith, closePopup = _a.closePopup, inline = _a.inline, isPickerInFocus = _a.isPickerInFocus, isTriggerInFocus = _a.isTriggerInFocus, onCalendarViewMount = _a.onCalendarViewMount, disable = _a.disable, enable = _a.enable, minDate = _a.minDate, maxDate = _a.maxDate, rest = __rest(_a, ["onChange", "value", "initializeWith", "closePopup", "inline", "isPickerInFocus", "isTriggerInFocus", "onCalendarViewMount", "disable", "enable", "minDate", "maxDate"]);
return (React.createElement(MonthView_1.default, __assign({}, rest, { values: this.buildCalendarValues(), onValueClick: this.handleChange, onCellHover: this.onHoveredCellPositionChange, onNextPageBtnClick: this.switchToNextPage, onPrevPageBtnClick: this.switchToPrevPage, hasPrevPage: this.isPrevPageAvailable(), hasNextPage: this.isNextPageAvailable(), onBlur: this.handleBlur, inline: this.props.inline, onMount: this.props.onCalendarViewMount, disabledItemIndexes: this.getDisabledPositions(), activeItemIndex: this.getActiveCellPosition(), hoveredItemIndex: this.state.hoveredCellPosition, currentHeadingValue: this.getCurrentDate() })));
};
MonthPicker.prototype.getCurrentDate = function () {
/* Return current year(string) to display in calendar header. */
return this.state.date.year().toString();
};
MonthPicker.prototype.buildCalendarValues = function () {
/*
Return array of months (strings) like ['Aug', 'Sep', ...]
that used to populate calendar's page.
*/
return moment.monthsShort();
};
MonthPicker.prototype.getSelectableCellPositions = function () {
var _this = this;
return _.filter(_.range(0, MONTHS_IN_YEAR), function (m) { return !_.includes(_this.getDisabledPositions(), m); });
};
MonthPicker.prototype.getInitialDatePosition = function () {
var selectable = this.getSelectableCellPositions();
if (selectable.indexOf(this.state.date.month()) < 0) {
return selectable[0];
}
return this.state.date.month();
};
MonthPicker.prototype.getActiveCellPosition = function () {
/*
Return position of a month that should be displayed as active
(position in array returned by `this.buildCalendarValues`).
*/
if (!_.isNil(this.props.value)) {
if (this.props.value.year() === this.state.date.year()) {
return this.props.value.month();
}
}
};
MonthPicker.prototype.getDisabledPositions = function () {
var _this = this;
/*
Return position numbers of months that should be displayed as disabled
(position in array returned by `this.buildCalendarValues`).
*/
var disabled = [];
if (_.isArray(this.props.enable)) {
var enabledMonthPositions_1 = this.props.enable
.filter(function (monthMoment) { return monthMoment.isSame(_this.state.date, 'year'); })
.map(function (monthMoment) { return monthMoment.month(); });
disabled = disabled.concat(_.range(0, MONTHS_IN_YEAR)
.filter(function (monthPosition) { return !_.includes(enabledMonthPositions_1, monthPosition); }));
}
if (_.isArray(this.props.disable)) {
disabled = disabled.concat(this.props.disable
.filter(function (monthMoment) { return monthMoment.year() === _this.state.date.year(); })
.map(function (monthMoment) { return monthMoment.month(); }));
}
if (!_.isNil(this.props.maxDate)) {
if (this.props.maxDate.year() === this.state.date.year()) {
disabled = disabled.concat(_.range(this.props.maxDate.month() + 1, MONTHS_IN_YEAR));
}
if (this.props.maxDate.year() < this.state.date.year()) {
disabled = _.range(0, MONTHS_IN_YEAR);
}
}
if (!_.isNil(this.props.minDate)) {
if (this.props.minDate.year() === this.state.date.year()) {
disabled = disabled.concat(_.range(0, this.props.minDate.month()));
}
if (this.props.minDate.year() > this.state.date.year()) {
disabled = _.range(0, MONTHS_IN_YEAR);
}
}
if (disabled.length > 0) {
return _.uniq(disabled);
}
};
MonthPicker.prototype.isNextPageAvailable = function () {
var _this = this;
var _a = this.props, maxDate = _a.maxDate, enable = _a.enable;
if (_.isArray(enable)) {
return _.some(enable, function (enabledMonth) { return enabledMonth.isAfter(_this.state.date, 'year'); });
}
if (_.isNil(maxDate)) {
return true;
}
if (this.state.date.year() >= maxDate.year()) {
return false;
}
return true;
};
MonthPicker.prototype.isPrevPageAvailable = function () {
var _this = this;
var _a = this.props, minDate = _a.minDate, enable = _a.enable;
if (_.isArray(enable)) {
return _.some(enable, function (enabledMonth) { return enabledMonth.isBefore(_this.state.date, 'year'); });
}
if (_.isNil(minDate)) {
return true;
}
if (this.state.date.year() <= minDate.year()) {
return false;
}
return true;
};
return MonthPicker;
}(BasePicker_1.SingleSelectionPicker));
exports.default = MonthPicker;