@elastic/eui
Version:
Elastic UI Component Library
217 lines (215 loc) • 9.5 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 HackerOne Inc and individual contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
import React from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import { getDay, getMonth, getDate, now, isSameDay, isDayDisabled, isDayInRange, getDayOfWeekCode } from "./date_utils";
import { jsx as ___EmotionJSX } from "@emotion/react";
var Day = /*#__PURE__*/function (_React$Component) {
function Day() {
var _this;
_classCallCheck(this, Day);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, Day, [].concat(args));
_defineProperty(_this, "handleClick", function (event) {
if (!_this.isDisabled() && _this.props.onClick) {
_this.props.onClick(event);
}
});
_defineProperty(_this, "handleMouseEnter", function (event) {
if (!_this.isDisabled() && _this.props.onMouseEnter) {
_this.props.onMouseEnter(event);
}
});
_defineProperty(_this, "isSameDay", function (other) {
return isSameDay(_this.props.day, other);
});
_defineProperty(_this, "isKeyboardSelected", function () {
return !_this.props.disabledKeyboardNavigation && (!_this.props.inline || _this.props.accessibleMode) && !_this.isSameDay(_this.props.selected) && _this.isSameDay(_this.props.preSelection);
});
_defineProperty(_this, "isDisabled", function () {
return isDayDisabled(_this.props.day, _this.props);
});
_defineProperty(_this, "getHighLightedClass", function (defaultClassName) {
var _this$props = _this.props,
day = _this$props.day,
highlightDates = _this$props.highlightDates;
if (!highlightDates) {
return false;
}
// Looking for className in the Map of {'day string, 'className'}
var dayStr = day.format("MM.DD.YYYY");
return highlightDates.get(dayStr);
});
_defineProperty(_this, "isInRange", function () {
var _this$props2 = _this.props,
day = _this$props2.day,
startDate = _this$props2.startDate,
endDate = _this$props2.endDate;
if (!startDate || !endDate) {
return false;
}
return isDayInRange(day, startDate, endDate);
});
_defineProperty(_this, "isInSelectingRange", function () {
var _this$props3 = _this.props,
day = _this$props3.day,
selectsStart = _this$props3.selectsStart,
selectsEnd = _this$props3.selectsEnd,
selectingDate = _this$props3.selectingDate,
startDate = _this$props3.startDate,
endDate = _this$props3.endDate;
if (!(selectsStart || selectsEnd) || !selectingDate || _this.isDisabled()) {
return false;
}
if (selectsStart && endDate && selectingDate.isSameOrBefore(endDate)) {
return isDayInRange(day, selectingDate, endDate);
}
if (selectsEnd && startDate && selectingDate.isSameOrAfter(startDate)) {
return isDayInRange(day, startDate, selectingDate);
}
return false;
});
_defineProperty(_this, "isSelectingRangeStart", function () {
if (!_this.isInSelectingRange()) {
return false;
}
var _this$props4 = _this.props,
day = _this$props4.day,
selectingDate = _this$props4.selectingDate,
startDate = _this$props4.startDate,
selectsStart = _this$props4.selectsStart;
if (selectsStart) {
return isSameDay(day, selectingDate);
} else {
return isSameDay(day, startDate);
}
});
_defineProperty(_this, "isSelectingRangeEnd", function () {
if (!_this.isInSelectingRange()) {
return false;
}
var _this$props5 = _this.props,
day = _this$props5.day,
selectingDate = _this$props5.selectingDate,
endDate = _this$props5.endDate,
selectsEnd = _this$props5.selectsEnd;
if (selectsEnd) {
return isSameDay(day, selectingDate);
} else {
return isSameDay(day, endDate);
}
});
_defineProperty(_this, "isRangeStart", function () {
var _this$props6 = _this.props,
day = _this$props6.day,
startDate = _this$props6.startDate,
endDate = _this$props6.endDate;
if (!startDate || !endDate) {
return false;
}
return isSameDay(startDate, day);
});
_defineProperty(_this, "isRangeEnd", function () {
var _this$props7 = _this.props,
day = _this$props7.day,
startDate = _this$props7.startDate,
endDate = _this$props7.endDate;
if (!startDate || !endDate) {
return false;
}
return isSameDay(endDate, day);
});
_defineProperty(_this, "isWeekend", function () {
var weekday = getDay(_this.props.day);
return weekday === 0 || weekday === 6;
});
_defineProperty(_this, "isOutsideMonth", function () {
return _this.props.month !== undefined && _this.props.month !== getMonth(_this.props.day);
});
_defineProperty(_this, "getClassNames", function (date) {
var dayClassName = _this.props.dayClassName ? _this.props.dayClassName(date) : undefined;
return classnames("react-datepicker__day", dayClassName, "react-datepicker__day--" + getDayOfWeekCode(_this.props.day), {
"react-datepicker__day--disabled": _this.isDisabled(),
"react-datepicker__day--selected": _this.isSameDay(_this.props.selected),
"react-datepicker__day--keyboard-selected": _this.isKeyboardSelected(),
"react-datepicker__day--range-start": _this.isRangeStart(),
"react-datepicker__day--range-end": _this.isRangeEnd(),
"react-datepicker__day--in-range": _this.isInRange(),
"react-datepicker__day--in-selecting-range": _this.isInSelectingRange(),
"react-datepicker__day--selecting-range-start": _this.isSelectingRangeStart(),
"react-datepicker__day--selecting-range-end": _this.isSelectingRangeEnd(),
"react-datepicker__day--today": _this.isSameDay(now(_this.props.utcOffset)),
"react-datepicker__day--weekend": _this.isWeekend(),
"react-datepicker__day--outside-month": _this.isOutsideMonth()
}, _this.getHighLightedClass("react-datepicker__day--highlighted"));
});
return _this;
}
_inherits(Day, _React$Component);
return _createClass(Day, [{
key: "render",
value: function render() {
return ___EmotionJSX("div", {
className: this.getClassNames(this.props.day),
onClick: this.handleClick,
onMouseEnter: this.handleMouseEnter,
"aria-label": "day-".concat(getDate(this.props.day)),
role: "option"
}, this.props.renderDayContents ? this.props.renderDayContents(getDate(this.props.day)) : getDate(this.props.day));
}
}]);
}(React.Component);
_defineProperty(Day, "propTypes", {
disabledKeyboardNavigation: PropTypes.bool,
day: PropTypes.object.isRequired,
dayClassName: PropTypes.func,
endDate: PropTypes.object,
highlightDates: PropTypes.instanceOf(Map),
inline: PropTypes.bool,
month: PropTypes.number,
onClick: PropTypes.func,
onMouseEnter: PropTypes.func,
preSelection: PropTypes.object,
selected: PropTypes.object,
selectingDate: PropTypes.object,
selectsEnd: PropTypes.bool,
selectsStart: PropTypes.bool,
startDate: PropTypes.object,
utcOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
renderDayContents: PropTypes.func,
accessibleMode: PropTypes.bool
});
export { Day as default };