import clsx from 'clsx';
import PropTypes from 'prop-types';
import { PureComponent } from 'react';
import ChartBarTooltip from './bar-tooltip';
export default class ChartBar extends PureComponent {
static propTypes = {
className: PropTypes.string,
clickHandler: PropTypes.func,
count: PropTypes.number,
data: PropTypes.object.isRequired,
isTouch: PropTypes.bool,
max: PropTypes.number,
tooltipPosition: PropTypes.string,
};
static defaultProps = {
max: Infinity,
};
clickHandler = () => {
if ( typeof this.props.clickHandler === 'function' ) {
this.props.clickHandler( this.props.data );
}
};
computeTooltipPosition() {
const { chartWidth, index, count } = this.props;
const barWidth = chartWidth / count;
const barOffset = barWidth * ( index + 1 );
const shouldFlip = barOffset > chartWidth - 230 && barOffset + barWidth > 230;
return shouldFlip ? 'bottom left' : 'bottom right';
}
mouseEnter = () => {
if (
! this.props.data.tooltipData ||
! this.props.data.tooltipData.length ||
this.props.isTouch
) {
return null;
}
this.props.setTooltip( this.bar, this.computeTooltipPosition(), this.getTooltipData() );
};
mouseLeave = () => {
this.props.setTooltip( null );
};
getTooltipData() {
return this.props.data.tooltipData.map( function ( options, i ) {
return