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              176x   176x                                                           176x                 176x                     65x   65x 65x   65x                                     176x 176x 176x                 176x                                                                                
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps, 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
			{...omitProps<IBarProps>(passThroughs, undefined, _.keys(Bar.propTypes))}
			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\`*
 
		Bars are typically used for bar charts and are pretty much a thin
		wrapper around svg rects.
	`,
	categories: ['visualizations', 'geoms'],
};
Bar.propTypes = {
	style: object`
		Passed through to the root element.
	`,
 
	className: string`
		Appended to the component-specific class names set on the root element.
	`,
 
	x: number`
		x coordinate.
	`,
 
	y: number`
		y coordinate.
	`,
 
	height: PropTypes.oneOfType([number, string])`
		Height of the bar.
	`,
 
	width: PropTypes.oneOfType([number, string])`
		Width of the bar.
	`,
 
	hasStroke: bool`
		Determines if the bar has a white stroke around it.
	`,
 
	color: string`
		Strings should match an existing color class unless they start with a '#'
		for specific colors. E.g.:
 
		- \`COLOR_0\`
		- \`COLOR_GOOD\`
		- \`'#123abc'\`
	`,
};
 
export default Bar;