All files / Bar Bar.tsx

100% Statements 12/12
100% Branches 2/2
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                5x   5x                                                           5x                 5x                     324x   324x 324x   324x                                     5x 5x 5x       5x                                                                                                
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
 
import { lucidClassNames } from '../../util/style-helpers';
import { StandardProps } from '../../util/component-types';
import * as chartConstants from '../../constants/charts';
 
const cx = lucidClassNames.bind('&-Bar');
 
const { number, bool, string, object } = PropTypes;
 
export interface IBarProps
	extends StandardProps,
		React.SVGProps<SVGRectElement> {
	/** x coordinate. */
	x: number;
 
	/** y coordinate. */
	y: number;
 
	/** Height of the bar. */
	height: number | string;
 
	/** Width of the bar. */
	width: number | string;
 
	/** Determines if the bar has a white stroke around it. */
	hasStroke: boolean;
 
	/** Strings should match an existing color class unless they start with a '#'
		for specific colors. E.g.:
 
		- \`COLOR_0\`
		- \`COLOR_GOOD\`
		- \`'#123abc'\`
	 */
	color: string;
}
 
const defaultProps = {
	x: 0,
	y: 0,
	height: 0,
	width: 0,
	color: chartConstants.COLOR_0,
	hasStroke: false,
};
 
export const Bar = (props: IBarProps): React.ReactElement => {
	const {
		className,
		color,
		hasStroke,
		height,
		width,
		style,
		x,
		y,
		...passThroughs
	} = props;
 
	const isCustomColor = _.startsWith(color, '#');
	const colorStyle = isCustomColor ? { fill: color } : null;
 
	return (
		<rect
			{...passThroughs}
			className={cx(className, '&', {
				'&-has-stroke': hasStroke,
				[`&-${color}`]: !isCustomColor,
			})}
			x={x}
			y={y}
			height={height}
			width={width}
			style={{
				...style,
				...colorStyle,
			}}
		/>
	);
};
 
Bar.defaultProps = defaultProps;
Bar.displayName = 'Bar';
Bar.peek = {
	description: `*For use within an \`svg\`*. A \`Bar\` is typically used for a \`Bar Chart\` and is pretty much a thin wrapper around an \`svg rect\`.`,
	categories: ['visualizations', 'geoms'],
};
Bar.propTypes = {
	/**
		Passed through to the root element.
	*/
	style: object,
 
	/**
		Appended to the component-specific class names set on the root element.
	*/
	className: string,
 
	/**
		x coordinate.
	*/
	x: number,
 
	/**
		y coordinate.
	*/
	y: number,
 
	/**
		Height of the bar.
	*/
	height: PropTypes.oneOfType([number, string]),
 
	/**
		Width of the bar.
	*/
	width: PropTypes.oneOfType([number, string]),
 
	/**
		Determines if the bar has a white stroke around it.
	*/
	hasStroke: bool,
 
	/**
		Strings should match an existing color class unless they start with a '#'
		for specific colors. E.g.:
 
		- \`COLOR_0\`
		- \`COLOR_GOOD\`
		- \`'#123abc'\`
	*/
	color: string,
};
 
export default Bar;