semantic-ui-calendar-react
Version:
date/time picker built from semantic-ui elements
262 lines (209 loc) • 11.6 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.DAYS_ON_PAGE = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _moment = _interopRequireDefault(require("moment"));
var _lodash = _interopRequireDefault(require("lodash"));
var _DayView = _interopRequireWildcard(require("../../views/DayView"));
var _lib = require("../../lib");
var _sharedFunctions = require("./sharedFunctions");
var _BasePicker2 = _interopRequireDefault(require("../BasePicker"));
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
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;
var DAYS_ON_PAGE = _DayView.WEEKS_TO_DISPLAY * PAGE_WIDTH;
exports.DAYS_ON_PAGE = DAYS_ON_PAGE;
var DayPicker =
/*#__PURE__*/
function (_BasePicker) {
_inherits(DayPicker, _BasePicker);
function DayPicker(props) {
var _this;
_classCallCheck(this, DayPicker);
_this = _possibleConstructorReturn(this, _getPrototypeOf(DayPicker).call(this, props));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getSelectableCellPositions", function () {
return _lodash.default.filter(_lodash.default.range(0, 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 value = _ref.value;
// `value` is selected date(string) like '31' or '1'
var result = {
year: _this.state.date.year(),
month: _this.state.date.month(),
date: parseInt(value)
};
_lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, {
value: result
}));
});
_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(DayPicker, [{
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, DAYS_ON_PAGE);
}
}, {
key: "getActiveCellPosition",
value: function getActiveCellPosition() {
/*
Return position of a date that should be displayed as active
(position in array returned by `this.buildCalendarValues`).
*/
if (this.props.value && this.props.value.isSame(this.state.date, 'month')) {
var disabledPositions = this.getDisabledDaysPositions();
var active = this.buildCalendarValues().map(function (day, i) {
return _lodash.default.includes(disabledPositions, i) ? undefined : day;
}).indexOf(this.props.value.date().toString());
if (active >= 0) {
return active;
}
}
}
}, {
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$props = this.props,
disable = _this$props.disable,
maxDate = _this$props.maxDate,
minDate = _this$props.minDate,
enable = _this$props.enable;
return (0, _sharedFunctions.getDisabledDays)(disable, maxDate, minDate, this.state.date, DAYS_ON_PAGE, enable);
}
}, {
key: "isNextPageAvailable",
value: function isNextPageAvailable() {
var _this2 = this;
var _this$props2 = this.props,
maxDate = _this$props2.maxDate,
enable = _this$props2.enable;
if (_lodash.default.isArray(enable)) {
return _lodash.default.some(enable, function (enabledDate) {
return enabledDate.isAfter(_this2.state.date, 'month');
});
}
return (0, _sharedFunctions.isNextPageAvailable)(this.state.date, maxDate);
}
}, {
key: "isPrevPageAvailable",
value: function isPrevPageAvailable() {
var _this3 = this;
var _this$props3 = this.props,
minDate = _this$props3.minDate,
enable = _this$props3.enable;
if (_lodash.default.isArray(enable)) {
return _lodash.default.some(enable, function (enabledDate) {
return enabledDate.isBefore(_this3.state.date, 'month');
});
}
return (0, _sharedFunctions.isPrevPageAvailable)(this.state.date, 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: "render",
value: function render() {
var rest = (0, _lib.getUnhandledProps)(DayPicker, this.props);
return _react.default.createElement(_DayView.default, _extends({}, rest, {
days: this.buildCalendarValues(),
hasNextPage: this.isNextPageAvailable(),
hasPrevPage: this.isPrevPageAvailable(),
onNextPageBtnClick: this.switchToNextPage,
onPrevPageBtnClick: this.switchToPrevPage,
onDayClick: this.handleChange,
onBlur: this.handleBlur,
inline: this.props.inline,
onMount: this.props.onCalendarViewMount,
hovered: this.state.hoveredCellPosition,
onCellHover: this.onHoveredCellPositionChange,
currentDate: this.getCurrentDate(),
disabled: this.getDisabledDaysPositions(),
active: this.getActiveCellPosition()
}));
}
}]);
return DayPicker;
}(_BasePicker2.default);
_defineProperty(DayPicker, "handledProps", ["closePopup", "disable", "displayWeeks", "enable", "initializeWith", "inline", "isPickerInFocus", "isTriggerInFocus", "maxDate", "minDate", "onCalendarViewMount", "onChange", "value"]);
DayPicker.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,
displayWeeks: _propTypes.default.bool,
/** Currently selected day. */
value: _propTypes.default.instanceOf(_moment.default),
/** Array of disabled days. */
disable: _propTypes.default.arrayOf(_propTypes.default.instanceOf(_moment.default)),
/** Array of enabled days. */
enable: _propTypes.default.arrayOf(_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),
/** 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 = DayPicker;
exports.default = _default;
;