UNPKG

primereact

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primereact.svg)](https://badge.fury.io/js/primereact) [![Discord Chat](https://img.shields.io/discord/5579

1,255 lines (1,072 loc) 122 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Calendar = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _InputText = require("../inputtext/InputText"); var _Button = require("../button/Button"); var _CalendarPanel = require("./CalendarPanel"); var _DomHandler = _interopRequireDefault(require("../utils/DomHandler")); var _ClassNames = require("../utils/ClassNames"); var _Tooltip = require("../tooltip/Tooltip"); var _Ripple = require("../ripple/Ripple"); var _UniqueComponentId = _interopRequireDefault(require("../utils/UniqueComponentId")); var _ConnectedOverlayScrollHandler = _interopRequireDefault(require("../utils/ConnectedOverlayScrollHandler")); var _Locale = require("../api/Locale"); var _OverlayEventBus = _interopRequireDefault(require("../overlayeventbus/OverlayEventBus")); var _Mask = require("../utils/Mask"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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 _typeof(obj) { "@babel/helpers - typeof"; 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 _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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); var _super = _createSuper(Calendar); function Calendar(props) { var _this; _classCallCheck(this, Calendar); _this = _super.call(this, props); _this.state = { focused: false, overlayVisible: false }; if (!_this.props.onViewDateChange) { var propValue = _this.props.value; if (Array.isArray(propValue)) { propValue = propValue[0]; } var viewDate = _this.props.viewDate && _this.isValidDate(_this.props.viewDate) ? _this.props.viewDate : propValue && _this.isValidDate(propValue) ? propValue : new Date(); _this.validateDate(viewDate); _this.state = _objectSpread(_objectSpread({}, _this.state), {}, { viewDate: viewDate }); } _this.navigation = null; _this.onUserInput = _this.onUserInput.bind(_assertThisInitialized(_this)); _this.onInputFocus = _this.onInputFocus.bind(_assertThisInitialized(_this)); _this.onInputBlur = _this.onInputBlur.bind(_assertThisInitialized(_this)); _this.onInputKeyDown = _this.onInputKeyDown.bind(_assertThisInitialized(_this)); _this.onButtonClick = _this.onButtonClick.bind(_assertThisInitialized(_this)); _this.onPrevButtonClick = _this.onPrevButtonClick.bind(_assertThisInitialized(_this)); _this.onNextButtonClick = _this.onNextButtonClick.bind(_assertThisInitialized(_this)); _this.onMonthDropdownChange = _this.onMonthDropdownChange.bind(_assertThisInitialized(_this)); _this.onYearDropdownChange = _this.onYearDropdownChange.bind(_assertThisInitialized(_this)); _this.onTodayButtonClick = _this.onTodayButtonClick.bind(_assertThisInitialized(_this)); _this.onClearButtonClick = _this.onClearButtonClick.bind(_assertThisInitialized(_this)); _this.onPanelClick = _this.onPanelClick.bind(_assertThisInitialized(_this)); _this.incrementHour = _this.incrementHour.bind(_assertThisInitialized(_this)); _this.decrementHour = _this.decrementHour.bind(_assertThisInitialized(_this)); _this.incrementMinute = _this.incrementMinute.bind(_assertThisInitialized(_this)); _this.decrementMinute = _this.decrementMinute.bind(_assertThisInitialized(_this)); _this.incrementSecond = _this.incrementSecond.bind(_assertThisInitialized(_this)); _this.decrementSecond = _this.decrementSecond.bind(_assertThisInitialized(_this)); _this.toggleAmPm = _this.toggleAmPm.bind(_assertThisInitialized(_this)); _this.onTimePickerElementMouseDown = _this.onTimePickerElementMouseDown.bind(_assertThisInitialized(_this)); _this.onTimePickerElementMouseUp = _this.onTimePickerElementMouseUp.bind(_assertThisInitialized(_this)); _this.onTimePickerElementMouseLeave = _this.onTimePickerElementMouseLeave.bind(_assertThisInitialized(_this)); _this.onOverlayEnter = _this.onOverlayEnter.bind(_assertThisInitialized(_this)); _this.onOverlayEntered = _this.onOverlayEntered.bind(_assertThisInitialized(_this)); _this.onOverlayExit = _this.onOverlayExit.bind(_assertThisInitialized(_this)); _this.onOverlayExited = _this.onOverlayExited.bind(_assertThisInitialized(_this)); _this.reFocusInputField = _this.reFocusInputField.bind(_assertThisInitialized(_this)); _this.id = _this.props.id || (0, _UniqueComponentId.default)(); _this.overlayRef = /*#__PURE__*/_react.default.createRef(); return _this; } _createClass(Calendar, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; if (this.props.tooltip) { this.renderTooltip(); } if (this.props.inline) { this.initFocusableCell(); } else if (this.props.mask) { (0, _Mask.mask)(this.inputElement, { mask: this.props.mask, readOnly: this.props.readOnlyInput || this.props.disabled, onChange: function onChange(e) { return _this2.updateValueOnInput(e.originalEvent, e.value); } }); } if (this.props.value) { this.updateInputfield(this.props.value); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { var _this3 = this; if (prevProps.tooltip !== this.props.tooltip || prevProps.tooltipOptions !== this.props.tooltipOptions) { if (this.tooltip) this.tooltip.update(_objectSpread({ content: this.props.tooltip }, this.props.tooltipOptions || {}));else this.renderTooltip(); } if (!this.props.onViewDateChange && !this.viewStateChanged) { var propValue = this.props.value; if (Array.isArray(propValue)) { propValue = propValue[0]; } var prevPropValue = prevProps.value; if (Array.isArray(prevPropValue)) { prevPropValue = prevPropValue[0]; } if (!prevPropValue && propValue || propValue && propValue instanceof Date && propValue.getTime() !== prevPropValue.getTime()) { var viewDate = this.props.viewDate && this.isValidDate(this.props.viewDate) ? this.props.viewDate : propValue && this.isValidDate(propValue) ? propValue : new Date(); this.validateDate(viewDate); this.setState({ viewDate: viewDate }, function () { _this3.viewStateChanged = true; }); } } if (this.overlayRef && this.overlayRef.current) { this.updateFocus(); } if (prevProps.value !== this.props.value && (!this.viewStateChanged || !this.state.overlayVisible) || this.isOptionChanged(prevProps)) { this.updateInputfield(this.props.value); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.hideTimeout) { clearTimeout(this.hideTimeout); } if (this.touchUIMask) { this.disableModality(); this.touchUIMask = null; } if (this.tooltip) { this.tooltip.destroy(); this.tooltip = null; } this.unbindDocumentClickListener(); this.unbindDocumentResizeListener(); if (this.scrollHandler) { this.scrollHandler.destroy(); this.scrollHandler = null; } _DomHandler.default.revertZIndex(); } }, { key: "renderTooltip", value: function renderTooltip() { this.tooltip = (0, _Tooltip.tip)({ target: this.inputElement, content: this.props.tooltip, options: this.props.tooltipOptions }); } }, { key: "isOptionChanged", value: function isOptionChanged(prevProps) { var _this4 = this; var optionProps = ['dateFormat', 'hourFormat', 'timeOnly', 'showSeconds', 'showMillisec']; return optionProps.some(function (option) { return prevProps[option] !== _this4.props[option]; }); } }, { key: "getDateFormat", value: function getDateFormat() { return this.props.dateFormat || (0, _Locale.localeOption)('dateFormat', this.props.locale); } }, { key: "onInputFocus", value: function onInputFocus(event) { var _this5 = this; if (this.ignoreFocusFunctionality) { this.setState({ focused: true }, function () { _this5.ignoreFocusFunctionality = false; }); } else { event.persist(); if (this.props.showOnFocus && !this.state.overlayVisible) { this.showOverlay(); } this.setState({ focused: true }, function () { if (_this5.props.onFocus) { _this5.props.onFocus(event); } }); } } }, { key: "onInputBlur", value: function onInputBlur(event) { var _this6 = this; event.persist(); this.setState({ focused: false }, function () { if (_this6.props.onBlur) { _this6.props.onBlur(event); } if (!_this6.props.keepInvalid) { _this6.updateInputfield(_this6.props.value); } }); } }, { key: "onInputKeyDown", value: function onInputKeyDown(event) { this.isKeydown = true; switch (event.which) { //escape case 27: { this.hideOverlay(); break; } //tab case 9: { if (this.state.overlayVisible) { this.trapFocus(event); } if (this.props.touchUI) { this.disableModality(); } break; } default: //no op break; } } }, { key: "onUserInput", value: function onUserInput(event) { // IE 11 Workaround for input placeholder if (!this.isKeydown) { return; } this.isKeydown = false; this.updateValueOnInput(event, event.target.value); if (this.props.onInput) { this.props.onInput(event); } } }, { key: "updateValueOnInput", value: function updateValueOnInput(event, rawValue) { try { var value = this.parseValueFromString(rawValue); if (this.isValidSelection(value)) { this.updateModel(event, value); this.updateViewDate(event, value.length ? value[0] : value); } } catch (err) { //invalid date var _value = this.props.keepInvalid ? rawValue : null; this.updateModel(event, _value); } } }, { key: "reFocusInputField", value: function reFocusInputField() { if (!this.props.inline && this.inputElement) { this.ignoreFocusFunctionality = true; this.inputElement.focus(); } } }, { key: "isValidSelection", value: function isValidSelection(value) { var _this7 = this; var isValid = true; if (this.isSingleSelection()) { if (!(this.isSelectable(value.getDate(), value.getMonth(), value.getFullYear(), false) && this.isSelectableTime(value))) { isValid = false; } } else if (value.every(function (v) { return _this7.isSelectable(v.getDate(), v.getMonth(), v.getFullYear(), false) && _this7.isSelectableTime(value); })) { if (this.isRangeSelection()) { isValid = value.length > 1 && value[1] > value[0] ? true : false; } } return isValid; } }, { key: "onButtonClick", value: function onButtonClick() { if (this.state.overlayVisible) { this.hideOverlay(); } else { this.showOverlay(); } } }, { key: "onPrevButtonClick", value: function onPrevButtonClick(event) { this.navigation = { backward: true, button: true }; this.navBackward(event); } }, { key: "onNextButtonClick", value: function onNextButtonClick(event) { this.navigation = { backward: false, button: true }; this.navForward(event); } }, { key: "onContainerButtonKeydown", value: function onContainerButtonKeydown(event) { switch (event.which) { //tab case 9: this.trapFocus(event); break; //escape case 27: this.hideOverlay(this.reFocusInputField); event.preventDefault(); break; default: //Noop break; } } }, { key: "trapFocus", value: function trapFocus(event) { event.preventDefault(); var focusableElements = _DomHandler.default.getFocusableElements(this.overlayRef.current); if (focusableElements && focusableElements.length > 0) { if (!document.activeElement) { focusableElements[0].focus(); } else { var focusedIndex = focusableElements.indexOf(document.activeElement); if (event.shiftKey) { if (focusedIndex === -1 || focusedIndex === 0) focusableElements[focusableElements.length - 1].focus();else focusableElements[focusedIndex - 1].focus(); } else { if (focusedIndex === -1 || focusedIndex === focusableElements.length - 1) focusableElements[0].focus();else focusableElements[focusedIndex + 1].focus(); } } } } }, { key: "updateFocus", value: function updateFocus() { var cell; if (this.navigation) { if (this.navigation.button) { this.initFocusableCell(); if (this.navigation.backward) _DomHandler.default.findSingle(this.overlayRef.current, '.p-datepicker-prev').focus();else _DomHandler.default.findSingle(this.overlayRef.current, '.p-datepicker-next').focus(); } else { if (this.navigation.backward) { var cells = _DomHandler.default.find(this.overlayRef.current, '.p-datepicker-calendar td span:not(.p-disabled)'); cell = cells[cells.length - 1]; } else { cell = _DomHandler.default.findSingle(this.overlayRef.current, '.p-datepicker-calendar td span:not(.p-disabled)'); } if (cell) { cell.tabIndex = '0'; cell.focus(); } } this.navigation = null; } else { this.initFocusableCell(); } } }, { key: "initFocusableCell", value: function initFocusableCell() { var cell; if (this.view === 'month') { var cells = _DomHandler.default.find(this.overlayRef.current, '.p-monthpicker .p-monthpicker-month'); var selectedCell = _DomHandler.default.findSingle(this.overlayRef.current, '.p-monthpicker .p-monthpicker-month.p-highlight'); cells.forEach(function (cell) { return cell.tabIndex = -1; }); cell = selectedCell || cells[0]; } else { cell = _DomHandler.default.findSingle(this.overlayRef.current, 'span.p-highlight'); if (!cell) { var todayCell = _DomHandler.default.findSingle(this.overlayRef.current, 'td.p-datepicker-today span:not(.p-disabled)'); if (todayCell) cell = todayCell;else cell = _DomHandler.default.findSingle(this.overlayRef.current, '.p-datepicker-calendar td span:not(.p-disabled)'); } } if (cell) { cell.tabIndex = '0'; } } }, { key: "navBackward", value: function navBackward(event) { if (this.props.disabled) { event.preventDefault(); return; } var newViewDate = new Date(this.getViewDate().getTime()); newViewDate.setDate(1); if (this.props.view === 'date') { if (newViewDate.getMonth() === 0) { newViewDate.setMonth(11); newViewDate.setFullYear(newViewDate.getFullYear() - 1); } else { newViewDate.setMonth(newViewDate.getMonth() - 1); } } else if (this.props.view === 'month') { var currentYear = newViewDate.getFullYear(); var newYear = currentYear - 1; if (this.props.yearNavigator) { var minYear = parseInt(this.props.yearRange.split(':')[0], 10); if (newYear < minYear) { newYear = minYear; } } newViewDate.setFullYear(newYear); } this.updateViewDate(event, newViewDate); event.preventDefault(); } }, { key: "navForward", value: function navForward(event) { if (this.props.disabled) { event.preventDefault(); return; } var newViewDate = new Date(this.getViewDate().getTime()); newViewDate.setDate(1); if (this.props.view === 'date') { if (newViewDate.getMonth() === 11) { newViewDate.setMonth(0); newViewDate.setFullYear(newViewDate.getFullYear() + 1); } else { newViewDate.setMonth(newViewDate.getMonth() + 1); } } else if (this.props.view === 'month') { var currentYear = newViewDate.getFullYear(); var newYear = currentYear + 1; if (this.props.yearNavigator) { var maxYear = parseInt(this.props.yearRange.split(':')[1], 10); if (newYear > maxYear) { newYear = maxYear; } } newViewDate.setFullYear(newYear); } this.updateViewDate(event, newViewDate); event.preventDefault(); } }, { key: "onMonthDropdownChange", value: function onMonthDropdownChange(event) { var currentViewDate = this.getViewDate(); var newViewDate = new Date(currentViewDate.getTime()); newViewDate.setMonth(parseInt(event.target.value, 10)); this.updateViewDate(event, newViewDate); } }, { key: "onYearDropdownChange", value: function onYearDropdownChange(event) { var currentViewDate = this.getViewDate(); var newViewDate = new Date(currentViewDate.getTime()); newViewDate.setFullYear(parseInt(event.target.value, 10)); this.updateViewDate(event, newViewDate); } }, { key: "onTodayButtonClick", value: function onTodayButtonClick(event) { var today = new Date(); var dateMeta = { day: today.getDate(), month: today.getMonth(), year: today.getFullYear(), today: true, selectable: true }; var timeMeta = { hours: today.getHours(), minutes: today.getMinutes(), seconds: today.getSeconds(), milliseconds: today.getMilliseconds() }; this.updateViewDate(event, today); this.onDateSelect(event, dateMeta, timeMeta); if (this.props.onTodayButtonClick) { this.props.onTodayButtonClick(event); } } }, { key: "onClearButtonClick", value: function onClearButtonClick(event) { this.updateModel(event, null); this.updateInputfield(null); this.hideOverlay(this.reFocusInputField); if (this.props.onClearButtonClick) { this.props.onClearButtonClick(event); } } }, { key: "onPanelClick", value: function onPanelClick(event) { if (!this.props.inline) { _OverlayEventBus.default.emit('overlay-click', { originalEvent: event, target: this.container }); } } }, { key: "onTimePickerElementMouseDown", value: function onTimePickerElementMouseDown(event, type, direction) { if (!this.props.disabled) { this.repeat(event, null, type, direction); event.preventDefault(); } } }, { key: "onTimePickerElementMouseUp", value: function onTimePickerElementMouseUp() { if (!this.props.disabled) { this.clearTimePickerTimer(); } } }, { key: "onTimePickerElementMouseLeave", value: function onTimePickerElementMouseLeave() { if (!this.props.disabled) { this.clearTimePickerTimer(); } } }, { key: "repeat", value: function repeat(event, interval, type, direction) { var _this8 = this; event.persist(); var i = interval || 500; this.clearTimePickerTimer(); this.timePickerTimer = setTimeout(function () { _this8.repeat(event, 100, type, direction); }, i); switch (type) { case 0: if (direction === 1) this.incrementHour(event);else this.decrementHour(event); break; case 1: if (direction === 1) this.incrementMinute(event);else this.decrementMinute(event); break; case 2: if (direction === 1) this.incrementSecond(event);else this.decrementSecond(event); break; case 3: if (direction === 1) this.incrementMilliSecond(event);else this.decrementMilliSecond(event); break; default: break; } } }, { key: "clearTimePickerTimer", value: function clearTimePickerTimer() { if (this.timePickerTimer) { clearTimeout(this.timePickerTimer); } } }, { key: "incrementHour", value: function incrementHour(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentHour = currentTime.getHours(); var newHour = currentHour + this.props.stepHour; newHour = newHour >= 24 ? newHour - 24 : newHour; if (this.validateHour(newHour, currentTime)) { if (this.props.maxDate && this.props.maxDate.toDateString() === currentTime.toDateString() && this.props.maxDate.getHours() === newHour) { if (this.props.maxDate.getMinutes() < currentTime.getMinutes()) { if (this.props.maxDate.getSeconds() < currentTime.getSeconds()) { if (this.props.maxDate.getMilliseconds() < currentTime.getMilliseconds()) { this.updateTime(event, newHour, this.props.maxDate.getMinutes(), this.props.maxDate.getSeconds(), this.props.maxDate.getMilliseconds()); } else { this.updateTime(event, newHour, this.props.maxDate.getMinutes(), this.props.maxDate.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, this.props.maxDate.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } else if (this.props.maxDate.getMinutes() === currentTime.getMinutes()) { if (this.props.maxDate.getSeconds() < currentTime.getSeconds()) { if (this.props.maxDate.getMilliseconds() < currentTime.getMilliseconds()) { this.updateTime(event, newHour, this.props.maxDate.getMinutes(), this.props.maxDate.getSeconds(), this.props.maxDate.getMilliseconds()); } else { this.updateTime(event, newHour, this.props.maxDate.getMinutes(), this.props.maxDate.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, this.props.maxDate.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, currentTime.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, currentTime.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } event.preventDefault(); } }, { key: "decrementHour", value: function decrementHour(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentHour = currentTime.getHours(); var newHour = currentHour - this.props.stepHour; newHour = newHour < 0 ? newHour + 24 : newHour; if (this.validateHour(newHour, currentTime)) { if (this.props.minDate && this.props.minDate.toDateString() === currentTime.toDateString() && this.props.minDate.getHours() === newHour) { if (this.props.minDate.getMinutes() > currentTime.getMinutes()) { if (this.props.minDate.getSeconds() > currentTime.getSeconds()) { if (this.props.minDate.getMilliseconds() > currentTime.getMilliseconds()) { this.updateTime(event, newHour, this.props.minDate.getMinutes(), this.props.minDate.getSeconds(), this.props.minDate.getMilliseconds()); } else { this.updateTime(event, newHour, this.props.minDate.getMinutes(), this.props.minDate.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, this.props.minDate.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } else if (this.props.minDate.getMinutes() === currentTime.getMinutes()) { if (this.props.minDate.getSeconds() > currentTime.getSeconds()) { if (this.props.minDate.getMilliseconds() > currentTime.getMilliseconds()) { this.updateTime(event, newHour, this.props.minDate.getMinutes(), this.props.minDate.getSeconds(), this.props.minDate.getMilliseconds()); } else { this.updateTime(event, newHour, this.props.minDate.getMinutes(), this.props.minDate.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, this.props.minDate.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, currentTime.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, newHour, currentTime.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); } } event.preventDefault(); } }, { key: "incrementMinute", value: function incrementMinute(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentMinute = currentTime.getMinutes(); var newMinute = currentMinute + this.props.stepMinute; newMinute = newMinute > 59 ? newMinute - 60 : newMinute; if (this.validateMinute(newMinute, currentTime)) { if (this.props.maxDate && this.props.maxDate.toDateString() === currentTime.toDateString() && this.props.maxDate.getMinutes() === newMinute) { if (this.props.maxDate.getSeconds() < currentTime.getSeconds()) { if (this.props.maxDate.getMilliseconds() < currentTime.getMilliseconds()) { this.updateTime(event, currentTime.getHours(), newMinute, this.props.maxDate.getSeconds(), this.props.maxDate.getMilliseconds()); } else { this.updateTime(event, currentTime.getHours(), newMinute, this.props.maxDate.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, currentTime.getHours(), newMinute, currentTime.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, currentTime.getHours(), newMinute, currentTime.getSeconds(), currentTime.getMilliseconds()); } } event.preventDefault(); } }, { key: "decrementMinute", value: function decrementMinute(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentMinute = currentTime.getMinutes(); var newMinute = currentMinute - this.props.stepMinute; newMinute = newMinute < 0 ? newMinute + 60 : newMinute; if (this.validateMinute(newMinute, currentTime)) { if (this.props.minDate && this.props.minDate.toDateString() === currentTime.toDateString() && this.props.minDate.getMinutes() === newMinute) { if (this.props.minDate.getSeconds() > currentTime.getSeconds()) { if (this.props.minDate.getMilliseconds() > currentTime.getMilliseconds()) { this.updateTime(event, currentTime.getHours(), newMinute, this.props.minDate.getSeconds(), this.props.minDate.getMilliseconds()); } else { this.updateTime(event, currentTime.getHours(), newMinute, this.props.minDate.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, currentTime.getHours(), newMinute, currentTime.getSeconds(), currentTime.getMilliseconds()); } } else { this.updateTime(event, currentTime.getHours(), newMinute, currentTime.getSeconds(), currentTime.getMilliseconds()); } } event.preventDefault(); } }, { key: "incrementSecond", value: function incrementSecond(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentSecond = currentTime.getSeconds(); var newSecond = currentSecond + this.props.stepSecond; newSecond = newSecond > 59 ? newSecond - 60 : newSecond; if (this.validateSecond(newSecond, currentTime)) { if (this.props.maxDate && this.props.maxDate.toDateString() === currentTime.toDateString() && this.props.maxDate.getSeconds() === newSecond) { if (this.props.maxDate.getMilliseconds() < currentTime.getMilliseconds()) { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), newSecond, this.props.maxDate.getMilliseconds()); } else { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), newSecond, currentTime.getMilliseconds()); } } else { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), newSecond, currentTime.getMilliseconds()); } } event.preventDefault(); } }, { key: "decrementSecond", value: function decrementSecond(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentSecond = currentTime.getSeconds(); var newSecond = currentSecond - this.props.stepSecond; newSecond = newSecond < 0 ? newSecond + 60 : newSecond; if (this.validateSecond(newSecond, currentTime)) { if (this.props.minDate && this.props.minDate.toDateString() === currentTime.toDateString() && this.props.minDate.getSeconds() === newSecond) { if (this.props.minDate.getMilliseconds() > currentTime.getMilliseconds()) { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), newSecond, this.props.minDate.getMilliseconds()); } else { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), newSecond, currentTime.getMilliseconds()); } } else { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), newSecond, currentTime.getMilliseconds()); } } event.preventDefault(); } }, { key: "incrementMilliSecond", value: function incrementMilliSecond(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentMillisecond = currentTime.getMilliseconds(); var newMillisecond = currentMillisecond + this.props.stepMillisec; newMillisecond = newMillisecond > 999 ? newMillisecond - 1000 : newMillisecond; if (this.validateMillisecond(newMillisecond, currentTime)) { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), currentTime.getSeconds(), newMillisecond); } event.preventDefault(); } }, { key: "decrementMilliSecond", value: function decrementMilliSecond(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentMillisecond = currentTime.getMilliseconds(); var newMillisecond = currentMillisecond - this.props.stepMillisec; newMillisecond = newMillisecond < 0 ? newMillisecond + 999 : newMillisecond; if (this.validateMillisecond(newMillisecond, currentTime)) { this.updateTime(event, currentTime.getHours(), currentTime.getMinutes(), currentTime.getSeconds(), newMillisecond); } event.preventDefault(); } }, { key: "toggleAmPm", value: function toggleAmPm(event) { var currentTime = this.props.value && this.props.value instanceof Date ? this.props.value : this.getViewDate(); var currentHour = currentTime.getHours(); var newHour = currentHour >= 12 ? currentHour - 12 : currentHour + 12; this.updateTime(event, newHour, currentTime.getMinutes(), currentTime.getSeconds(), currentTime.getMilliseconds()); event.preventDefault(); } }, { key: "getViewDate", value: function getViewDate() { return this.props.onViewDateChange ? this.props.viewDate : this.state.viewDate; } }, { key: "isValidDate", value: function isValidDate(date) { return date instanceof Date && !isNaN(date); } }, { key: "validateHour", value: function validateHour(hour, value) { var valid = true; var valueDateString = value ? value.toDateString() : null; if (this.props.minDate && valueDateString && this.props.minDate.toDateString() === valueDateString) { if (this.props.minDate.getHours() > hour) { valid = false; } } if (this.props.maxDate && valueDateString && this.props.maxDate.toDateString() === valueDateString) { if (this.props.maxDate.getHours() < hour) { valid = false; } } return valid; } }, { key: "validateMinute", value: function validateMinute(minute, value) { var valid = true; var valueDateString = value ? value.toDateString() : null; if (this.props.minDate && valueDateString && this.props.minDate.toDateString() === valueDateString) { if (value.getHours() === this.props.minDate.getHours()) { if (this.props.minDate.getMinutes() > minute) { valid = false; } } } if (this.props.maxDate && valueDateString && this.props.maxDate.toDateString() === valueDateString) { if (value.getHours() === this.props.maxDate.getHours()) { if (this.props.maxDate.getMinutes() < minute) { valid = false; } } } return valid; } }, { key: "validateSecond", value: function validateSecond(second, value) { var valid = true; var valueDateString = value ? value.toDateString() : null; if (this.props.minDate && valueDateString && this.props.minDate.toDateString() === valueDateString) { if (value.getHours() === this.props.minDate.getHours() && value.getMinutes() === this.props.minDate.getMinutes()) { if (this.props.minDate.getSeconds() > second) { valid = false; } } } if (this.props.maxDate && valueDateString && this.props.maxDate.toDateString() === valueDateString) { if (value.getHours() === this.props.maxDate.getHours() && value.getMinutes() === this.props.maxDate.getMinutes()) { if (this.props.maxDate.getSeconds() < second) { valid = false; } } } return valid; } }, { key: "validateMillisecond", value: function validateMillisecond(millisecond, value) { var valid = true; var valueDateString = value ? value.toDateString() : null; if (this.props.minDate && valueDateString && this.props.minDate.toDateString() === valueDateString) { if (value.getHours() === this.props.minDate.getHours() && value.getSeconds() === this.props.minDate.getSeconds() && value.getMinutes() === this.props.minDate.getMinutes()) { if (this.props.minDate.getMilliseconds() > millisecond) { valid = false; } } } if (this.props.maxDate && valueDateString && this.props.maxDate.toDateString() === valueDateString) { if (value.getHours() === this.props.maxDate.getHours() && value.getSeconds() === this.props.maxDate.getSeconds() && value.getMinutes() === this.props.maxDate.getMinutes()) { if (this.props.maxDate.getMilliseconds() < millisecond) { valid = false; } } } return valid; } }, { key: "validateDate", value: function validateDate(value) { if (this.props.yearNavigator) { var viewYear = value.getFullYear(); var minRangeYear = this.props.yearRange ? parseInt(this.props.yearRange.split(':')[0], 10) : null; var maxRangeYear = this.props.yearRange ? parseInt(this.props.yearRange.split(':')[1], 10) : null; var minYear = this.props.minDate && minRangeYear != null ? Math.max(this.props.minDate.getFullYear(), minRangeYear) : this.props.minDate || minRangeYear; var maxYear = this.props.maxDate && maxRangeYear != null ? Math.min(this.props.maxDate.getFullYear(), maxRangeYear) : this.props.maxDate || maxRangeYear; if (minYear && minYear > viewYear) { viewYear = minYear; } if (maxYear && maxYear < viewYear) { viewYear = maxYear; } value.setFullYear(viewYear); } if (this.props.monthNavigator && this.props.view !== 'month') { var viewMonth = value.getMonth(); var viewMonthWithMinMax = parseInt(this.isInMinYear(value) && Math.max(this.props.minDate.getMonth(), viewMonth).toString() || this.isInMaxYear(value) && Math.min(this.props.maxDate.getMonth(), viewMonth).toString() || viewMonth); value.setMonth(viewMonthWithMinMax); } } }, { key: "updateTime", value: function updateTime(event, hour, minute, second, millisecond) { var newDateTime = this.props.value && this.props.value instanceof Date ? new Date(this.props.value) : new Date(); newDateTime.setHours(hour); newDateTime.setMinutes(minute); newDateTime.setSeconds(second); newDateTime.setMilliseconds(millisecond); this.updateModel(event, newDateTime); if (this.props.onSelect) { this.props.onSelect({ originalEvent: event, value: newDateTime }); } this.updateInputfield(newDateTime); } }, { key: "updateViewDate", value: function updateViewDate(event, value) { this.validateDate(value); if (this.props.onViewDateChange) { this.props.onViewDateChange({ originalEvent: event, value: value }); } else { this.viewStateChanged = true; this.setState({ viewDate: value }); } } }, { key: "onDateCellKeydown", value: function onDateCellKeydown(event, date, groupIndex) { var cellContent = event.currentTarget; var cell = cellContent.parentElement; switch (event.which) { //down arrow case 40: { cellContent.tabIndex = '-1'; var cellIndex = _DomHandler.default.index(cell); var nextRow = cell.parentElement.nextElementSibling; if (nextRow) { var focusCell = nextRow.children[cellIndex].children[0]; if (_DomHandler.default.hasClass(focusCell, 'p-disabled')) { this.navigation = { backward: false }; this.navForward(event); } else { nextRow.children[cellIndex].children[0].tabIndex = '0'; nextRow.children[cellIndex].children[0].focus(); } } else { this.navigation = { backward: false }; this.navForward(event); } event.preventDefault(); break; } //up arrow case 38: { cellContent.tabIndex = '-1'; var _cellIndex = _DomHandler.default.index(cell); var prevRow = cell.parentElement.previousElementSibling; if (prevRow) { var _focusCell = prevRow.children[_cellIndex].children[0]; if (_DomHandler.default.hasClass(_focusCell, 'p-disabled')) { this.navigation = { backward: true }; this.navBackward(event); } else { _focusCell.tabIndex = '0'; _focusCell.focus(); } } else { this.navigation = { backward: true }; this.navBackward(event); } event.preventDefault(); break; } //left arrow case 37: { cellContent.tabIndex = '-1'; var prevCell = cell.previousElementSibling; if (prevCell) { var _focusCell2 = prevCell.children[0]; if (_DomHandler.default.hasClass(_focusCell2, 'p-disabled')) { this.navigateToMonth(true, groupIndex, event); } else { _focusCell2.tabIndex = '0'; _focusCell2.focus(); } } else { this.navigateToMonth(true, groupIndex, event); } event.preventDefault(); break; } //right arrow case 39: { cellContent.tabIndex = '-1'; var nextCell = cell.nextElementSibling; if (nextCell) { var _focusCell3 = nextCell.children[0]; if (_DomHandler.default.hasClass(_focusCell3, 'p-disabled')) { this.navigateToMonth(false, groupIndex, event); } else { _focusCell3.tabIndex = '0'; _focusCell3.focus(); } } else { this.navigateToMonth(false, groupIndex, event); } event.preventDefault(); break; } //enter case 13: { this.onDateSelect(event, date); event.preventDefault(); break; } //escape case 27: { this.hideOverlay(this.reFocusInputField); event.preventDefault(); break; } //tab case 9: { this.trapFocus(event); break; } default: //no op break; } } }, { key: "nav