2018-05-28 10:55:19 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-07-22 23:43:21 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-05-28 10:55:19 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
import {
|
|
|
|
DayPickerRangeController,
|
|
|
|
isInclusivelyAfterDay,
|
|
|
|
isInclusivelyBeforeDay,
|
|
|
|
} from 'react-dates';
|
|
|
|
import { partial } from 'lodash';
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import 'react-dates/lib/css/_datepicker.css';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-07-22 23:43:21 +00:00
|
|
|
import DateInput from './input';
|
2018-08-02 22:20:48 +00:00
|
|
|
import { isMobileViewport } from 'lib/ui';
|
2018-05-28 10:55:19 +00:00
|
|
|
import phrases from './phrases';
|
2018-08-02 22:20:48 +00:00
|
|
|
import { validateDateInputForRange } from 'lib/date';
|
2018-05-28 10:55:19 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
class DateRange extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
|
|
|
|
this.onDatesChange = this.onDatesChange.bind( this );
|
|
|
|
this.onFocusChange = this.onFocusChange.bind( this );
|
|
|
|
this.onInputChange = this.onInputChange.bind( this );
|
|
|
|
this.getOutsideRange = this.getOutsideRange.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
onDatesChange( { startDate, endDate } ) {
|
2018-07-22 23:43:21 +00:00
|
|
|
const { onUpdate, shortDateFormat } = this.props;
|
|
|
|
onUpdate( {
|
|
|
|
after: startDate,
|
|
|
|
before: endDate,
|
2018-07-02 02:51:00 +00:00
|
|
|
afterText: startDate ? startDate.format( shortDateFormat ) : '',
|
|
|
|
beforeText: endDate ? endDate.format( shortDateFormat ) : '',
|
2018-07-22 23:43:21 +00:00
|
|
|
afterError: null,
|
|
|
|
beforeError: null,
|
2018-05-28 10:55:19 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
onFocusChange( focusedInput ) {
|
2018-07-22 23:43:21 +00:00
|
|
|
this.props.onUpdate( {
|
|
|
|
focusedInput: ! focusedInput ? 'startDate' : focusedInput,
|
2018-05-28 10:55:19 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
onInputChange( input, event ) {
|
|
|
|
const value = event.target.value;
|
2018-07-22 23:43:21 +00:00
|
|
|
const { after, before, shortDateFormat } = this.props;
|
|
|
|
const { date, error } = validateDateInputForRange(
|
|
|
|
input,
|
|
|
|
value,
|
|
|
|
before,
|
|
|
|
after,
|
|
|
|
shortDateFormat
|
|
|
|
);
|
|
|
|
this.props.onUpdate( {
|
|
|
|
[ input ]: date,
|
2018-07-22 23:43:21 +00:00
|
|
|
[ input + 'Text' ]: value,
|
|
|
|
[ input + 'Error' ]: value.length > 0 ? error : null,
|
|
|
|
} );
|
2018-05-28 10:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getOutsideRange() {
|
|
|
|
const { inValidDays } = this.props;
|
|
|
|
if ( 'string' === typeof inValidDays ) {
|
|
|
|
switch ( inValidDays ) {
|
|
|
|
case 'past':
|
|
|
|
return day => isInclusivelyBeforeDay( day, moment() );
|
|
|
|
case 'future':
|
|
|
|
return day => isInclusivelyAfterDay( day, moment() );
|
|
|
|
case 'none':
|
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'function' === typeof inValidDays ? inValidDays : undefined;
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
setTnitialVisibleMonth( isDoubleCalendar, before ) {
|
|
|
|
return () => {
|
|
|
|
const visibleDate = before || moment();
|
|
|
|
if ( isDoubleCalendar ) {
|
|
|
|
return visibleDate.clone().subtract( 1, 'month' );
|
|
|
|
}
|
|
|
|
return visibleDate;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
render() {
|
2018-07-22 23:43:21 +00:00
|
|
|
const {
|
|
|
|
after,
|
|
|
|
before,
|
|
|
|
focusedInput,
|
|
|
|
afterText,
|
|
|
|
beforeText,
|
|
|
|
afterError,
|
|
|
|
beforeError,
|
|
|
|
shortDateFormat,
|
|
|
|
} = this.props;
|
2018-05-28 10:55:19 +00:00
|
|
|
const isOutsideRange = this.getOutsideRange();
|
|
|
|
const isMobile = isMobileViewport();
|
2018-07-22 23:43:21 +00:00
|
|
|
const isDoubleCalendar = isMobile && window.innerWidth > 624;
|
2018-05-28 10:55:19 +00:00
|
|
|
return (
|
2018-07-22 23:43:21 +00:00
|
|
|
<div
|
|
|
|
className={ classnames( 'woocommerce-calendar', {
|
|
|
|
'is-mobile': isMobile,
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
<div className="woocommerce-calendar__inputs">
|
|
|
|
<DateInput
|
2018-07-02 02:51:00 +00:00
|
|
|
value={ afterText }
|
|
|
|
onChange={ partial( this.onInputChange, 'after' ) }
|
2018-07-22 23:43:21 +00:00
|
|
|
dateFormat={ shortDateFormat }
|
|
|
|
label={ __( 'Start Date', 'wc-admin' ) }
|
|
|
|
error={ afterError }
|
|
|
|
describedBy={ sprintf(
|
2018-05-28 10:55:19 +00:00
|
|
|
__(
|
|
|
|
"Date input describing a selected date range's start date in format %s",
|
2018-07-10 12:48:06 +00:00
|
|
|
'wc-admin'
|
2018-05-28 10:55:19 +00:00
|
|
|
),
|
2018-06-27 03:54:01 +00:00
|
|
|
shortDateFormat
|
2018-05-28 10:55:19 +00:00
|
|
|
) }
|
2018-07-22 23:43:21 +00:00
|
|
|
/>
|
|
|
|
<div className="woocommerce-calendar__inputs-to">{ __( 'to', 'wc-admin' ) }</div>
|
|
|
|
<DateInput
|
2018-07-02 02:51:00 +00:00
|
|
|
value={ beforeText }
|
|
|
|
onChange={ partial( this.onInputChange, 'before' ) }
|
2018-07-22 23:43:21 +00:00
|
|
|
dateFormat={ shortDateFormat }
|
|
|
|
label={ __( 'End Date', 'wc-admin' ) }
|
|
|
|
error={ beforeError }
|
|
|
|
describedBy={ sprintf(
|
2018-05-28 10:55:19 +00:00
|
|
|
__(
|
|
|
|
"Date input describing a selected date range's end date in format %s",
|
2018-07-10 12:48:06 +00:00
|
|
|
'wc-admin'
|
2018-05-28 10:55:19 +00:00
|
|
|
),
|
2018-06-27 03:54:01 +00:00
|
|
|
shortDateFormat
|
2018-05-28 10:55:19 +00:00
|
|
|
) }
|
2018-07-22 23:43:21 +00:00
|
|
|
/>
|
2018-05-28 10:55:19 +00:00
|
|
|
</div>
|
2018-07-22 23:43:21 +00:00
|
|
|
<div className="woocommerce-calendar__react-dates">
|
2018-05-28 10:55:19 +00:00
|
|
|
<DayPickerRangeController
|
|
|
|
onDatesChange={ this.onDatesChange }
|
|
|
|
onFocusChange={ this.onFocusChange }
|
|
|
|
focusedInput={ focusedInput }
|
2018-07-02 02:51:00 +00:00
|
|
|
startDate={ after }
|
|
|
|
endDate={ before }
|
2018-05-28 10:55:19 +00:00
|
|
|
orientation={ 'horizontal' }
|
2018-07-22 23:43:21 +00:00
|
|
|
numberOfMonths={ isDoubleCalendar ? 2 : 1 }
|
2018-05-28 10:55:19 +00:00
|
|
|
isOutsideRange={ isOutsideRange }
|
|
|
|
minimumNights={ 0 }
|
|
|
|
hideKeyboardShortcutsPanel
|
|
|
|
noBorder
|
2018-07-22 23:43:21 +00:00
|
|
|
initialVisibleMonth={ this.setTnitialVisibleMonth( isDoubleCalendar, before ) }
|
2018-05-28 10:55:19 +00:00
|
|
|
phrases={ phrases }
|
2018-07-04 01:50:12 +00:00
|
|
|
firstDayOfWeek={ Number( wcSettings.date.dow ) }
|
2018-05-28 10:55:19 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2018-07-22 23:43:21 +00:00
|
|
|
</div>
|
2018-05-28 10:55:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DateRange.propTypes = {
|
2018-07-02 02:51:00 +00:00
|
|
|
after: PropTypes.object,
|
|
|
|
before: PropTypes.object,
|
2018-07-22 23:43:21 +00:00
|
|
|
onUpdate: PropTypes.func.isRequired,
|
2018-05-28 10:55:19 +00:00
|
|
|
inValidDays: PropTypes.oneOfType( [
|
|
|
|
PropTypes.oneOf( [ 'past', 'future', 'none' ] ),
|
|
|
|
PropTypes.func,
|
|
|
|
] ),
|
2018-07-22 23:43:21 +00:00
|
|
|
focusedInput: PropTypes.string,
|
|
|
|
afterText: PropTypes.string,
|
|
|
|
beforeText: PropTypes.string,
|
|
|
|
afterError: PropTypes.string,
|
|
|
|
beforeError: PropTypes.string,
|
|
|
|
shortDateFormat: PropTypes.string.isRequired,
|
2018-05-28 10:55:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { DateRange };
|