semantic-ui-calendar-react
Version:
date/time picker built from semantic-ui elements
224 lines (177 loc) • 9.81 kB
JavaScript
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 _MinuteView = _interopRequireDefault(require("../../views/MinuteView"));
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 MINUTES_STEP = 5;
var MINUTES_ON_PAGE = 12;
var PAGE_WIDTH = 3;
var MinutePicker =
/*#__PURE__*/
function (_BasePicker) {
_inherits(MinutePicker, _BasePicker);
function MinutePicker(props) {
var _this;
_classCallCheck(this, MinutePicker);
_this = _possibleConstructorReturn(this, _getPrototypeOf(MinutePicker).call(this, props));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getSelectableCellPositions", function () {
return _lodash.default.range(0, MINUTES_ON_PAGE);
});
_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.state.date.hour(),
minute: _this.buildCalendarValues().indexOf(value) * MINUTES_STEP
};
_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(MinutePicker, [{
key: "buildCalendarValues",
value: function buildCalendarValues() {
var _this2 = this;
/*
Return array of minutes (strings) like ['16:15', '16:20', ...]
that used to populate calendar's page.
*/
var hour = this.state.date.hour() < 10 ? '0' + this.state.date.hour().toString() : this.state.date.hour().toString();
return _lodash.default.range(0, 60, MINUTES_STEP).map(function (minute) {
return "".concat(minute < 10 ? '0' : '').concat(minute);
}).map(function (minute) {
return (0, _sharedFunctions.buildTimeStringWithSuffix)(hour, minute, _this2.props.timeFormat);
});
}
}, {
key: "getActiveCellPosition",
value: function getActiveCellPosition() {
/*
Return position of a minute 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 Math.floor(this.props.value.minutes() / MINUTES_STEP);
}
}
}, {
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 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)(MinutePicker, this.props);
return _react.default.createElement(_MinuteView.default, _extends({}, rest, {
minutes: this.buildCalendarValues(),
onNextPageBtnClick: this.switchToNextPage,
onPrevPageBtnClick: this.switchToPrevPage,
onMinuteClick: this.handleChange,
hovered: this.state.hoveredCellPosition,
onCellHover: this.onHoveredCellPositionChange,
onBlur: this.handleBlur,
inline: this.props.inline,
onMount: this.props.onCalendarViewMount,
hasNextPage: this.isNextPageAvailable(),
hasPrevPage: this.isPrevPageAvailable(),
currentDate: this.getCurrentDate(),
active: this.getActiveCellPosition()
}));
}
}]);
return MinutePicker;
}(_BasePicker2.default);
_defineProperty(MinutePicker, "handledProps", ["closePopup", "disable", "initializeWith", "inline", "isPickerInFocus", "isTriggerInFocus", "maxDate", "minDate", "onCalendarViewMount", "onChange", "timeFormat", "value"]);
MinutePicker.propTypes = {
/** Called after minute is selected. */
onChange: _propTypes.default.func.isRequired,
/** A value for initializing minute picker's state. */
initializeWith: _propTypes.default.instanceOf(_moment.default).isRequired,
/** Currently selected minute. */
value: _propTypes.default.instanceOf(_moment.default),
/** Array of disabled dates. */
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
};
MinutePicker.defaultProps = {
timeFormat: '24'
};
var _default = MinutePicker;
exports.default = _default;
;