2018-06-14 15:16:57 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-07-30 15:14:09 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2018-08-02 22:20:48 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2018-06-14 15:16:57 +00:00
|
|
|
import classnames from 'classnames';
|
2018-07-30 15:14:09 +00:00
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import { isUndefined } from 'lodash';
|
2018-06-14 15:16:57 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2018-07-30 15:14:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import Link from 'components/link';
|
2018-06-14 15:16:57 +00:00
|
|
|
|
2018-07-30 15:14:09 +00:00
|
|
|
const SummaryNumber = ( {
|
|
|
|
delta,
|
|
|
|
href,
|
2018-08-13 15:25:11 +00:00
|
|
|
isOpen,
|
2018-07-30 15:14:09 +00:00
|
|
|
label,
|
2018-08-02 22:20:48 +00:00
|
|
|
onToggle,
|
2018-07-30 15:14:09 +00:00
|
|
|
prevLabel,
|
|
|
|
prevValue,
|
|
|
|
reverseTrend,
|
|
|
|
selected,
|
|
|
|
value,
|
|
|
|
} ) => {
|
2018-08-02 22:20:48 +00:00
|
|
|
const liClasses = classnames( 'woocommerce-summary__item-container', {
|
|
|
|
'is-dropdown-button': onToggle,
|
2018-08-13 15:25:11 +00:00
|
|
|
'is-dropdown-expanded': isOpen,
|
2018-08-02 22:20:48 +00:00
|
|
|
} );
|
2018-06-14 15:16:57 +00:00
|
|
|
const classes = classnames( 'woocommerce-summary__item', {
|
|
|
|
'is-selected': selected,
|
2018-07-30 15:14:09 +00:00
|
|
|
'is-good-trend': reverseTrend ? delta < 0 : delta > 0,
|
|
|
|
'is-bad-trend': reverseTrend ? delta > 0 : delta < 0,
|
2018-06-14 15:16:57 +00:00
|
|
|
} );
|
|
|
|
|
2018-07-30 15:14:09 +00:00
|
|
|
let icon = delta > 0 ? 'arrow-up' : 'arrow-down';
|
|
|
|
let screenReaderLabel =
|
|
|
|
delta > 0
|
|
|
|
? sprintf( __( 'Up %d%% from %s', 'wc-admin' ), delta, prevLabel )
|
|
|
|
: sprintf( __( 'Down %d%% from %s', 'wc-admin' ), Math.abs( delta ), prevLabel );
|
|
|
|
if ( ! delta ) {
|
|
|
|
// delta is zero or falsey
|
|
|
|
icon = 'arrow-right';
|
|
|
|
screenReaderLabel = sprintf( __( 'No change from %s', 'wc-admin' ), prevLabel );
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:20:48 +00:00
|
|
|
const Container = onToggle ? Button : Link;
|
|
|
|
const containerProps = {
|
|
|
|
className: classes,
|
|
|
|
'aria-current': selected ? 'page' : null,
|
|
|
|
};
|
|
|
|
if ( ! onToggle ) {
|
|
|
|
containerProps.href = href;
|
|
|
|
containerProps.role = 'menuitem';
|
|
|
|
} else {
|
|
|
|
containerProps.onClick = onToggle;
|
2018-08-13 15:25:11 +00:00
|
|
|
containerProps[ 'aria-expanded' ] = isOpen;
|
2018-08-02 22:20:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 15:16:57 +00:00
|
|
|
return (
|
2018-08-02 22:20:48 +00:00
|
|
|
<li className={ liClasses }>
|
|
|
|
<Container { ...containerProps }>
|
2018-07-30 15:14:09 +00:00
|
|
|
<span className="woocommerce-summary__item-label">{ label }</span>
|
|
|
|
|
|
|
|
<span className="woocommerce-summary__item-data">
|
|
|
|
<span className="woocommerce-summary__item-value">{ value }</span>
|
|
|
|
<div
|
|
|
|
className="woocommerce-summary__item-delta"
|
|
|
|
role="presentation"
|
|
|
|
aria-label={ screenReaderLabel }
|
|
|
|
>
|
|
|
|
<Gridicon className="woocommerce-summary__item-delta-icon" icon={ icon } size={ 18 } />
|
|
|
|
<span className="woocommerce-summary__item-delta-value">
|
|
|
|
{ ! isUndefined( delta )
|
|
|
|
? sprintf( __( '%d%%', 'wc-admin' ), delta )
|
|
|
|
: __( 'N/A', 'wc-admin' ) }
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
<span className="woocommerce-summary__item-prev-label">{ prevLabel }</span>
|
|
|
|
{ ' ' /* Add a real space so the line breaks here, and not in the label text. */ }
|
|
|
|
<span className="woocommerce-summary__item-prev-value">
|
|
|
|
{ ! isUndefined( prevValue ) ? prevValue : __( 'N/A', 'wc-admin' ) }
|
2018-06-14 15:16:57 +00:00
|
|
|
</span>
|
2018-08-02 22:20:48 +00:00
|
|
|
|
|
|
|
{ onToggle ? (
|
|
|
|
<Gridicon className="woocommerce-summary__toggle" icon="chevron-down" size={ 24 } />
|
|
|
|
) : null }
|
|
|
|
</Container>
|
2018-06-14 15:16:57 +00:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SummaryNumber.propTypes = {
|
|
|
|
delta: PropTypes.number,
|
2018-07-30 15:14:09 +00:00
|
|
|
href: PropTypes.string.isRequired,
|
2018-08-13 15:25:11 +00:00
|
|
|
isOpen: PropTypes.bool,
|
2018-06-14 15:16:57 +00:00
|
|
|
label: PropTypes.string.isRequired,
|
2018-08-02 22:20:48 +00:00
|
|
|
onToggle: PropTypes.func,
|
2018-07-30 15:14:09 +00:00
|
|
|
prevLabel: PropTypes.string,
|
|
|
|
prevValue: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] ),
|
|
|
|
reverseTrend: PropTypes.bool,
|
2018-06-14 15:16:57 +00:00
|
|
|
selected: PropTypes.bool,
|
|
|
|
value: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] ).isRequired,
|
|
|
|
};
|
|
|
|
|
2018-07-30 15:14:09 +00:00
|
|
|
SummaryNumber.defaultProps = {
|
|
|
|
href: '/analytics',
|
2018-08-13 15:25:11 +00:00
|
|
|
isOpen: false,
|
2018-07-30 15:14:09 +00:00
|
|
|
prevLabel: __( 'Previous Period:', 'wc-admin' ),
|
|
|
|
reverseTrend: false,
|
|
|
|
selected: false,
|
|
|
|
};
|
|
|
|
|
2018-06-14 15:16:57 +00:00
|
|
|
export default SummaryNumber;
|