semantic-ui-calendar-react
Version:
date/time picker built from semantic-ui elements
412 lines (340 loc) • 15.9 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 _DatesRangeView = _interopRequireDefault(require("../../views/DatesRangeView"));
var _DayPicker = require("./DayPicker");
var _lib = require("../../lib");
var _sharedFunctions = require("./sharedFunctions");
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 PAGE_WIDTH = 7;
/** Return position of a given date on the page.
*
* Page consists of some dates from previous month, dates from current month
* and some dates from next month.
* Return undefined if date that is under test is out of page.
*
* @param {Moment} prevMonth
* @param {Moment} currentMonth
* @param {Moment} nextMonth
* @param {Moment} date Date to test
* @param {number[]} fromPrevMonthDates
* @param {number[]} fromCurrentMonthDates
* @param {number[]} fromNextMonthDates
*/
function getDatePosition(prevMonth, currentMonth, nextMonth, date, fromPrevMonthDates, fromCurrentMonthDates, fromNextMonthDates) {
if (date.isSame(prevMonth, 'month')) {
var position = fromPrevMonthDates.indexOf(date.date());
if (position >= 0) {
return position;
}
}
if (date.isSame(currentMonth, 'month')) {
return fromCurrentMonthDates.indexOf(date.date()) + fromPrevMonthDates.length;
}
if (date.isSame(nextMonth, 'month')) {
var _position = fromNextMonthDates.indexOf(date.date());
if (_position >= 0) {
return _position + fromPrevMonthDates.length + fromCurrentMonthDates.length;
}
}
}
function getDatesFromPrevMonth(date, allDays, currentMonthStartPosition) {
if (currentMonthStartPosition === 0) {
return [];
}
return allDays.slice(0, currentMonthStartPosition).map(function (date) {
return parseInt(date);
});
}
function getDatesFromNextMonth(date, allDays, nextMonthStartPosition) {
if (nextMonthStartPosition === allDays.length) {
return [];
}
return allDays.slice(nextMonthStartPosition, allDays.length).map(function (date) {
return parseInt(date);
});
}
/** Build moment based on current page and date position on that page. */
function buildMoment(date
/*Moment*/
, firstOnPage
/*number*/
, dateToBuildPosition
/*number*/
) {
var result;
if (firstOnPage === 1
/* page starts from first day in month */
) {
result = (0, _moment.default)({
year: date.year(),
month: date.month(),
date: firstOnPage
});
} else {
/* page starts from day in previous month */
result = (0, _moment.default)({
year: date.month() ? date.year() : date.year() - 1,
month: (date.month() + 11) % 12,
date: firstOnPage
});
}
result.add(dateToBuildPosition, 'day');
return result;
}
var DatesRangePicker =
/*#__PURE__*/
function (_BasePicker) {
_inherits(DatesRangePicker, _BasePicker);
function DatesRangePicker(props) {
var _this;
_classCallCheck(this, DatesRangePicker);
_this = _possibleConstructorReturn(this, _getPrototypeOf(DatesRangePicker).call(this, props));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getSelectableCellPositions", function () {
return _lodash.default.filter(_lodash.default.range(0, _DayPicker.DAYS_ON_PAGE), function (d) {
return !_lodash.default.includes(_this.getDisabledDaysPositions(), d);
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getInitialDatePosition", function () {
return _this.buildCalendarValues().indexOf(_this.state.date.date().toString());
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleChange", function (e, _ref) {
var itemPosition = _ref.itemPosition;
// call `onChange` with value: { start: moment, end: moment }
var _this$props = _this.props,
start = _this$props.start,
end = _this$props.end;
var firstOnPage = parseInt(_this.buildCalendarValues()[0]);
if (_lodash.default.isNil(start) && _lodash.default.isNil(end)) {
var range = {
start: buildMoment(_this.state.date, firstOnPage, itemPosition)
};
_lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, {
value: range
}));
} else if (!_lodash.default.isNil(start) && _lodash.default.isNil(end)) {
var selectedDate = buildMoment(_this.state.date, firstOnPage, itemPosition);
if (selectedDate.isAfter(start, 'date')) {
var _range = {
start: start,
end: selectedDate
};
_lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, {
value: _range
}));
} else {
_lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, {
value: {}
}));
}
} else {
_lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, {
value: {}
}));
}
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "switchToNextPage", function () {
_this.setState(function (_ref2) {
var date = _ref2.date;
var nextDate = date.clone();
nextDate.add(1, 'month');
return {
date: nextDate
};
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "switchToPrevPage", function () {
_this.setState(function (_ref3) {
var date = _ref3.date;
var prevDate = date.clone();
prevDate.subtract(1, 'month');
return {
date: prevDate
};
});
});
_this.state = {
/* moment instance */
date: props.initializeWith.clone()
};
_this.PAGE_WIDTH = PAGE_WIDTH;
return _this;
}
_createClass(DatesRangePicker, [{
key: "buildCalendarValues",
value: function buildCalendarValues() {
/*
Return array of dates (strings) like ['31', '1', ...]
that used to populate calendar's page.
*/
return (0, _sharedFunctions.buildDays)(this.state.date, _DayPicker.DAYS_ON_PAGE);
}
}, {
key: "getActiveCellsPositions",
// TODO: too complicated method
value: function getActiveCellsPositions() {
/*
Return starting and ending positions of dates range that should be displayed as active
{ start: number, end: number }
(position in array returned by `this.buildCalendarValues`).
*/
var date = this.state.date;
var _this$props2 = this.props,
start = _this$props2.start,
end = _this$props2.end;
var allDays = this.buildCalendarValues();
var fromCurrentMonthDayPositions = (0, _sharedFunctions.getDefaultEnabledDayPositions)(allDays, date);
var fromPrevMonthDates = getDatesFromPrevMonth(date, allDays, fromCurrentMonthDayPositions[0]);
var fromNextMonthDates = getDatesFromNextMonth(date, allDays, _lodash.default.last(fromCurrentMonthDayPositions) + 1);
var fromCurrentMonthDates = _lodash.default.range(1, this.state.date.daysInMonth() + 1);
var prevMonth = date.clone();
prevMonth.subtract(1, 'month');
var nextMonth = date.clone();
nextMonth.add(1, 'month');
if (start && end) {
var startPosition = getDatePosition(prevMonth, this.state.date, nextMonth, start, fromPrevMonthDates, fromCurrentMonthDates, fromNextMonthDates);
var endPosition = getDatePosition(prevMonth, this.state.date, nextMonth, end, fromPrevMonthDates, fromCurrentMonthDates, fromNextMonthDates);
if (startPosition && endPosition) {
return {
start: startPosition,
end: endPosition
};
}
if (startPosition) {
return {
start: startPosition,
end: _DayPicker.DAYS_ON_PAGE - 1
};
}
if (endPosition) {
return {
start: 0,
end: endPosition
};
}
if (this.state.date.isBetween(start, end)) {
return {
start: 0,
end: _DayPicker.DAYS_ON_PAGE - 1
};
}
}
if (start) {
var _startPosition = getDatePosition(prevMonth, this.state.date, nextMonth, start, fromPrevMonthDates, fromCurrentMonthDates, fromNextMonthDates);
return {
start: _startPosition,
end: undefined
};
}
return {
start: undefined,
end: undefined
};
}
}, {
key: "getDisabledDaysPositions",
value: function getDisabledDaysPositions() {
/*
Return position numbers of dates that should be displayed as disabled
(position in array returned by `this.buildCalendarValues`).
*/
var _this$props3 = this.props,
maxDate = _this$props3.maxDate,
minDate = _this$props3.minDate;
return (0, _sharedFunctions.getDisabledDays)(undefined, maxDate, minDate, this.state.date, _DayPicker.DAYS_ON_PAGE);
}
}, {
key: "isNextPageAvailable",
value: function isNextPageAvailable() {
return (0, _sharedFunctions.isNextPageAvailable)(this.state.date, this.props.maxDate);
}
}, {
key: "isPrevPageAvailable",
value: function isPrevPageAvailable() {
return (0, _sharedFunctions.isPrevPageAvailable)(this.state.date, this.props.minDate);
}
}, {
key: "getCurrentDate",
value: function getCurrentDate() {
/* Return currently selected year and month(string) to display in calendar header. */
return this.state.date.format('MMMM YYYY');
}
}, {
key: "getSelectedRange",
value: function getSelectedRange() {
/* Return currently selected dates range(string) to display in calendar header. */
var _this$props4 = this.props,
start = _this$props4.start,
end = _this$props4.end,
dateFormat = _this$props4.dateFormat;
return "".concat(start ? start.format(dateFormat) : '- - -', " - ").concat(end ? end.format(dateFormat) : '- - -');
}
}, {
key: "render",
value: function render() {
var rest = (0, _lib.getUnhandledProps)(DatesRangePicker, this.props);
return _react.default.createElement(_DatesRangeView.default, _extends({}, rest, {
days: this.buildCalendarValues(),
onNextPageBtnClick: this.switchToNextPage,
onPrevPageBtnClick: this.switchToPrevPage,
onCellHover: this.onHoveredCellPositionChange,
hovered: this.state.hoveredCellPosition,
onDayClick: this.handleChange,
inline: this.props.inline,
hasPrevPage: this.isPrevPageAvailable(),
hasNextPage: this.isNextPageAvailable(),
onBlur: this.handleBlur,
onMount: this.props.onCalendarViewMount,
currentDate: this.getCurrentDate(),
selectedRange: this.getSelectedRange(),
active: this.getActiveCellsPositions(),
disabled: this.getDisabledDaysPositions()
}));
}
}]);
return DatesRangePicker;
}(_BasePicker2.default);
_defineProperty(DatesRangePicker, "handledProps", ["closePopup", "dateFormat", "end", "initializeWith", "inline", "isPickerInFocus", "isTriggerInFocus", "maxDate", "minDate", "onCalendarViewMount", "onChange", "start"]);
DatesRangePicker.propTypes = {
/** Called after day is selected. */
onChange: _propTypes.default.func.isRequired,
/** A value for initializing day picker's state. */
initializeWith: _propTypes.default.instanceOf(_moment.default).isRequired,
/** Moment date formatting string. */
dateFormat: _propTypes.default.string.isRequired,
/** Start of currently selected dates range. */
start: _propTypes.default.instanceOf(_moment.default),
/** End of currently selected dates range. */
end: _propTypes.default.instanceOf(_moment.default),
/** Minimal date that could be selected. */
minDate: _propTypes.default.instanceOf(_moment.default),
/** Maximal date that could be selected. */
maxDate: _propTypes.default.instanceOf(_moment.default),
isPickerInFocus: _propTypes.default.func,
isTriggerInFocus: _propTypes.default.func,
onCalendarViewMount: _propTypes.default.func,
/** Force popup to close. */
closePopup: _propTypes.default.func,
inline: _propTypes.default.bool
};
var _default = DatesRangePicker;
exports.default = _default;