import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized"; import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose"; import shallowEqual from "enzyme-shallow-equal"; import React from 'react'; import PropTypes from 'prop-types'; import moment from 'moment'; import momentPropTypes from 'react-moment-proptypes'; import { forbidExtraProps, nonNegativeInteger } from 'airbnb-prop-types'; import openDirectionShape from '../shapes/OpenDirectionShape'; import { DateRangePickerInputPhrases } from '../defaultPhrases'; import getPhrasePropTypes from '../utils/getPhrasePropTypes'; import DateRangePickerInput from './DateRangePickerInput'; import IconPositionShape from '../shapes/IconPositionShape'; import DisabledShape from '../shapes/DisabledShape'; import toMomentObject from '../utils/toMomentObject'; import toLocalizedDateString from '../utils/toLocalizedDateString'; import isInclusivelyAfterDay from '../utils/isInclusivelyAfterDay'; import isBeforeDay from '../utils/isBeforeDay'; import { START_DATE, END_DATE, ICON_BEFORE_POSITION, OPEN_DOWN } from '../constants'; var propTypes = process.env.NODE_ENV !== "production" ? forbidExtraProps({ children: PropTypes.node, startDate: momentPropTypes.momentObj, startDateId: PropTypes.string, startDatePlaceholderText: PropTypes.string, isStartDateFocused: PropTypes.bool, startDateAriaLabel: PropTypes.string, endDate: momentPropTypes.momentObj, endDateId: PropTypes.string, endDatePlaceholderText: PropTypes.string, isEndDateFocused: PropTypes.bool, endDateAriaLabel: PropTypes.string, screenReaderMessage: PropTypes.string, showClearDates: PropTypes.bool, showCaret: PropTypes.bool, showDefaultInputIcon: PropTypes.bool, inputIconPosition: IconPositionShape, disabled: DisabledShape, required: PropTypes.bool, readOnly: PropTypes.bool, openDirection: openDirectionShape, noBorder: PropTypes.bool, block: PropTypes.bool, small: PropTypes.bool, regular: PropTypes.bool, verticalSpacing: nonNegativeInteger, keepOpenOnDateSelect: PropTypes.bool, reopenPickerOnClearDates: PropTypes.bool, withFullScreenPortal: PropTypes.bool, minimumNights: nonNegativeInteger, isOutsideRange: PropTypes.func, displayFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), onFocusChange: PropTypes.func, onClose: PropTypes.func, onDatesChange: PropTypes.func, onKeyDownArrowDown: PropTypes.func, onKeyDownQuestionMark: PropTypes.func, customInputIcon: PropTypes.node, customArrowIcon: PropTypes.node, customCloseIcon: PropTypes.node, // accessibility isFocused: PropTypes.bool, // i18n phrases: PropTypes.shape(getPhrasePropTypes(DateRangePickerInputPhrases)), isRTL: PropTypes.bool }) : {}; var defaultProps = { children: null, startDate: null, startDateId: START_DATE, startDatePlaceholderText: 'Start Date', isStartDateFocused: false, startDateAriaLabel: undefined, endDate: null, endDateId: END_DATE, endDatePlaceholderText: 'End Date', isEndDateFocused: false, endDateAriaLabel: undefined, screenReaderMessage: '', showClearDates: false, showCaret: false, showDefaultInputIcon: false, inputIconPosition: ICON_BEFORE_POSITION, disabled: false, required: false, readOnly: false, openDirection: OPEN_DOWN, noBorder: false, block: false, small: false, regular: false, verticalSpacing: undefined, keepOpenOnDateSelect: false, reopenPickerOnClearDates: false, withFullScreenPortal: false, minimumNights: 1, isOutsideRange: function isOutsideRange(day) { return !isInclusivelyAfterDay(day, moment()); }, displayFormat: function displayFormat() { return moment.localeData().longDateFormat('L'); }, onFocusChange: function onFocusChange() {}, onClose: function onClose() {}, onDatesChange: function onDatesChange() {}, onKeyDownArrowDown: function onKeyDownArrowDown() {}, onKeyDownQuestionMark: function onKeyDownQuestionMark() {}, customInputIcon: null, customArrowIcon: null, customCloseIcon: null, // accessibility isFocused: false, // i18n phrases: DateRangePickerInputPhrases, isRTL: false }; var DateRangePickerInputController = /*#__PURE__*/ function (_ref) { _inheritsLoose(DateRangePickerInputController, _ref); var _proto = DateRangePickerInputController.prototype; _proto[!React.PureComponent && "shouldComponentUpdate"] = function (nextProps, nextState) { return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState); }; function DateRangePickerInputController(props) { var _this; _this = _ref.call(this, props) || this; _this.onClearFocus = _this.onClearFocus.bind(_assertThisInitialized(_this)); _this.onStartDateChange = _this.onStartDateChange.bind(_assertThisInitialized(_this)); _this.onStartDateFocus = _this.onStartDateFocus.bind(_assertThisInitialized(_this)); _this.onEndDateChange = _this.onEndDateChange.bind(_assertThisInitialized(_this)); _this.onEndDateFocus = _this.onEndDateFocus.bind(_assertThisInitialized(_this)); _this.clearDates = _this.clearDates.bind(_assertThisInitialized(_this)); return _this; } _proto.onClearFocus = function onClearFocus() { var _this$props = this.props, onFocusChange = _this$props.onFocusChange, onClose = _this$props.onClose, startDate = _this$props.startDate, endDate = _this$props.endDate; onFocusChange(null); onClose({ startDate: startDate, endDate: endDate }); }; _proto.onEndDateChange = function onEndDateChange(endDateString) { var _this$props2 = this.props, startDate = _this$props2.startDate, isOutsideRange = _this$props2.isOutsideRange, minimumNights = _this$props2.minimumNights, keepOpenOnDateSelect = _this$props2.keepOpenOnDateSelect, onDatesChange = _this$props2.onDatesChange; var endDate = toMomentObject(endDateString, this.getDisplayFormat()); var isEndDateValid = endDate && !isOutsideRange(endDate) && !(startDate && isBeforeDay(endDate, startDate.clone().add(minimumNights, 'days'))); if (isEndDateValid) { onDatesChange({ startDate: startDate, endDate: endDate }); if (!keepOpenOnDateSelect) this.onClearFocus(); } else { onDatesChange({ startDate: startDate, endDate: null }); } }; _proto.onEndDateFocus = function onEndDateFocus() { var _this$props3 = this.props, startDate = _this$props3.startDate, onFocusChange = _this$props3.onFocusChange, withFullScreenPortal = _this$props3.withFullScreenPortal, disabled = _this$props3.disabled; if (!startDate && withFullScreenPortal && (!disabled || disabled === END_DATE)) { // When the datepicker is full screen, we never want to focus the end date first // because there's no indication that that is the case once the datepicker is open and it // might confuse the user onFocusChange(START_DATE); } else if (!disabled || disabled === START_DATE) { onFocusChange(END_DATE); } }; _proto.onStartDateChange = function onStartDateChange(startDateString) { var endDate = this.props.endDate; var _this$props4 = this.props, isOutsideRange = _this$props4.isOutsideRange, minimumNights = _this$props4.minimumNights, onDatesChange = _this$props4.onDatesChange, onFocusChange = _this$props4.onFocusChange, disabled = _this$props4.disabled; var startDate = toMomentObject(startDateString, this.getDisplayFormat()); var isEndDateBeforeStartDate = startDate && isBeforeDay(endDate, startDate.clone().add(minimumNights, 'days')); var isStartDateValid = startDate && !isOutsideRange(startDate) && !(disabled === END_DATE && isEndDateBeforeStartDate); if (isStartDateValid) { if (isEndDateBeforeStartDate) { endDate = null; } onDatesChange({ startDate: startDate, endDate: endDate }); onFocusChange(END_DATE); } else { onDatesChange({ startDate: null, endDate: endDate }); } }; _proto.onStartDateFocus = function onStartDateFocus() { var _this$props5 = this.props, disabled = _this$props5.disabled, onFocusChange = _this$props5.onFocusChange; if (!disabled || disabled === END_DATE) { onFocusChange(START_DATE); } }; _proto.getDisplayFormat = function getDisplayFormat() { var displayFormat = this.props.displayFormat; return typeof displayFormat === 'string' ? displayFormat : displayFormat(); }; _proto.getDateString = function getDateString(date) { var displayFormat = this.getDisplayFormat(); if (date && displayFormat) { return date && date.format(displayFormat); } return toLocalizedDateString(date); }; _proto.clearDates = function clearDates() { var _this$props6 = this.props, onDatesChange = _this$props6.onDatesChange, reopenPickerOnClearDates = _this$props6.reopenPickerOnClearDates, onFocusChange = _this$props6.onFocusChange; onDatesChange({ startDate: null, endDate: null }); if (reopenPickerOnClearDates) { onFocusChange(START_DATE); } }; _proto.render = function render() { var _this$props7 = this.props, children = _this$props7.children, startDate = _this$props7.startDate, startDateId = _this$props7.startDateId, startDatePlaceholderText = _this$props7.startDatePlaceholderText, isStartDateFocused = _this$props7.isStartDateFocused, startDateAriaLabel = _this$props7.startDateAriaLabel, endDate = _this$props7.endDate, endDateId = _this$props7.endDateId, endDatePlaceholderText = _this$props7.endDatePlaceholderText, endDateAriaLabel = _this$props7.endDateAriaLabel, isEndDateFocused = _this$props7.isEndDateFocused, screenReaderMessage = _this$props7.screenReaderMessage, showClearDates = _this$props7.showClearDates, showCaret = _this$props7.showCaret, showDefaultInputIcon = _this$props7.showDefaultInputIcon, inputIconPosition = _this$props7.inputIconPosition, customInputIcon = _this$props7.customInputIcon, customArrowIcon = _this$props7.customArrowIcon, customCloseIcon = _this$props7.customCloseIcon, disabled = _this$props7.disabled, required = _this$props7.required, readOnly = _this$props7.readOnly, openDirection = _this$props7.openDirection, isFocused = _this$props7.isFocused, phrases = _this$props7.phrases, onKeyDownArrowDown = _this$props7.onKeyDownArrowDown, onKeyDownQuestionMark = _this$props7.onKeyDownQuestionMark, isRTL = _this$props7.isRTL, noBorder = _this$props7.noBorder, block = _this$props7.block, small = _this$props7.small, regular = _this$props7.regular, verticalSpacing = _this$props7.verticalSpacing; var startDateString = this.getDateString(startDate); var endDateString = this.getDateString(endDate); return React.createElement(DateRangePickerInput, { startDate: startDateString, startDateId: startDateId, startDatePlaceholderText: startDatePlaceholderText, isStartDateFocused: isStartDateFocused, startDateAriaLabel: startDateAriaLabel, endDate: endDateString, endDateId: endDateId, endDatePlaceholderText: endDatePlaceholderText, isEndDateFocused: isEndDateFocused, endDateAriaLabel: endDateAriaLabel, isFocused: isFocused, disabled: disabled, required: required, readOnly: readOnly, openDirection: openDirection, showCaret: showCaret, showDefaultInputIcon: showDefaultInputIcon, inputIconPosition: inputIconPosition, customInputIcon: customInputIcon, customArrowIcon: customArrowIcon, customCloseIcon: customCloseIcon, phrases: phrases, onStartDateChange: this.onStartDateChange, onStartDateFocus: this.onStartDateFocus, onStartDateShiftTab: this.onClearFocus, onEndDateChange: this.onEndDateChange, onEndDateFocus: this.onEndDateFocus, showClearDates: showClearDates, onClearDates: this.clearDates, screenReaderMessage: screenReaderMessage, onKeyDownArrowDown: onKeyDownArrowDown, onKeyDownQuestionMark: onKeyDownQuestionMark, isRTL: isRTL, noBorder: noBorder, block: block, small: small, regular: regular, verticalSpacing: verticalSpacing }, children); }; return DateRangePickerInputController; }(React.PureComponent || React.Component); export { DateRangePickerInputController as default }; DateRangePickerInputController.propTypes = process.env.NODE_ENV !== "production" ? propTypes : {}; DateRangePickerInputController.defaultProps = defaultProps;