2018-06-14 15:16:57 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2018-07-30 15:14:09 +00:00
|
|
|
import classnames from 'classnames';
|
2018-08-02 22:20:48 +00:00
|
|
|
import { Children, cloneElement } from '@wordpress/element';
|
|
|
|
import { Dropdown, NavigableMenu } from '@wordpress/components';
|
2018-06-14 15:16:57 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-30 15:14:09 +00:00
|
|
|
import { uniqueId } from 'lodash';
|
2018-06-14 15:16:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-08-02 22:20:48 +00:00
|
|
|
import { isMobileViewport, isTabletViewport } from 'lib/ui';
|
2018-06-14 15:16:57 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const SummaryList = ( { children, label } ) => {
|
|
|
|
if ( ! label ) {
|
2018-07-10 12:48:06 +00:00
|
|
|
label = __( 'Performance Indicators', 'wc-admin' );
|
2018-06-14 15:16:57 +00:00
|
|
|
}
|
2018-08-02 22:20:48 +00:00
|
|
|
const isDropdownBreakpoint = isTabletViewport() || isMobileViewport();
|
|
|
|
|
|
|
|
// We default to "one" because we can't have empty children.
|
|
|
|
const itemCount = Children.count( children ) || 1;
|
|
|
|
const hasItemsClass = itemCount < 10 ? `has-${ itemCount }-items` : 'has-10-items';
|
|
|
|
const classes = classnames( 'woocommerce-summary', {
|
|
|
|
[ hasItemsClass ]: ! isDropdownBreakpoint,
|
|
|
|
} );
|
2018-07-30 15:14:09 +00:00
|
|
|
|
|
|
|
const instanceId = uniqueId( 'woocommerce-summary-helptext-' );
|
2018-08-02 22:20:48 +00:00
|
|
|
const menu = (
|
|
|
|
<NavigableMenu
|
|
|
|
aria-label={ label }
|
|
|
|
aria-describedby={ instanceId }
|
|
|
|
orientation={ isDropdownBreakpoint ? 'vertical' : 'horizontal' }
|
|
|
|
stopNavigationEvents
|
|
|
|
>
|
2018-07-30 15:14:09 +00:00
|
|
|
<p id={ instanceId } className="screen-reader-text">
|
2018-08-02 22:20:48 +00:00
|
|
|
{ __(
|
|
|
|
'List of data points available for filtering. Use arrow keys to cycle through ' +
|
|
|
|
'the list. Click a data point for a detailed report.',
|
|
|
|
'wc-admin'
|
|
|
|
) }
|
2018-07-30 15:14:09 +00:00
|
|
|
</p>
|
|
|
|
<ul className={ classes }>{ children }</ul>
|
2018-08-02 22:20:48 +00:00
|
|
|
</NavigableMenu>
|
|
|
|
);
|
|
|
|
|
|
|
|
// On large screens, or if there are not multiple SummaryNumbers, we'll display the plain list.
|
|
|
|
if ( ! isDropdownBreakpoint || itemCount < 2 ) {
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
const items = Children.toArray( children );
|
|
|
|
const selected = items.find( item => !! item.props.selected );
|
|
|
|
if ( ! selected ) {
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
className="woocommerce-summary"
|
|
|
|
position="bottom"
|
|
|
|
headerTitle={ label }
|
|
|
|
expandOnMobile
|
|
|
|
renderToggle={ ( { onToggle } ) => cloneElement( selected, { onToggle } ) }
|
|
|
|
renderContent={ () => menu }
|
|
|
|
/>
|
2018-06-14 15:16:57 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SummaryList.propTypes = {
|
|
|
|
children: PropTypes.node.isRequired,
|
|
|
|
label: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export { SummaryList };
|
|
|
|
export { default as SummaryNumber } from './item';
|