All files / components/Legend Legend.jsx

82.35% Statements 14/17
90.48% Branches 19/21
100% Functions 2/2
81.25% Lines 13/16
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                120x   120x 120x               120x                       120x                                                       120x                                                                                                     24x   24x 24x 24x 28x   24x                                     57x                                                                        
import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, findTypes, omitProps } from '../../util/component-types';
 
import Point from '../Point/Point';
import Line from '../Line/Line';
 
const cx = lucidClassNames.bind('&-Legend');
 
const POINT_SIZE = 12;
const LINE_WIDTH = 22;
 
const {
	number,
	string,
	oneOf,
	bool,
	func,
} = React.PropTypes;
 
/**
 * {"categories": ["visualizations", "chart primitives"]}
 *
 * Contrary to the other chart primitives, this component is not rendered in
 * svg. In order to sanely render horizontal legends, we need to know the width
 * of the text elements ahead of rendering time. Since we're using a variable
 * width font, the only way I know of to correctly get the width is with the
 * DOM function `getComputedTextLength`. Variable widths are much more easy to
 * implement outside of svg.
 */
const Legend = createClass({
	displayName: 'Legend',
 
	statics: {
		HEIGHT: 28, // exposed for consumer convenience
	},
 
	propTypes: {
		/**
		 * Appended to the component-specific class names set on the root element.
		 */
		className: string,
		/**
		 * Determine orientation of the legend.
		 */
		orient: oneOf(['horizontal', 'vertical']),
		/**
		 * Show the legend borders. Turn this off if you want to put the legend in
		 * a `ToolTip` for example.
		 */
		hasBorders: bool,
		/**
		 * Reverse the order of items in the legend.
		 */
		isReversed: bool,
	},
 
	getDefaultProps() {
		return {
			orient: 'vertical',
			hasBorders: true,
			isReversed: false,
		};
	},
 
	components: {
		/**
		 * Renders a `<li>` that describes the data series.
		 */
		Item: createClass({
			displayName: 'Legend.Item',
			propsName: 'Item',
			propTypes: {
				hasPoint: bool,
				hasLine: 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,
				pointKind: number,
				onClick: func,
				/**
				 * Class names that are appended to the defaults.
				 */
				className: string,
			},
		}),
	},
 
	handleItemClick(index, props, event) {
		if (!props.onClick) {
			return null;
		}
 
		props.onClick(index, { props, event });
	},
 
	render() {
		const {
			orient,
			className,
			hasBorders,
			isReversed,
			...passThroughs,
		} = this.props;
 
		const isHorizontal = orient === 'horizontal';
		const isVertical = orient === 'vertical';
		const itemProps = _.map(findTypes(this.props, Legend.Item), 'props');
		const hasSomeLines = isVertical && _.some(itemProps, ({ hasLine }) => hasLine);
 
		return (
			<ul
				{...omitProps(passThroughs, Legend)}
				className={cx(className, '&', {
					'&-is-horizontal': isHorizontal,
					'&-is-vertical': isVertical,
					'&-has-borders': hasBorders,
					'&-is-reversed': isReversed,
				})}
			>
				{_.map(itemProps, ({
					hasLine,
					hasPoint,
					pointKind = 1,
					color,
					onClick,
					children,
					className: itemClass,
				}, index) => (
					<li
						key={index}
						className={cx(itemClass, '&-Item')}
						onClick={_.partial(this.handleItemClick, index, itemProps[index])}
					>
						{hasPoint || hasLine ?
							<svg
								className={cx('&-Item-indicator')}
								width={hasLine || hasSomeLines ? LINE_WIDTH : POINT_SIZE}
								height={POINT_SIZE}
							>
								{hasPoint ?
									<Point
										x={hasLine || hasSomeLines ? LINE_WIDTH / 2 : POINT_SIZE / 2}
										y={POINT_SIZE / 2}
										color={color}
										kind={pointKind}
									/>
								: null}
								{hasLine ?
									<Line
										d={`M0,${POINT_SIZE / 2} L${LINE_WIDTH},${POINT_SIZE / 2}`}
										color={color}
									/>
								: null}
							</svg>
						: null}
						{children}
					</li>
				))}
			</ul>
		);
	},
});
 
export default Legend;