All files / components/Bar Bar.jsx

100% Statements 8/8
100% Branches 2/2
100% Functions 0/0
100% Lines 8/8
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            120x             120x                     120x                                                                                       120x                                       58x   58x 58x   58x                                          
import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, omitProps } from '../../util/component-types';
import * as chartConstants from '../../constants/charts';
 
const cx = lucidClassNames.bind('&-Bar');
 
const {
	number,
	bool,
	string,
	object,
} = React.PropTypes;
 
/**
 * {"categories": ["visualizations", "geoms"]}
 *
 * *For use within an `svg`*
 *
 * Bars are typically used for bar charts and are pretty much a thin wrapper
 * around svg rects.
 *
 */
const Bar = createClass({
	displayName: '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: number,
		/**
		 * Width of the bar.
		 */
		width: number,
		/**
		 * 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,
	},
 
	getDefaultProps() {
		return {
			x: 0,
			y: 0,
			height: 0,
			width: 0,
			color: chartConstants.COLOR_0,
		};
	},
 
	render() {
		const {
			className,
			color,
			hasStroke,
			height,
			width,
			style,
			x,
			y,
			...passThroughs,
		} = this.props;
 
		const isCustomColor = _.startsWith(color, '#');
		const colorStyle = isCustomColor ? { fill: color } : null;
 
		return (
			<rect
				{...omitProps(passThroughs, Bar)}
				className={cx(className, '&', {
					'&-has-stroke': hasStroke,
					[`&-${color}`]: !isCustomColor,
				})}
				x={x}
				y={y}
				height={height}
				width={width}
				style={{
					...style,
					...colorStyle,
				}}
			/>
		);
	},
});
 
export default Bar;