UNPKG

awesome-react-datepicker

Version:

React datepicker component.

233 lines (186 loc) 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _constants = require("../utils/constants"); var _CalendarRow = _interopRequireDefault(require("./CalendarRow")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } 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 Calendar = /*#__PURE__*/ function (_Component) { _inherits(Calendar, _Component); function Calendar(props) { var _this; _classCallCheck(this, Calendar); _this = _possibleConstructorReturn(this, _getPrototypeOf(Calendar).call(this, props)); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "asyncSetState", function (obj) { return new Promise(function (resolve) { _this.setState(obj, resolve); }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "updateDateDistribution", /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(year, month) { var _this$state, dayOneIndex, dateDistMap, dateDistMapInverse, k, value; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _this$state = _this.state, dayOneIndex = _this$state.dayOneIndex, dateDistMap = _this$state.dateDistMap, dateDistMapInverse = _this$state.dateDistMapInverse; // Loop for 42 Times - for 42 Box-es for (k = 0; k < 42; k++) { value = new Date(year, month, k - dayOneIndex + 1).valueOf(); dateDistMap[k] = value; dateDistMapInverse[value] = k; } _context.next = 4; return _this.asyncSetState({ dateDistMap: dateDistMap, dateDistMapInverse: dateDistMapInverse }); case 4: case "end": return _context.stop(); } } }, _callee, this); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); _this.state = { dayOneIndex: 0, // Index of Day-1 of current month dateDistMap: {}, // Keeps track of dates-of-month by calendar-index dateDistMapInverse: {} // Keeps track of calendar-index by date-timestamps }; return _this; } _createClass(Calendar, [{ key: "componentWillReceiveProps", value: function () { var _componentWillReceiveProps = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2(nextProps) { var year, month, dayOneIndex, tempDayOne; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: year = nextProps.year, month = nextProps.month; dayOneIndex = this.state.dayOneIndex; tempDayOne = new Date(year, month, 1).getDay(); if (!(tempDayOne !== dayOneIndex)) { _context2.next = 6; break; } _context2.next = 6; return this.asyncSetState({ dayOneIndex: tempDayOne }); case 6: _context2.next = 8; return this.updateDateDistribution(year, month); case 8: case "end": return _context2.stop(); } } }, _callee2, this); })); function componentWillReceiveProps(_x3) { return _componentWillReceiveProps.apply(this, arguments); } return componentWillReceiveProps; }() }, { key: "componentDidMount", value: function () { var _componentDidMount = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee3() { var _this$props, year, month; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _this$props = this.props, year = _this$props.year, month = _this$props.month; _context3.next = 3; return this.asyncSetState({ dayOneIndex: new Date(year, month, 1).getDay() }); case 3: _context3.next = 5; return this.updateDateDistribution(this.props.year, this.props.month); case 5: case "end": return _context3.stop(); } } }, _callee3, this); })); function componentDidMount() { return _componentDidMount.apply(this, arguments); } return componentDidMount; }() }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, year = _this$props2.year, month = _this$props2.month; var numDatesThis = new Date(year, month, 0).getDate(); var isFiveRows = this.state.dayOneIndex + numDatesThis <= 35; var rowArr = isFiveRows ? _constants.fiveLengthArr : _constants.sixLengthArr; // console.log('initDate', this.props.initDate.valueOf()); var _this$props$customSty = this.props.customStyles, borderColor = _this$props$customSty.borderColor, textColor = _this$props$customSty.textColor, borderRadius = _this$props$customSty.borderRadius, backgroundColor = _this$props$customSty.backgroundColor; return React.createElement("div", { className: "awesome-react-datepicker-_Calendar", style: { borderRadius: borderRadius ? borderRadius : '3px', background: backgroundColor ? backgroundColor : 'white' } }, rowArr.map(function (x, i) { return React.createElement(_CalendarRow.default, { key: i, index: i, year: year, month: month, isFiveRows: isFiveRows, dateDistMap: _this2.state.dateDistMap, dateDistMapInverse: _this2.state.dateDistMapInverse, onDateSelect: _this2.props.onDateSelect, initDate: _this2.props.initDate, customStyles: _this2.props.customStyles }); })); } }]); return Calendar; }(React.Component); var _default = Calendar; exports.default = _default;