react-dates
Version:
A responsive and accessible date range picker component built with React
417 lines (373 loc) • 16.8 kB
JavaScript
import shallowCompare from "react-addons-shallow-compare";
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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 () { function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); } return _getPrototypeOf; }(); return _getPrototypeOf(o); }
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 () { function _setPrototypeOf(o, p) { o.__proto__ = p; return o; } return _setPrototypeOf; }(); 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 _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 _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; }
import React from 'react';
import PropTypes from 'prop-types';
import momentPropTypes from 'react-moment-proptypes';
import { forbidExtraProps, nonNegativeInteger } from 'airbnb-prop-types';
import { css, withStyles, withStylesPropTypes } from 'react-with-styles';
import moment from 'moment';
import { CalendarDayPhrases } from '../defaultPhrases';
import getPhrasePropTypes from '../utils/getPhrasePropTypes';
import getCalendarDaySettings from '../utils/getCalendarDaySettings';
import ModifiersShape from '../shapes/ModifiersShape';
import { DAY_SIZE } from '../constants';
var propTypes = process.env.NODE_ENV !== "production" ? forbidExtraProps(_objectSpread({}, withStylesPropTypes, {
day: momentPropTypes.momentObj,
daySize: nonNegativeInteger,
isOutsideDay: PropTypes.bool,
modifiers: ModifiersShape,
isFocused: PropTypes.bool,
tabIndex: PropTypes.oneOf([0, -1]),
onDayClick: PropTypes.func,
onDayMouseEnter: PropTypes.func,
onDayMouseLeave: PropTypes.func,
renderDayContents: PropTypes.func,
ariaLabelFormat: PropTypes.string,
// internationalization
phrases: PropTypes.shape(getPhrasePropTypes(CalendarDayPhrases))
})) : {};
var defaultProps = {
day: moment(),
daySize: DAY_SIZE,
isOutsideDay: false,
modifiers: new Set(),
isFocused: false,
tabIndex: -1,
onDayClick: function () {
function onDayClick() {}
return onDayClick;
}(),
onDayMouseEnter: function () {
function onDayMouseEnter() {}
return onDayMouseEnter;
}(),
onDayMouseLeave: function () {
function onDayMouseLeave() {}
return onDayMouseLeave;
}(),
renderDayContents: null,
ariaLabelFormat: 'dddd, LL',
// internationalization
phrases: CalendarDayPhrases
};
var CalendarDay =
/*#__PURE__*/
function (_ref) {
_inherits(CalendarDay, _ref);
_createClass(CalendarDay, [{
key: !React.PureComponent && "shouldComponentUpdate",
value: function () {
function value(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
return value;
}()
}]);
function CalendarDay() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, CalendarDay);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CalendarDay)).call.apply(_getPrototypeOf2, [this].concat(args)));
_this.setButtonRef = _this.setButtonRef.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
_createClass(CalendarDay, [{
key: "componentDidUpdate",
value: function () {
function componentDidUpdate(prevProps) {
var _this$props = this.props,
isFocused = _this$props.isFocused,
tabIndex = _this$props.tabIndex;
if (tabIndex === 0) {
if (isFocused || tabIndex !== prevProps.tabIndex) {
this.buttonRef.focus();
}
}
}
return componentDidUpdate;
}()
}, {
key: "onDayClick",
value: function () {
function onDayClick(day, e) {
var onDayClick = this.props.onDayClick;
onDayClick(day, e);
}
return onDayClick;
}()
}, {
key: "onDayMouseEnter",
value: function () {
function onDayMouseEnter(day, e) {
var onDayMouseEnter = this.props.onDayMouseEnter;
onDayMouseEnter(day, e);
}
return onDayMouseEnter;
}()
}, {
key: "onDayMouseLeave",
value: function () {
function onDayMouseLeave(day, e) {
var onDayMouseLeave = this.props.onDayMouseLeave;
onDayMouseLeave(day, e);
}
return onDayMouseLeave;
}()
}, {
key: "onKeyDown",
value: function () {
function onKeyDown(day, e) {
var onDayClick = this.props.onDayClick;
var key = e.key;
if (key === 'Enter' || key === ' ') {
onDayClick(day, e);
}
}
return onKeyDown;
}()
}, {
key: "setButtonRef",
value: function () {
function setButtonRef(ref) {
this.buttonRef = ref;
}
return setButtonRef;
}()
}, {
key: "render",
value: function () {
function render() {
var _this2 = this;
var _this$props2 = this.props,
day = _this$props2.day,
ariaLabelFormat = _this$props2.ariaLabelFormat,
daySize = _this$props2.daySize,
isOutsideDay = _this$props2.isOutsideDay,
modifiers = _this$props2.modifiers,
renderDayContents = _this$props2.renderDayContents,
tabIndex = _this$props2.tabIndex,
styles = _this$props2.styles,
phrases = _this$props2.phrases;
if (!day) return React.createElement("td", null);
var _getCalendarDaySettin = getCalendarDaySettings(day, ariaLabelFormat, daySize, modifiers, phrases),
daySizeStyles = _getCalendarDaySettin.daySizeStyles,
useDefaultCursor = _getCalendarDaySettin.useDefaultCursor,
selected = _getCalendarDaySettin.selected,
hoveredSpan = _getCalendarDaySettin.hoveredSpan,
isOutsideRange = _getCalendarDaySettin.isOutsideRange,
ariaLabel = _getCalendarDaySettin.ariaLabel;
return React.createElement("td", _extends({}, css(styles.CalendarDay, useDefaultCursor && styles.CalendarDay__defaultCursor, styles.CalendarDay__default, isOutsideDay && styles.CalendarDay__outside, modifiers.has('today') && styles.CalendarDay__today, modifiers.has('first-day-of-week') && styles.CalendarDay__firstDayOfWeek, modifiers.has('last-day-of-week') && styles.CalendarDay__lastDayOfWeek, modifiers.has('hovered-offset') && styles.CalendarDay__hovered_offset, modifiers.has('highlighted-calendar') && styles.CalendarDay__highlighted_calendar, modifiers.has('blocked-minimum-nights') && styles.CalendarDay__blocked_minimum_nights, modifiers.has('blocked-calendar') && styles.CalendarDay__blocked_calendar, hoveredSpan && styles.CalendarDay__hovered_span, modifiers.has('selected-span') && styles.CalendarDay__selected_span, modifiers.has('last-in-range') && styles.CalendarDay__last_in_range, modifiers.has('selected-start') && styles.CalendarDay__selected_start, modifiers.has('selected-end') && styles.CalendarDay__selected_end, selected && styles.CalendarDay__selected, isOutsideRange && styles.CalendarDay__blocked_out_of_range, daySizeStyles), {
role: "button" // eslint-disable-line jsx-a11y/no-noninteractive-element-to-interactive-role
,
ref: this.setButtonRef,
"aria-label": ariaLabel,
onMouseEnter: function () {
function onMouseEnter(e) {
_this2.onDayMouseEnter(day, e);
}
return onMouseEnter;
}(),
onMouseLeave: function () {
function onMouseLeave(e) {
_this2.onDayMouseLeave(day, e);
}
return onMouseLeave;
}(),
onMouseUp: function () {
function onMouseUp(e) {
e.currentTarget.blur();
}
return onMouseUp;
}(),
onClick: function () {
function onClick(e) {
_this2.onDayClick(day, e);
}
return onClick;
}(),
onKeyDown: function () {
function onKeyDown(e) {
_this2.onKeyDown(day, e);
}
return onKeyDown;
}(),
tabIndex: tabIndex
}), renderDayContents ? renderDayContents(day, modifiers) : day.format('D'));
}
return render;
}()
}]);
return CalendarDay;
}(React.PureComponent || React.Component);
CalendarDay.propTypes = process.env.NODE_ENV !== "production" ? propTypes : {};
CalendarDay.defaultProps = defaultProps;
export { CalendarDay as PureCalendarDay };
export default withStyles(function (_ref2) {
var _ref2$reactDates = _ref2.reactDates,
color = _ref2$reactDates.color,
font = _ref2$reactDates.font;
return {
CalendarDay: {
boxSizing: 'border-box',
cursor: 'pointer',
fontSize: font.size,
textAlign: 'center',
':active': {
outline: 0
}
},
CalendarDay__defaultCursor: {
cursor: 'default'
},
CalendarDay__default: {
border: "1px solid ".concat(color.core.borderLight),
color: color.text,
background: color.background,
':hover': {
background: color.core.borderLight,
border: "1px solid ".concat(color.core.borderLight),
color: 'inherit'
}
},
CalendarDay__hovered_offset: {
background: color.core.borderBright,
border: "1px double ".concat(color.core.borderLight),
color: 'inherit'
},
CalendarDay__outside: {
border: 0,
background: color.outside.backgroundColor,
color: color.outside.color,
':hover': {
border: 0
}
},
CalendarDay__blocked_minimum_nights: {
background: color.minimumNights.backgroundColor,
border: "1px solid ".concat(color.minimumNights.borderColor),
color: color.minimumNights.color,
':hover': {
background: color.minimumNights.backgroundColor_hover,
color: color.minimumNights.color_active
},
':active': {
background: color.minimumNights.backgroundColor_active,
color: color.minimumNights.color_active
}
},
CalendarDay__highlighted_calendar: {
background: color.highlighted.backgroundColor,
color: color.highlighted.color,
':hover': {
background: color.highlighted.backgroundColor_hover,
color: color.highlighted.color_active
},
':active': {
background: color.highlighted.backgroundColor_active,
color: color.highlighted.color_active
}
},
CalendarDay__selected_span: {
background: color.selectedSpan.backgroundColor,
border: "1px double ".concat(color.selectedSpan.borderColor),
color: color.selectedSpan.color,
':hover': {
background: color.selectedSpan.backgroundColor_hover,
border: "1px double ".concat(color.selectedSpan.borderColor),
color: color.selectedSpan.color_active
},
':active': {
background: color.selectedSpan.backgroundColor_active,
border: "1px double ".concat(color.selectedSpan.borderColor),
color: color.selectedSpan.color_active
}
},
CalendarDay__last_in_range: {
borderStyle: 'solid',
':hover': {
borderStyle: 'solid'
}
},
CalendarDay__selected: {
background: color.selected.backgroundColor,
border: "1px double ".concat(color.selected.borderColor),
color: color.selected.color,
':hover': {
background: color.selected.backgroundColor_hover,
border: "1px double ".concat(color.selected.borderColor),
color: color.selected.color_active
},
':active': {
background: color.selected.backgroundColor_active,
border: "1px double ".concat(color.selected.borderColor),
color: color.selected.color_active
}
},
CalendarDay__hovered_span: {
background: color.hoveredSpan.backgroundColor,
border: "1px double ".concat(color.hoveredSpan.borderColor),
color: color.hoveredSpan.color,
':hover': {
background: color.hoveredSpan.backgroundColor_hover,
border: "1px double ".concat(color.hoveredSpan.borderColor),
color: color.hoveredSpan.color_active
},
':active': {
background: color.hoveredSpan.backgroundColor_active,
border: "1px double ".concat(color.hoveredSpan.borderColor),
color: color.hoveredSpan.color_active
}
},
CalendarDay__blocked_calendar: {
background: color.blocked_calendar.backgroundColor,
border: "1px solid ".concat(color.blocked_calendar.borderColor),
color: color.blocked_calendar.color,
':hover': {
background: color.blocked_calendar.backgroundColor_hover,
border: "1px solid ".concat(color.blocked_calendar.borderColor),
color: color.blocked_calendar.color_active
},
':active': {
background: color.blocked_calendar.backgroundColor_active,
border: "1px solid ".concat(color.blocked_calendar.borderColor),
color: color.blocked_calendar.color_active
}
},
CalendarDay__blocked_out_of_range: {
background: color.blocked_out_of_range.backgroundColor,
border: "1px solid ".concat(color.blocked_out_of_range.borderColor),
color: color.blocked_out_of_range.color,
':hover': {
background: color.blocked_out_of_range.backgroundColor_hover,
border: "1px solid ".concat(color.blocked_out_of_range.borderColor),
color: color.blocked_out_of_range.color_active
},
':active': {
background: color.blocked_out_of_range.backgroundColor_active,
border: "1px solid ".concat(color.blocked_out_of_range.borderColor),
color: color.blocked_out_of_range.color_active
}
},
CalendarDay__selected_start: {},
CalendarDay__selected_end: {},
CalendarDay__today: {},
CalendarDay__firstDayOfWeek: {},
CalendarDay__lastDayOfWeek: {}
};
}, {
pureComponent: typeof React.PureComponent !== 'undefined'
})(CalendarDay);