2018-05-28 10:55:19 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-05-28 10:55:19 +00:00
|
|
|
import { TabPanel, Button } from '@wordpress/components';
|
2018-05-28 10:55:19 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-22 23:43:21 +00:00
|
|
|
import classnames from 'classnames';
|
2018-05-28 10:55:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ComparePeriods from './compare-periods';
|
2018-06-20 15:10:06 +00:00
|
|
|
import { H, Section } from 'layout/section';
|
|
|
|
import PresetPeriods from './preset-periods';
|
2018-06-26 14:49:42 +00:00
|
|
|
import Link from 'components/link';
|
2018-05-28 10:55:19 +00:00
|
|
|
import { DateRange } from 'components/calendar';
|
2018-05-28 10:55:19 +00:00
|
|
|
|
2018-07-22 23:43:21 +00:00
|
|
|
const isMobileViewport = () => window.innerWidth < 782;
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
class DatePickerContent extends Component {
|
2018-05-28 10:55:19 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.onTabSelect = this.onTabSelect.bind( this );
|
|
|
|
}
|
|
|
|
onTabSelect( tab ) {
|
2018-07-22 23:43:21 +00:00
|
|
|
const { onUpdate, period } = this.props;
|
2018-05-28 10:55:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the period is `custom` and the user switches tabs to view the presets,
|
|
|
|
* then a preset should be selected. This logic selects the default, otherwise
|
|
|
|
* `custom` value for period will result in no selection.
|
|
|
|
*/
|
|
|
|
if ( 'period' === tab && 'custom' === period ) {
|
2018-07-22 23:43:21 +00:00
|
|
|
onUpdate( { period: 'today' } );
|
2018-05-28 10:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 10:55:19 +00:00
|
|
|
render() {
|
2018-05-28 10:55:19 +00:00
|
|
|
const {
|
|
|
|
period,
|
|
|
|
compare,
|
2018-07-02 02:51:00 +00:00
|
|
|
after,
|
|
|
|
before,
|
2018-07-22 23:43:21 +00:00
|
|
|
onUpdate,
|
2018-05-28 10:55:19 +00:00
|
|
|
onClose,
|
|
|
|
getUpdatePath,
|
|
|
|
isValidSelection,
|
2018-07-22 23:43:21 +00:00
|
|
|
resetCustomValues,
|
2018-07-22 23:43:21 +00:00
|
|
|
focusedInput,
|
|
|
|
afterText,
|
|
|
|
beforeText,
|
|
|
|
afterError,
|
|
|
|
beforeError,
|
|
|
|
shortDateFormat,
|
2018-05-28 10:55:19 +00:00
|
|
|
} = this.props;
|
2018-05-28 10:55:19 +00:00
|
|
|
return (
|
2018-07-22 23:43:21 +00:00
|
|
|
<div>
|
2018-06-20 15:10:06 +00:00
|
|
|
<H className="screen-reader-text" tabIndex="0">
|
2018-07-10 12:48:06 +00:00
|
|
|
{ __( 'Select date range and comparison', 'wc-admin' ) }
|
2018-06-20 15:10:06 +00:00
|
|
|
</H>
|
|
|
|
<Section component={ false }>
|
|
|
|
<H className="woocommerce-date-picker__text">
|
2018-07-10 12:48:06 +00:00
|
|
|
{ __( 'select a date range', 'wc-admin' ) }
|
2018-06-20 15:10:06 +00:00
|
|
|
</H>
|
|
|
|
<TabPanel
|
|
|
|
tabs={ [
|
|
|
|
{
|
|
|
|
name: 'period',
|
2018-07-10 12:48:06 +00:00
|
|
|
title: __( 'Presets', 'wc-admin' ),
|
2018-06-20 15:10:06 +00:00
|
|
|
className: 'woocommerce-date-picker__tab',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'custom',
|
2018-07-10 12:48:06 +00:00
|
|
|
title: __( 'Custom', 'wc-admin' ),
|
2018-06-20 15:10:06 +00:00
|
|
|
className: 'woocommerce-date-picker__tab',
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
className="woocommerce-date-picker__tabs"
|
|
|
|
activeClass="is-active"
|
2018-05-28 10:55:19 +00:00
|
|
|
initialTabName={
|
|
|
|
'custom' === period
|
|
|
|
? 'custom'
|
|
|
|
: 'period' /* Open to current tab https://github.com/WordPress/gutenberg/pull/6885 */
|
|
|
|
}
|
|
|
|
onSelect={ this.onTabSelect }
|
2018-06-20 15:10:06 +00:00
|
|
|
>
|
|
|
|
{ selectedTab => (
|
|
|
|
<Fragment>
|
|
|
|
{ selectedTab === 'period' && (
|
2018-07-22 23:43:21 +00:00
|
|
|
<PresetPeriods onSelect={ onUpdate } period={ period } />
|
2018-06-20 15:10:06 +00:00
|
|
|
) }
|
2018-05-28 10:55:19 +00:00
|
|
|
{ selectedTab === 'custom' && (
|
|
|
|
<DateRange
|
2018-07-02 02:51:00 +00:00
|
|
|
after={ after }
|
|
|
|
before={ before }
|
2018-07-22 23:43:21 +00:00
|
|
|
onUpdate={ onUpdate }
|
2018-05-28 10:55:19 +00:00
|
|
|
inValidDays="future"
|
2018-07-22 23:43:21 +00:00
|
|
|
focusedInput={ focusedInput }
|
|
|
|
afterText={ afterText }
|
|
|
|
beforeText={ beforeText }
|
|
|
|
afterError={ afterError }
|
|
|
|
beforeError={ beforeError }
|
|
|
|
shortDateFormat={ shortDateFormat }
|
2018-05-28 10:55:19 +00:00
|
|
|
/>
|
|
|
|
) }
|
2018-07-22 23:43:21 +00:00
|
|
|
<div
|
|
|
|
className={ classnames( 'woocommerce-date-picker__content-controls', {
|
|
|
|
'is-sticky-bottom': selectedTab === 'custom' && isMobileViewport(),
|
|
|
|
'is-custom': selectedTab === 'custom',
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
<H className="woocommerce-date-picker__text">
|
|
|
|
{ __( 'compare to', 'wc-admin' ) }
|
|
|
|
</H>
|
2018-07-22 23:43:21 +00:00
|
|
|
<ComparePeriods onSelect={ onUpdate } compare={ compare } />
|
2018-07-22 23:43:21 +00:00
|
|
|
<div className="woocommerce-date-picker__content-controls-btns">
|
|
|
|
{ selectedTab === 'custom' && (
|
|
|
|
<Button
|
|
|
|
className="woocommerce-date-picker__content-controls-btn"
|
|
|
|
isPrimary
|
|
|
|
onClick={ resetCustomValues }
|
|
|
|
disabled={ ! ( after || before ) }
|
|
|
|
>
|
|
|
|
{ __( 'Reset', 'wc-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
{ isValidSelection( selectedTab ) ? (
|
|
|
|
<Link
|
|
|
|
/* eslint-disable max-len */
|
|
|
|
className="woocommerce-date-picker__content-controls-btn components-button is-button is-primary"
|
|
|
|
/* eslint-enable max-len */
|
|
|
|
href={ getUpdatePath( selectedTab ) }
|
|
|
|
onClick={ onClose }
|
|
|
|
>
|
|
|
|
{ __( 'Update', 'wc-admin' ) }
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
className="woocommerce-date-picker__content-controls-btn"
|
|
|
|
isPrimary
|
|
|
|
disabled
|
|
|
|
>
|
|
|
|
{ __( 'Update', 'wc-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-06-20 15:10:06 +00:00
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</TabPanel>
|
|
|
|
</Section>
|
2018-07-22 23:43:21 +00:00
|
|
|
</div>
|
2018-05-28 10:55:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DatePickerContent.propTypes = {
|
|
|
|
period: PropTypes.string.isRequired,
|
|
|
|
compare: PropTypes.string.isRequired,
|
2018-07-22 23:43:21 +00:00
|
|
|
onUpdate: PropTypes.func.isRequired,
|
2018-05-28 10:55:19 +00:00
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
getUpdatePath: PropTypes.func.isRequired,
|
2018-07-22 23:43:21 +00:00
|
|
|
resetCustomValues: PropTypes.func.isRequired,
|
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 default DatePickerContent;
|