UNPKG

@roo-ui/components

Version:

309 lines (267 loc) 13.2 kB
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; 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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } 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 _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 Dayzed from 'dayzed'; import subDays from 'date-fns/sub_days'; import differenceInCalendarMonths from 'date-fns/difference_in_calendar_months'; import startOfDay from 'date-fns/start_of_day'; import endOfDay from 'date-fns/end_of_day'; import isSameDay from 'date-fns/is_same_day'; import throttle from 'lodash/fp/throttle'; import { Flex, Box } from '../'; import isDateInRange from './lib/isDateInRange'; import CalendarNav from './components/CalendarNav'; import CalendarMonth from './components/CalendarMonth'; var NOOP = function NOOP() { return false; }; var throttled = throttle(10); var DateRangePicker = /*#__PURE__*/ function (_React$Component) { _inherits(DateRangePicker, _React$Component); function DateRangePicker(props) { var _this; _classCallCheck(this, DateRangePicker); _this = _possibleConstructorReturn(this, _getPrototypeOf(DateRangePicker).call(this, props)); _defineProperty(_assertThisInitialized(_this), "onOffsetChanged", function (offset) { return _this.setState({ offset: offset }); }); _defineProperty(_assertThisInitialized(_this), "onMouseLeaveOfCalendar", function () { return _this.setState({ hoveredDate: null }); }); _defineProperty(_assertThisInitialized(_this), "onMouseEnterOfDay", throttled(function (hoveredDate) { if (_this.state.startDate && !_this.state.endDate) { _this.setState({ hoveredDate: hoveredDate.date }); } })); _defineProperty(_assertThisInitialized(_this), "onDateSelected", function (_ref) { var selectable = _ref.selectable, date = _ref.date; if (!selectable) return; var _this$state = _this.state, startDate = _this$state.startDate, endDate = _this$state.endDate, isSettingStartDate = _this$state.isSettingStartDate, isSettingEndDate = _this$state.isSettingEndDate; if (!startDate || isSettingStartDate) { _this.selectStartDate(date); } else if (!endDate || isSettingEndDate) { _this.selectEndDate(date); } else { _this.resetWithStartDate(date); } _this.notifyRangeSelection(); }); _defineProperty(_assertThisInitialized(_this), "notifyRangeSelection", function () { var _this$state2 = _this.state, startDate = _this$state2.startDate, endDate = _this$state2.endDate; var onRangeSelected = _this.props.onRangeSelected; if (startDate && endDate && onRangeSelected) { onRangeSelected({ startDate: startDate, endDate: endDate }); } }); _defineProperty(_assertThisInitialized(_this), "selectStartDate", function (startDate) { var endDate = startDate <= _this.state.endDate ? _this.state.endDate : null; var endDateRequiresSelection = endDate === null; _this.setState({ startDate: startDate, endDate: endDate, isSettingStartDate: false, isSettingEndDate: endDateRequiresSelection, hoveredDate: null }); }); _defineProperty(_assertThisInitialized(_this), "selectEndDate", function (date) { if (date <= _this.state.startDate) { _this.resetWithStartDate(date); } else { _this.setState({ endDate: date, isSettingEndDate: false, hoveredDate: null }); } }); _defineProperty(_assertThisInitialized(_this), "resetWithStartDate", function (startDate) { _this.setState({ startDate: startDate, endDate: null, isSettingStartDate: false, isSettingEndDate: true, hoveredDate: null }); }); _defineProperty(_assertThisInitialized(_this), "isInRange", function (date) { var _this$state3 = _this.state, startDate = _this$state3.startDate, endDate = _this$state3.endDate, isSettingStartDate = _this$state3.isSettingStartDate, hoveredDate = _this$state3.hoveredDate; return isDateInRange({ startDate: startDate, endDate: endDate, isSettingStartDate: isSettingStartDate, hoveredDate: hoveredDate, date: date }); }); var _this$props = _this.props, initialStartDate = _this$props.initialStartDate, initialEndDate = _this$props.initialEndDate, initialDisplayDate = _this$props.initialDisplayDate; var _startDate = initialStartDate ? startOfDay(initialStartDate) : null; var _endDate = initialEndDate ? endOfDay(initialEndDate) : null; var focusDate = _startDate || _endDate || initialDisplayDate; _this.state = { hoveredDate: null, offset: differenceInCalendarMonths(focusDate, initialDisplayDate), isSettingStartDate: props.isSettingStartDate, isSettingEndDate: props.isSettingEndDate, startDate: _startDate, endDate: _endDate }; return _this; } _createClass(DateRangePicker, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var _this$state4 = this.state, startDate = _this$state4.startDate, endDate = _this$state4.endDate; var prevStartDate = prevState.startDate, prevEndDate = prevState.endDate; var startDateDidUpdate = !isSameDay(startDate, prevStartDate); var endDateDidUpdate = !isSameDay(endDate, prevEndDate); if (startDateDidUpdate || endDateDidUpdate) { this.props.onChangeDates({ startDate: startDate, endDate: endDate }); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, monthNames = _this$props2.monthNames, weekdayNames = _this$props2.weekdayNames, monthsToDisplay = _this$props2.monthsToDisplay, stacked = _this$props2.stacked, disabledDates = _this$props2.disabledDates, interactiveDisabledDates = _this$props2.interactiveDisabledDates, initialDisplayDate = _this$props2.initialDisplayDate, rest = _objectWithoutProperties(_this$props2, ["monthNames", "weekdayNames", "monthsToDisplay", "stacked", "disabledDates", "interactiveDisabledDates", "initialDisplayDate"]); var _this$state5 = this.state, startDate = _this$state5.startDate, endDate = _this$state5.endDate, offset = _this$state5.offset; var selectedDates = [startDate, endDate]; return React.createElement(Dayzed, _extends({}, rest, { selected: selectedDates, date: initialDisplayDate, offset: offset, monthsToDisplay: monthsToDisplay, onDateSelected: this.onDateSelected, onOffsetChanged: this.onOffsetChanged, render: function render(_ref2) { var calendars = _ref2.calendars, getBackProps = _ref2.getBackProps, getForwardProps = _ref2.getForwardProps, getDateProps = _ref2.getDateProps; if (!calendars.length) return null; return React.createElement(Box, { onMouseLeave: _this2.onMouseLeaveOfCalendar, position: "relative" }, React.createElement(CalendarNav, { prevProps: getBackProps({ calendars: calendars }), nextProps: getForwardProps({ calendars: calendars }) }), React.createElement(Flex, { flexWrap: "wrap" }, calendars.map(function (calendar) { return React.createElement(CalendarMonth, { key: "".concat(calendar.month).concat(calendar.year), monthsToDisplay: monthsToDisplay, monthName: monthNames[calendar.month], month: calendar.month, year: calendar.year, stacked: stacked, weekdayNames: weekdayNames, weeks: calendar.weeks, getDateProps: getDateProps, disabledDates: disabledDates, interactiveDisabledDates: interactiveDisabledDates, onMouseEnterOfDay: _this2.onMouseEnterOfDay, isInRange: _this2.isInRange }); }))); } })); } }]); return DateRangePicker; }(React.Component); DateRangePicker.defaultProps = { monthsToDisplay: 1, firstDayOfWeek: 1, stacked: false, minDate: subDays(new Date(), 1), disabledDates: [], monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], weekdayNames: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], interactiveDisabledDates: false, initialStartDate: null, initialEndDate: null, initialDisplayDate: new Date(), onChangeStartDate: null, onChangeEndDate: null, isSettingStartDate: false, isSettingEndDate: false, onRangeSelected: NOOP, onChangeDates: NOOP }; DateRangePicker.propTypes = { monthsToDisplay: PropTypes.number, firstDayOfWeek: PropTypes.number, stacked: PropTypes.bool, minDate: PropTypes.instanceOf(Date), disabledDates: PropTypes.arrayOf(PropTypes.instanceOf(Date)), interactiveDisabledDates: PropTypes.bool, monthNames: PropTypes.arrayOf(PropTypes.string), weekdayNames: PropTypes.arrayOf(PropTypes.string), initialStartDate: PropTypes.instanceOf(Date), initialEndDate: PropTypes.instanceOf(Date), initialDisplayDate: PropTypes.instanceOf(Date), isSettingStartDate: PropTypes.bool, isSettingEndDate: PropTypes.bool, onRangeSelected: PropTypes.func, onChangeDates: PropTypes.func }; export default DateRangePicker;