woocommerce/plugins/woocommerce-admin/client/components/date-picker/content.js

175 lines
4.9 KiB
JavaScript
Raw Normal View History

/** @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';
import PropTypes from 'prop-types';
2018-07-22 23:43:21 +00:00
import classnames from 'classnames';
/**
* Internal dependencies
*/
import ComparePeriods from './compare-periods';
import { H, Section } from 'layout/section';
import PresetPeriods from './preset-periods';
import Link from 'components/link';
2018-05-28 10:55:19 +00:00
import { DateRange } from 'components/calendar';
2018-07-22 23:43:21 +00:00
const isMobileViewport = () => window.innerWidth < 782;
class DatePickerContent extends Component {
2018-05-28 10:55:19 +00:00
constructor() {
super();
this.onTabSelect = this.onTabSelect.bind( this );
}
onTabSelect( tab ) {
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 ) {
onUpdate( { period: 'today' } );
2018-05-28 10:55:19 +00:00
}
}
render() {
2018-05-28 10:55:19 +00:00
const {
period,
compare,
after,
before,
onUpdate,
2018-05-28 10:55:19 +00:00
onClose,
getUpdatePath,
isValidSelection,
2018-07-22 23:43:21 +00:00
resetCustomValues,
focusedInput,
afterText,
beforeText,
afterError,
beforeError,
shortDateFormat,
2018-05-28 10:55:19 +00:00
} = this.props;
return (
2018-07-22 23:43:21 +00:00
<div>
<H className="screen-reader-text" tabIndex="0">
{ __( 'Select date range and comparison', 'wc-admin' ) }
</H>
<Section component={ false }>
<H className="woocommerce-date-picker__text">
{ __( 'select a date range', 'wc-admin' ) }
</H>
<TabPanel
tabs={ [
{
name: 'period',
title: __( 'Presets', 'wc-admin' ),
className: 'woocommerce-date-picker__tab',
},
{
name: 'custom',
title: __( 'Custom', 'wc-admin' ),
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 }
>
{ selectedTab => (
<Fragment>
{ selectedTab === 'period' && (
<PresetPeriods onSelect={ onUpdate } period={ period } />
) }
2018-05-28 10:55:19 +00:00
{ selectedTab === 'custom' && (
<DateRange
after={ after }
before={ before }
onUpdate={ onUpdate }
2018-05-28 10:55:19 +00:00
inValidDays="future"
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>
<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>
</Fragment>
) }
</TabPanel>
</Section>
2018-07-22 23:43:21 +00:00
</div>
);
}
}
DatePickerContent.propTypes = {
period: PropTypes.string.isRequired,
compare: PropTypes.string.isRequired,
onUpdate: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
getUpdatePath: PropTypes.func.isRequired,
2018-07-22 23:43:21 +00:00
resetCustomValues: PropTypes.func.isRequired,
focusedInput: PropTypes.string,
afterText: PropTypes.string,
beforeText: PropTypes.string,
afterError: PropTypes.string,
beforeError: PropTypes.string,
shortDateFormat: PropTypes.string.isRequired,
};
export default DatePickerContent;