|
|
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'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ) => <DateRangeTrigger { ...props } />, |
|
|
renderHeader: ( props ) => <DateRangeHeader { ...props } />, |
|
|
renderFooter: ( props ) => <DateRangeFooter { ...props } />, |
|
|
renderInputs: ( props ) => <DateRangeInputs { ...props } />, |
|
|
displayShortcuts: false, |
|
|
rootClass: '', |
|
|
useArrowNavigation: false, |
|
|
overlay: null, |
|
|
customTitle: '', |
|
|
trackExternalDateChanges: false, |
|
|
}; |
|
|
|
|
|
constructor( props ) { |
|
|
super( props ); |
|
|
|
|
|
|
|
|
const firstSelectableDate = |
|
|
this.props.firstSelectableDate && this.props.moment( this.props.firstSelectableDate ); |
|
|
const lastSelectableDate = |
|
|
this.props.lastSelectableDate && this.props.moment( this.props.lastSelectableDate ); |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
if ( startDate && endDate && endDate.isBefore( startDate ) ) { |
|
|
|
|
|
[ startDate, endDate ] = [ endDate, startDate ]; |
|
|
} |
|
|
|
|
|
|
|
|
this.state = { |
|
|
popoverVisible: false, |
|
|
staleStartDate: null, |
|
|
staleEndDate: null, |
|
|
startDate: startDate, |
|
|
endDate: endDate, |
|
|
selectedShortcutId, |
|
|
staleDatesSaved: false, |
|
|
|
|
|
|
|
|
textInputStartDate: this.toDateString( startDate ), |
|
|
textInputEndDate: this.toDateString( endDate ), |
|
|
initialStartDate: startDate, |
|
|
initialEndDate: endDate, |
|
|
focusedMonth: this.props.focusedMonth, |
|
|
numberOfMonths: this.getNumberOfMonths(), |
|
|
}; |
|
|
|
|
|
|
|
|
this.triggerButtonRef = createRef(); |
|
|
this.throttledHandleResize = debounce( () => { |
|
|
this.setState( { |
|
|
numberOfMonths: this.getNumberOfMonths(), |
|
|
} ); |
|
|
}, 250 ); |
|
|
} |
|
|
|
|
|
componentDidMount() { |
|
|
window.addEventListener( 'resize', this.throttledHandleResize ); |
|
|
} |
|
|
|
|
|
componentWillUnmount() { |
|
|
window.removeEventListener( 'resize', this.throttledHandleResize ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openPopover = () => { |
|
|
const newState = { |
|
|
popoverVisible: true, |
|
|
}; |
|
|
if ( this.props.trackExternalDateChanges ) { |
|
|
newState.startDate = this.props.selectedStartDate; |
|
|
newState.endDate = this.props.selectedEndDate; |
|
|
newState.selectedShortcutId = this.props.selectedShortcutId; |
|
|
newState.textInputStartDate = this.toDateString( this.props.selectedStartDate ); |
|
|
newState.textInputEndDate = this.toDateString( this.props.selectedEndDate ); |
|
|
newState.staleStartDate = this.props.selectedStartDate; |
|
|
newState.staleEndDate = this.props.selectedEndDate; |
|
|
} |
|
|
this.setState( newState ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
closePopover = () => { |
|
|
this.setState( { |
|
|
popoverVisible: false, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
togglePopover = () => { |
|
|
if ( this.state.popoverVisible ) { |
|
|
this.closePopover(); |
|
|
} else { |
|
|
this.openPopover(); |
|
|
} |
|
|
}; |
|
|
|
|
|
closePopoverAndRevert = () => { |
|
|
this.closePopover(); |
|
|
this.revertDates(); |
|
|
}; |
|
|
|
|
|
closePopoverAndCommit = () => { |
|
|
this.closePopover(); |
|
|
this.commitDates(); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleInputChange = ( val, startOrEnd ) => { |
|
|
this.setState( { |
|
|
[ `textInput${ startOrEnd }Date` ]: val, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleInputBlur = ( val, startOrEnd ) => { |
|
|
if ( val === '' ) { |
|
|
return; |
|
|
} |
|
|
const date = this.props.moment( val, this.getLocaleDateFormat() ); |
|
|
|
|
|
if ( ! date.isValid() ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const stateKey = `${ startOrEnd.toLowerCase() }Date`; |
|
|
|
|
|
const isSameDate = |
|
|
this.state[ stateKey ] !== null ? this.state[ stateKey ].isSame( date, 'day' ) : false; |
|
|
|
|
|
if ( isSameDate ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
if ( ! this.state.startDate ) { |
|
|
this.setState( { |
|
|
startDate: date, |
|
|
} ); |
|
|
return; |
|
|
} |
|
|
|
|
|
this.setState( { |
|
|
[ stateKey ]: date, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleInputFocus = ( val, startOrEnd ) => { |
|
|
if ( val === '' ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const date = this.props.moment( val, this.getLocaleDateFormat() ); |
|
|
|
|
|
if ( ! date.isValid() ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const numMonthsShowing = this.getNumberOfMonths(); |
|
|
|
|
|
|
|
|
|
|
|
if ( startOrEnd === 'End' && numMonthsShowing > 1 ) { |
|
|
|
|
|
|
|
|
date.subtract( 1, 'months' ); |
|
|
} |
|
|
|
|
|
this.setState( { |
|
|
focusedMonth: date.toDate(), |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commitDates = () => { |
|
|
this.setState( |
|
|
( previousState ) => ( { |
|
|
staleStartDate: previousState.startDate, |
|
|
staleEndDate: previousState.endDate, |
|
|
staleDatesSaved: false, |
|
|
} ), |
|
|
() => { |
|
|
this.props.onDateCommit( |
|
|
this.state.startDate, |
|
|
this.state.endDate, |
|
|
this.state.selectedShortcutId |
|
|
); |
|
|
this.closePopover(); |
|
|
} |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
revertDates = () => { |
|
|
this.setState( |
|
|
( previousState, props ) => { |
|
|
const startDate = previousState.staleStartDate; |
|
|
const endDate = previousState.staleEndDate; |
|
|
const previousAppliedShortcutId = props.selectedShortcutId; |
|
|
|
|
|
const newState = { |
|
|
staleDatesSaved: false, |
|
|
startDate: startDate, |
|
|
endDate: endDate, |
|
|
selectedShortcutId: previousAppliedShortcutId, |
|
|
textInputStartDate: this.toDateString( startDate ), |
|
|
textInputEndDate: this.toDateString( endDate ), |
|
|
}; |
|
|
|
|
|
return newState; |
|
|
}, |
|
|
() => { |
|
|
this.props.onDateCommit( |
|
|
this.state.startDate, |
|
|
this.state.endDate, |
|
|
this.state.selectedShortcutId |
|
|
); |
|
|
} |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resetDates = () => { |
|
|
this.setState( ( previousState, props ) => { |
|
|
const startDate = previousState.initialStartDate; |
|
|
const endDate = previousState.initialEndDate; |
|
|
const previousAppliedShortcutId = props.selectedShortcutId; |
|
|
|
|
|
const newState = { |
|
|
staleDatesSaved: false, |
|
|
startDate: startDate, |
|
|
endDate: endDate, |
|
|
selectedShortcutId: previousAppliedShortcutId, |
|
|
textInputStartDate: this.toDateString( startDate ), |
|
|
textInputEndDate: this.toDateString( endDate ), |
|
|
}; |
|
|
|
|
|
return newState; |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clearDates = () => { |
|
|
this.setState( |
|
|
{ |
|
|
startDate: null, |
|
|
endDate: null, |
|
|
selectedShortcutId: null, |
|
|
staleStartDate: null, |
|
|
staleEndDate: null, |
|
|
textInputStartDate: '', |
|
|
textInputEndDate: '', |
|
|
}, |
|
|
() => { |
|
|
|
|
|
this.props.onDateCommit( |
|
|
this.state.startDate, |
|
|
this.state.endDate, |
|
|
this.state.selectedShortcutId |
|
|
); |
|
|
} |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
formatDateToLocale( date ) { |
|
|
return this.props.moment( date ).format( 'L' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getLocaleDateFormat() { |
|
|
return this.props.moment.localeData().longDateFormat( 'L' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clampDateToRange( date, { dateFrom, dateTo } ) { |
|
|
|
|
|
if ( dateFrom && date.isBefore( dateFrom ) ) { |
|
|
date = dateFrom; |
|
|
} |
|
|
|
|
|
if ( dateTo && date.isAfter( dateTo ) ) { |
|
|
date = dateTo; |
|
|
} |
|
|
|
|
|
return date; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
toDateString( date ) { |
|
|
if ( this.props.moment.isMoment( date ) || this.props.moment.isDate( date ) ) { |
|
|
return this.formatDateToLocale( this.props.moment( date ) ); |
|
|
} |
|
|
|
|
|
return this.getLocaleDateFormat(); |
|
|
} |
|
|
|
|
|
getNumberOfMonths() { |
|
|
return window.matchMedia( '(min-width: 520px)' ).matches ? 2 : 1; |
|
|
} |
|
|
|
|
|
handleDateRangeChange = ( startDate, endDate ) => { |
|
|
this.setState( { |
|
|
startDate, |
|
|
endDate, |
|
|
selectedShortcutId: null, |
|
|
textInputStartDate: this.toDateString( startDate ), |
|
|
textInputEndDate: this.toDateString( endDate ), |
|
|
} ); |
|
|
this.props.onDateSelect && this.props.onDateSelect( startDate, endDate ); |
|
|
}; |
|
|
|
|
|
handleDateRangeChangeByShortcutClick = ( startDate, endDate, shortcut ) => { |
|
|
this.handleDateRangeChange( startDate, endDate ); |
|
|
|
|
|
this.setState( { |
|
|
selectedShortcutId: shortcut.id, |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
handleShortcutClick = ( shortcut ) => { |
|
|
this.props.onShortcutClick( shortcut, this.closePopoverAndCommit ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderPopover() { |
|
|
const headerProps = { |
|
|
customTitle: this.props.customTitle, |
|
|
startDate: this.state.startDate, |
|
|
endDate: this.state.endDate, |
|
|
resetDates: this.resetDates, |
|
|
}; |
|
|
|
|
|
const footerProps = { |
|
|
onApplyClick: this.commitDates, |
|
|
onCancelClick: this.closePopoverAndRevert, |
|
|
}; |
|
|
|
|
|
const inputsProps = { |
|
|
startDateValue: this.state.textInputStartDate, |
|
|
endDateValue: this.state.textInputEndDate, |
|
|
onInputChange: this.handleInputChange, |
|
|
onInputBlur: this.handleInputBlur, |
|
|
onInputFocus: this.handleInputFocus, |
|
|
}; |
|
|
|
|
|
return ( |
|
|
<Popover |
|
|
className="date-range__popover" |
|
|
isVisible={ this.state.popoverVisible } |
|
|
context={ this.triggerButtonRef.current } |
|
|
position="bottom" |
|
|
onClose={ this.closePopover } |
|
|
> |
|
|
<div className="date-range__popover-content"> |
|
|
<div |
|
|
className={ clsx( 'date-range__popover-inner', { |
|
|
'date-range__popover-inner__hasoverlay': !! this.props.overlay, |
|
|
} ) } |
|
|
> |
|
|
{ this.props.overlay && ( |
|
|
<div className="date-range__popover-inner-overlay">{ this.props.overlay }</div> |
|
|
) } |
|
|
<div { ...( this.props.overlay && { 'aria-hidden': true, inert: '' } ) }> |
|
|
{ this.props.renderHeader( headerProps ) } |
|
|
{ this.props.renderInputs( inputsProps ) } |
|
|
{ this.renderDatePicker() } |
|
|
{ this.props.renderFooter( footerProps ) } |
|
|
</div> |
|
|
</div> |
|
|
{ /* Render shortcuts to the right of the calendar */ } |
|
|
{ this.props.displayShortcuts && ( |
|
|
<div className="date-range-picker-shortcuts"> |
|
|
<Shortcuts |
|
|
selectedShortcutId={ this.state.selectedShortcutId } |
|
|
shortcutList={ this.props.shortcutList } |
|
|
onClick={ this.handleDateRangeChangeByShortcutClick } |
|
|
locked={ !! this.props.overlay } |
|
|
startDate={ this.state.startDate } |
|
|
endDate={ this.state.endDate } |
|
|
onShortcutClick={ this.handleShortcutClick } // for tracking shortcut clicks |
|
|
/> |
|
|
</div> |
|
|
) } |
|
|
</div> |
|
|
</Popover> |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderDatePicker() { |
|
|
return ( |
|
|
<DateRangePicker |
|
|
firstSelectableDate={ this.props.firstSelectableDate } |
|
|
lastSelectableDate={ this.props.lastSelectableDate } |
|
|
selectedStartDate={ this.state.startDate } |
|
|
selectedEndDate={ this.state.endDate } |
|
|
onDateRangeChange={ this.handleDateRangeChange } |
|
|
focusedMonth={ this.state.focusedMonth } |
|
|
numberOfMonths={ this.state.numberOfMonths } |
|
|
useArrowNavigation={ this.props.useArrowNavigation } |
|
|
/> |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
render() { |
|
|
const rootClassNames = clsx( |
|
|
{ |
|
|
'date-range': true, |
|
|
'toggle-visible': this.state.popoverVisible, |
|
|
}, |
|
|
this.props.rootClass |
|
|
); |
|
|
|
|
|
const triggerProps = { |
|
|
startDate: this.state.startDate, |
|
|
endDate: this.state.endDate, |
|
|
startDateText: this.toDateString( this.state.startDate ), |
|
|
endDateText: this.toDateString( this.state.endDate ), |
|
|
buttonRef: this.triggerButtonRef, |
|
|
onTriggerClick: this.togglePopover, |
|
|
onClearClick: this.clearDates, |
|
|
triggerText: this.props.triggerText, |
|
|
isCompact: this.props.isCompact, |
|
|
showClearBtn: this.props.showTriggerClear, |
|
|
}; |
|
|
|
|
|
return ( |
|
|
<div className={ rootClassNames }> |
|
|
{ this.props.renderTrigger( triggerProps ) } |
|
|
{ this.renderPopover() } |
|
|
</div> |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
export default localize( withLocalizedMoment( DateRange ) ); |
|
|
|