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';
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A container element for a list of SummaryNumbers. This component handles detecting & switching to
|
|
|
|
* the mobile format on smaller screens.
|
|
|
|
*
|
|
|
|
* @return { object } -
|
|
|
|
*/
|
2018-06-14 15:16:57 +00:00
|
|
|
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 }
|
2018-08-13 15:25:11 +00:00
|
|
|
renderToggle={ ( { isOpen, onToggle } ) => cloneElement( selected, { onToggle, isOpen } ) }
|
2018-08-02 22:20:48 +00:00
|
|
|
renderContent={ () => menu }
|
|
|
|
/>
|
2018-06-14 15:16:57 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SummaryList.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A list of `<SummaryNumber />`s
|
|
|
|
*/
|
2018-06-14 15:16:57 +00:00
|
|
|
children: PropTypes.node.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An optional label of this group, read to screen reader users. Defaults to "Performance Indicators".
|
|
|
|
*/
|
2018-06-14 15:16:57 +00:00
|
|
|
label: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
2018-08-31 17:19:13 +00:00
|
|
|
export default SummaryList;
|