semantic-ui-calendar-react
Version:
date/time picker built from semantic-ui elements
301 lines (243 loc) • 12.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _moment = _interopRequireDefault(require("moment"));
var _lodash = _interopRequireDefault(require("lodash"));
var _MonthView = _interopRequireDefault(require("../views/MonthView"));
var _lib = require("../lib");
var _BasePicker2 = _interopRequireDefault(require("./BasePicker"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var MONTHS_IN_YEAR = 12;
var PAGE_WIDTH = 3;
var MonthPicker =
/*#__PURE__*/
function (_BasePicker) {
_inherits(MonthPicker, _BasePicker);
/*
Note:
use it like this <MonthPicker key={someInputValue} />
to make react create new instance when input value changes
*/
function MonthPicker(props) {
var _this;
_classCallCheck(this, MonthPicker);
_this = _possibleConstructorReturn(this, _getPrototypeOf(MonthPicker).call(this, props));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getSelectableCellPositions", function () {
return _lodash.default.filter(_lodash.default.range(0, MONTHS_IN_YEAR), function (m) {
return !_lodash.default.includes(_this.getDisabledMonthsPositions(), m);
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleChange", function (e, _ref) {
var value = _ref.value;
var year = parseInt(_this.getCurrentYear());
var month = _this.buildCalendarValues().indexOf(value);
_lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, {
value: {
year: year,
month: month
}
}));
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "switchToNextPage", function () {
_this.setState(function (_ref2) {
var date = _ref2.date;
var nextDate = date.clone();
nextDate.add(1, 'year');
return {
date: nextDate
};
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "switchToPrevPage", function () {
_this.setState(function (_ref3) {
var date = _ref3.date;
var prevDate = date.clone();
prevDate.subtract(1, 'year');
return {
date: prevDate
};
});
});
_this.state = {
/* moment instance */
date: props.initializeWith.clone()
};
_this.PAGE_WIDTH = PAGE_WIDTH;
return _this;
}
_createClass(MonthPicker, [{
key: "buildCalendarValues",
value: function buildCalendarValues() {
/*
Return array of months (strings) like ['Aug', 'Sep', ...]
that used to populate calendar's page.
*/
return _moment.default.monthsShort();
}
}, {
key: "getInitialDatePosition",
value: function getInitialDatePosition() {
return this.state.date.month();
}
}, {
key: "getActiveCellPosition",
value: function getActiveCellPosition() {
/*
Return position of a month that should be displayed as active
(position in array returned by `this.buildCalendarValues`).
*/
if (!_lodash.default.isNil(this.props.value)) {
if (this.props.value.year() === this.state.date.year()) {
return this.props.value.month();
}
}
}
}, {
key: "getDisabledMonthsPositions",
value: function getDisabledMonthsPositions() {
var _this2 = this;
/*
Return position numbers of months that should be displayed as disabled
(position in array returned by `this.buildCalendarValues`).
*/
var disabled = [];
if (_lodash.default.isArray(this.props.enable)) {
var enabledMonthPositions = this.props.enable.filter(function (monthMoment) {
return monthMoment.isSame(_this2.state.date, 'year');
}).map(function (monthMoment) {
return monthMoment.month();
});
disabled = disabled.concat(_lodash.default.range(0, MONTHS_IN_YEAR).filter(function (monthPosition) {
return !_lodash.default.includes(enabledMonthPositions, monthPosition);
}));
}
if (_lodash.default.isArray(this.props.disable)) {
disabled = disabled.concat(this.props.disable.filter(function (monthMoment) {
return monthMoment.year() === _this2.state.date.year();
}).map(function (monthMoment) {
return monthMoment.month();
}));
}
if (!_lodash.default.isNil(this.props.maxDate)) {
if (this.props.maxDate.year() === this.state.date.year()) {
disabled = disabled.concat(_lodash.default.range(this.props.maxDate.month() + 1, MONTHS_IN_YEAR));
}
if (this.props.maxDate.year() < this.state.date.year()) {
disabled = _lodash.default.range(0, MONTHS_IN_YEAR);
}
}
if (!_lodash.default.isNil(this.props.minDate)) {
if (this.props.minDate.year() === this.state.date.year()) {
disabled = disabled.concat(_lodash.default.range(0, this.props.minDate.month()));
}
if (this.props.minDate.year() > this.state.date.year()) {
disabled = _lodash.default.range(0, MONTHS_IN_YEAR);
}
}
if (disabled.length > 0) {
return _lodash.default.uniq(disabled);
}
}
}, {
key: "isNextPageAvailable",
value: function isNextPageAvailable() {
var _this3 = this;
var _this$props = this.props,
maxDate = _this$props.maxDate,
enable = _this$props.enable;
if (_lodash.default.isArray(enable)) {
return _lodash.default.some(enable, function (enabledMonth) {
return enabledMonth.isAfter(_this3.state.date, 'year');
});
}
if (_lodash.default.isNil(maxDate)) return true;
if (this.state.date.year() >= maxDate.year()) return false;
return true;
}
}, {
key: "isPrevPageAvailable",
value: function isPrevPageAvailable() {
var _this4 = this;
var _this$props2 = this.props,
minDate = _this$props2.minDate,
enable = _this$props2.enable;
if (_lodash.default.isArray(enable)) {
return _lodash.default.some(enable, function (enabledMonth) {
return enabledMonth.isBefore(_this4.state.date, 'year');
});
}
if (_lodash.default.isNil(minDate)) return true;
if (this.state.date.year() <= minDate.year()) return false;
return true;
}
}, {
key: "getCurrentYear",
value: function getCurrentYear() {
/* Return current year(string) to display in calendar header. */
return this.state.date.year().toString();
}
}, {
key: "render",
value: function render() {
var rest = (0, _lib.getUnhandledProps)(MonthPicker, this.props);
return _react.default.createElement(_MonthView.default, _extends({}, rest, {
months: this.buildCalendarValues(),
onMonthClick: 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,
disabled: this.getDisabledMonthsPositions(),
active: this.getActiveCellPosition(),
hovered: this.state.hoveredCellPosition,
currentYear: this.getCurrentYear()
}));
}
}]);
return MonthPicker;
}(_BasePicker2.default);
_defineProperty(MonthPicker, "handledProps", ["closePopup", "disable", "enable", "initializeWith", "inline", "isPickerInFocus", "isTriggerInFocus", "maxDate", "minDate", "onCalendarViewMount", "onChange", "value"]);
MonthPicker.propTypes = {
/** Called after month is selected. */
onChange: _propTypes.default.func.isRequired,
/** A value for initializing month picker's state. */
initializeWith: _propTypes.default.instanceOf(_moment.default).isRequired,
/** Currently selected month. */
value: _propTypes.default.instanceOf(_moment.default),
/** Array of disabled months. */
disable: _propTypes.default.arrayOf(_propTypes.default.instanceOf(_moment.default)),
/** Array of enabled months. */
enable: _propTypes.default.arrayOf(_propTypes.default.instanceOf(_moment.default)),
/** Minimal month that could be selected. */
minDate: _propTypes.default.instanceOf(_moment.default),
/** Maximal month that could be selected. */
maxDate: _propTypes.default.instanceOf(_moment.default),
/** Force popup to close. */
closePopup: _propTypes.default.func,
isPickerInFocus: _propTypes.default.func,
isTriggerInFocus: _propTypes.default.func,
onCalendarViewMount: _propTypes.default.func,
inline: _propTypes.default.bool
};
var _default = MonthPicker;
exports.default = _default;