UNPKG

semantic-ui-calendar-react

Version:
267 lines (214 loc) 11.2 kB
"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 _HourView = _interopRequireDefault(require("../../views/HourView")); 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 HOURS_ON_PAGE = 24; var PAGE_WIDTH = 4; var HourPicker = /*#__PURE__*/ function (_BasePicker) { _inherits(HourPicker, _BasePicker); function HourPicker(props) { var _this; _classCallCheck(this, HourPicker); _this = _possibleConstructorReturn(this, _getPrototypeOf(HourPicker).call(this, props)); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getSelectableCellPositions", function () { return _lodash.default.filter(_lodash.default.range(0, HOURS_ON_PAGE), function (h) { return !_lodash.default.includes(_this.getDisabledHoursPositions(), h); }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getInitialDatePosition", function () { return 0; }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleChange", function (e, _ref) { var value = _ref.value; var data = { year: _this.state.date.year(), month: _this.state.date.month(), date: _this.state.date.date(), hour: _this.buildCalendarValues().indexOf(value) }; _lodash.default.invoke(_this.props, 'onChange', e, _objectSpread({}, _this.props, { value: data })); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "switchToNextPage", function () { _this.setState(function (_ref2) { var date = _ref2.date; var nextDate = date.clone(); nextDate.add(1, 'day'); return { date: nextDate }; }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "switchToPrevPage", function () { _this.setState(function (_ref3) { var date = _ref3.date; var prevDate = date.clone(); prevDate.subtract(1, 'day'); return { date: prevDate }; }); }); _this.state = { /* moment instance */ date: props.initializeWith.clone() }; _this.PAGE_WIDTH = PAGE_WIDTH; return _this; } _createClass(HourPicker, [{ key: "buildCalendarValues", value: function buildCalendarValues() { var _this2 = this; /* Return array of hours (strings) like ['16:00', '17:00', ...] that used to populate calendar's page. */ return _lodash.default.range(0, 24).map(function (h) { return "".concat(h < 10 ? '0' : '').concat(h); }).map(function (hour) { return (0, _sharedFunctions.buildTimeStringWithSuffix)(hour, '00', _this2.props.timeFormat); }); } }, { key: "getActiveCellPosition", value: function getActiveCellPosition() { /* Return position of an hour that should be displayed as active (position in array returned by `this.buildCalendarValues`). */ var value = this.props.value; if (value && value.isSame(this.state.date, 'date')) { return this.props.value.hour(); } } }, { 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: "getDisabledHoursPositions", value: function getDisabledHoursPositions() { var _this3 = this; /* Return position numbers of hours that should be displayed as disabled (position in array returned by `this.buildCalendarValues`). */ var _this$props = this.props, disable = _this$props.disable, minDate = _this$props.minDate, maxDate = _this$props.maxDate; var disabledByDisable = []; var disabledByMaxDate = []; var disabledByMinDate = []; if (_lodash.default.isArray(disable)) { disabledByDisable = _lodash.default.concat(disabledByDisable, disable.filter(function (date) { return date.isSame(_this3.state.date, 'day'); }).map(function (date) { return date.hour(); })); } if (minDate) { if (minDate.isSame(this.state.date, 'day')) { disabledByMinDate = _lodash.default.concat(disabledByMinDate, _lodash.default.range(0, minDate.hour())); } } if (maxDate) { if (maxDate.isSame(this.state.date, 'day')) { disabledByMaxDate = _lodash.default.concat(disabledByMaxDate, _lodash.default.range(maxDate.hour() + 1, 24)); } } var result = _lodash.default.sortBy(_lodash.default.uniq(_lodash.default.concat(disabledByDisable, disabledByMaxDate, disabledByMinDate))); if (result.length > 0) { return result; } } }, { key: "getCurrentDate", value: function getCurrentDate() { /* Return currently selected month, date and year(string) to display in calendar header. */ return (0, _sharedFunctions.getCurrentDate)(this.state.date); } }, { key: "render", value: function render() { var rest = (0, _lib.getUnhandledProps)(HourPicker, this.props); return _react.default.createElement(_HourView.default, _extends({}, rest, { hours: this.buildCalendarValues(), onNextPageBtnClick: this.switchToNextPage, onPrevPageBtnClick: this.switchToPrevPage, hasPrevPage: this.isPrevPageAvailable(), hasNextPage: this.isNextPageAvailable(), onHourClick: this.handleChange, onBlur: this.handleBlur, inline: this.props.inline, onMount: this.props.onCalendarViewMount, hovered: this.state.hoveredCellPosition, onCellHover: this.onHoveredCellPositionChange, disabled: this.getDisabledHoursPositions(), active: this.getActiveCellPosition(), currentDate: this.getCurrentDate() })); } }]); return HourPicker; }(_BasePicker2.default); _defineProperty(HourPicker, "handledProps", ["closePopup", "disable", "initializeWith", "inline", "isPickerInFocus", "isTriggerInFocus", "maxDate", "minDate", "onCalendarViewMount", "onChange", "timeFormat", "value"]); HourPicker.propTypes = { /** Called after hour is selected. */ onChange: _propTypes.default.func.isRequired, /** A value for initializing hour picker's state. */ initializeWith: _propTypes.default.instanceOf(_moment.default).isRequired, /** Currently selected hour. */ value: _propTypes.default.instanceOf(_moment.default), /** Array of disabled hours. */ disable: _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), /** Time format. */ timeFormat: _propTypes.default.oneOf(['ampm', 'AMPM', '24']), isPickerInFocus: _propTypes.default.func, isTriggerInFocus: _propTypes.default.func, onCalendarViewMount: _propTypes.default.func, /** Force popup to close. */ closePopup: _propTypes.default.func, inline: _propTypes.default.bool }; HourPicker.defaultProps = { timeFormat: '24' }; var _default = HourPicker; exports.default = _default;