2018-07-11 15:23:16 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
import { formatCurrency } from 'lib/currency';
|
2018-08-13 10:19:32 +00:00
|
|
|
import { getColor } from './utils';
|
2018-07-11 15:23:16 +00:00
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A legend specifically designed for the WooCommerce admin charts.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
class Legend extends Component {
|
|
|
|
render() {
|
2018-08-13 10:19:32 +00:00
|
|
|
const {
|
|
|
|
colorScheme,
|
|
|
|
data,
|
|
|
|
handleLegendHover,
|
|
|
|
handleLegendToggle,
|
|
|
|
legendDirection,
|
|
|
|
} = this.props;
|
|
|
|
const colorParams = {
|
|
|
|
orderedKeys: data,
|
|
|
|
colorScheme,
|
|
|
|
};
|
2018-07-11 15:23:16 +00:00
|
|
|
return (
|
|
|
|
<ul
|
|
|
|
className={ classNames(
|
|
|
|
'woocommerce-legend',
|
|
|
|
`woocommerce-legend__direction-${ legendDirection }`,
|
|
|
|
this.props.className
|
|
|
|
) }
|
|
|
|
>
|
2018-08-13 10:19:32 +00:00
|
|
|
{ data.map( row => (
|
2018-07-11 15:23:16 +00:00
|
|
|
<li
|
2018-07-25 14:58:32 +00:00
|
|
|
className="woocommerce-legend__item"
|
|
|
|
key={ row.key }
|
2018-07-11 15:23:16 +00:00
|
|
|
id={ row.key }
|
|
|
|
onMouseEnter={ handleLegendHover }
|
|
|
|
onMouseLeave={ handleLegendHover }
|
|
|
|
onBlur={ handleLegendHover }
|
|
|
|
onFocus={ handleLegendHover }
|
|
|
|
>
|
|
|
|
<button onClick={ handleLegendToggle } id={ row.key }>
|
|
|
|
<div className="woocommerce-legend__item-container" id={ row.key }>
|
|
|
|
<span
|
|
|
|
className={ classNames( 'woocommerce-legend__item-checkmark', {
|
|
|
|
'woocommerce-legend__item-checkmark-checked': row.visible,
|
|
|
|
} ) }
|
|
|
|
id={ row.key }
|
2018-08-13 10:19:32 +00:00
|
|
|
style={ { backgroundColor: getColor( row.key, colorParams ) } }
|
2018-07-11 15:23:16 +00:00
|
|
|
/>
|
|
|
|
<span className="woocommerce-legend__item-title" id={ row.key }>
|
|
|
|
{ row.key }
|
|
|
|
</span>
|
|
|
|
<span className="woocommerce-legend__item-total" id={ row.key }>
|
|
|
|
{ formatCurrency( row.total ) }
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
) ) }
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Legend.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Additional CSS classes.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
className: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A chromatic color function to be passed down to d3.
|
|
|
|
*/
|
2018-08-13 10:19:32 +00:00
|
|
|
colorScheme: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of `orderedKeys`.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
data: PropTypes.array.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Handles `onClick` event.
|
|
|
|
*/
|
2018-07-25 14:58:32 +00:00
|
|
|
handleLegendToggle: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Handles `onMouseEnter`/`onMouseLeave` events.
|
|
|
|
*/
|
2018-07-25 14:58:32 +00:00
|
|
|
handleLegendHover: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Display legend items as a `row` or `column` inside a flex-box.
|
|
|
|
*/
|
2018-07-11 15:23:16 +00:00
|
|
|
legendDirection: PropTypes.oneOf( [ 'row', 'column' ] ),
|
|
|
|
};
|
|
|
|
|
|
|
|
Legend.defaultProps = {
|
|
|
|
legendDirection: 'row',
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Legend;
|