import { Popover } from '@automattic/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import { debounce } from 'lodash'; import moment from 'moment'; import PropTypes from 'prop-types'; import { createRef, Component } from 'react'; import { withLocalizedMoment } from 'calypso/components/localized-moment'; import DateRangePicker from './date-range-picker'; import DateRangeFooter from './footer'; import DateRangeHeader from './header'; import DateRangeInputs from './inputs'; import Shortcuts from './shortcuts'; import DateRangeTrigger from './trigger'; import './style.scss'; /** * Module variables */ const NO_DATE_SELECTED_VALUE = null; const noop = () => {}; export class DateRange extends Component { static propTypes = { selectedStartDate: PropTypes.oneOfType( [ PropTypes.instanceOf( Date ), PropTypes.instanceOf( moment ), ] ), selectedEndDate: PropTypes.oneOfType( [ PropTypes.instanceOf( Date ), PropTypes.instanceOf( moment ), ] ), selectedShortcutId: PropTypes.string, onDateSelect: PropTypes.func, onDateCommit: PropTypes.func, firstSelectableDate: PropTypes.oneOfType( [ PropTypes.instanceOf( Date ), PropTypes.instanceOf( moment ), ] ), lastSelectableDate: PropTypes.oneOfType( [ PropTypes.instanceOf( Date ), PropTypes.instanceOf( moment ), ] ), triggerText: PropTypes.func, isCompact: PropTypes.bool, showTriggerClear: PropTypes.bool, renderTrigger: PropTypes.func, renderHeader: PropTypes.func, renderFooter: PropTypes.func, renderInputs: PropTypes.func, displayShortcuts: PropTypes.bool, rootClass: PropTypes.string, useArrowNavigation: PropTypes.bool, overlay: PropTypes.node, customTitle: PropTypes.string, onShortcutClick: PropTypes.func, trackExternalDateChanges: PropTypes.bool, shortcutList: PropTypes.array, }; static defaultProps = { onDateSelect: noop, onDateCommit: noop, isCompact: false, focusedMonth: null, showTriggerClear: true, renderTrigger: ( props ) => , renderHeader: ( props ) => , renderFooter: ( props ) => , renderInputs: ( props ) => , displayShortcuts: false, rootClass: '', useArrowNavigation: false, overlay: null, customTitle: '', trackExternalDateChanges: false, }; constructor( props ) { super( props ); // Define the date range that is selectable (ie: not disabled) const firstSelectableDate = this.props.firstSelectableDate && this.props.moment( this.props.firstSelectableDate ); const lastSelectableDate = this.props.lastSelectableDate && this.props.moment( this.props.lastSelectableDate ); // Clamp start/end dates to ranges (if specified) let startDate = this.props.selectedStartDate == null ? NO_DATE_SELECTED_VALUE : this.clampDateToRange( this.props.moment( this.props.selectedStartDate ), { dateFrom: firstSelectableDate, dateTo: lastSelectableDate, } ); let endDate = this.props.selectedEndDate == null ? NO_DATE_SELECTED_VALUE : this.clampDateToRange( this.props.moment( this.props.selectedEndDate ), { dateFrom: firstSelectableDate, dateTo: lastSelectableDate, } ); const selectedShortcutId = this.props.selectedShortcutId || null; // Ensure start is before end otherwise flip the values if ( startDate && endDate && endDate.isBefore( startDate ) ) { // flip values via array destructuring (think about it...) [ startDate, endDate ] = [ endDate, startDate ]; } // Build initial state this.state = { popoverVisible: false, staleStartDate: null, staleEndDate: null, startDate: startDate, endDate: endDate, selectedShortcutId, staleDatesSaved: false, // this needs to be independent from startDate because we must independently validate them // before updating the central source of truth (ie: startDate) textInputStartDate: this.toDateString( startDate ), textInputEndDate: this.toDateString( endDate ), initialStartDate: startDate, // cache values in case we need to reset to them initialEndDate: endDate, // cache values in case we need to reset to them focusedMonth: this.props.focusedMonth, numberOfMonths: this.getNumberOfMonths(), }; // Ref to the Trigger