All files / components/ToolTip ToolTip.jsx

88.46% Statements 23/26
100% Branches 15/15
100% Functions 2/2
88% Lines 22/25
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257                120x 120x                     120x         120x                   120x                                                                                                                                                                                                 240x                             52x             2x                   2x 2x 1x           1x                 3x 3x       2x 2x                                       58x   58x 58x 58x 58x           58x                                                                            
import _ from 'lodash';
import React from 'react';
import ContextMenu from '../ContextMenu/ContextMenu';
import CrossIcon from '../Icon/CrossIcon/CrossIcon';
import * as reducers from './ToolTip.reducers';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, findTypes, omitProps } from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-ToolTip');
const flyOutCx = cx.bind('&-FlyOut');
 
const {
	bool,
	func,
	node,
	number,
	object,
	oneOf,
	string,
	oneOfType,
} = React.PropTypes;
 
const {
	Target,
	FlyOut,
} = ContextMenu;
 
 
/**
 * {"categories": ["communication"], "madeFrom": ["ContextMenu"]}
 *
 * A utility component that creates a transient message anchored to another
 * component.
 */
 
const ToolTip = createClass({
	displayName: 'ToolTip',
 
	reducers,
 
	propTypes: {
		/**
		 * `children` should include exactly one ToolTip.Target and one ToolTip.FlyOut.
		 */
		children: node,
		/**
		 * Appended to the component-specific class names set on the root element.
		 */
		className: string,
		/**
		 * Set this to `true` if you want to have a `x` close icon.
		 */
		isCloseable: bool,
		/**
		 * Style variations of the `ToolTip`.
		 */
		kind: oneOf([
			'primary',
			'success',
			'warning',
			'danger',
			'info',
			'default',
		]),
		/**
		 * Called when the user closes the `Banner`.
		 *
		 * Signature: `({ event, props }) => {}`
		 */
		onClose: func,
		/**
		 * Passed through to the root target element.
		 */
		style: object,
		/**
		 * Passed through to the root FlyOut element.
		 */
		flyOutStyle: object,
		/**
		 * maximum width of the ToolTip FlyOut. Defaults to 200px.
		 */
		flyOutMaxWidth: oneOfType([number, string]),
		/**
		 * direction of the FlyOut relative to Target. Defaults to `'down'`.
		 */
		direction: oneOf(['down', 'up', 'right', 'left']),
		/**
		 * alignment of the Flyout relative to Target in the cross axis from `direction` Defaults to `'start'`
		 */
		alignment: oneOf(['start', 'center', 'end']),
		/**
		 * Indicates whether the ToolTip will render or not. Defaults to `true`.
		 */
		isExpanded: bool,
		/**
		 * Called when cursor moves over the target
		 * Signature: `({ props, event }) => {}`
		 */
		onMouseOver: func,
		/**
		 * Called when cursor leaves the target and the ToolTip
		 * Signature: `({ props, event }) => {}`
		 */
		onMouseOut: func,
		/**
		 * The `id` of the FlyOut portal element that is appended to `document.body`. Defaults to a generated `id`.
		 */
		portalId: string,
	},
 
	components: {
		/**
		 * The hover target that will trigger the ToolTip to be displayed.
		 */
		Target: createClass({
			displayName: 'ToolTip.Target',
		}),
		/**
		 * The title displayed at the top of the ToolTip.
		 */
		Title: createClass({
			displayName: 'ToolTip.Title',
		}),
		/**
		 * The body of the ToolTip displayed below the Title.
		 */
		Body: createClass({
			displayName: 'ToolTip.Body',
		}),
	},
 
	getDefaultProps() {
		return {
			alignment: ContextMenu.CENTER,
			direction: ContextMenu.UP,
			flyOutStyle: {},
			isCloseable: false,
			isExpanded: false,
			kind: 'default',
			onClose: _.noop,
			onMouseOut: _.noop,
			onMouseOver: _.noop,
			portalId: null,
		};
	},
 
	getInitialState() {
		return {
			isMouseOverFlyout: false,
			isMouseOverTarget: false,
		};
	},
 
	handleMouseOut(event) {
		setTimeout(() => {
			const {
				props,
				state: {
					isMouseOverFlyout,
					isMouseOverTarget,
				},
				props: {
					onMouseOut,
				},
			} = this;
			if (!isMouseOverFlyout && !isMouseOverTarget) {
				onMouseOut({ props, event });
			}
		}, 100);
	},
 
	handleMouseOverFlyout() {
		this.setState({ isMouseOverFlyout: true });
	},
 
	handleMouseOutFlyout() {
		this.setState({ isMouseOverFlyout: false });
		this.handleMouseOut();
	},
 
	handleMouseOverTarget(event) {
		this.setState({ isMouseOverTarget: true });
		this.props.onMouseOver({ props: this.props, event });
	},
 
	handleMouseOutTarget() {
		this.setState({ isMouseOverTarget: false });
		this.handleMouseOut();
	},
 
	handleClose(event) {
		this.props.onClose({ event, props: this.props });
	},
 
	render() {
		const {
			className,
			alignment,
			direction,
			flyOutMaxWidth,
			flyOutStyle,
			isCloseable,
			isExpanded,
			kind,
			portalId,
			style,
			...passThroughs,
		} = this.props;
 
		const targetProps = _.first(_.map(findTypes(this.props, ToolTip.Target), 'props'));
		const title = _.chain(findTypes(this.props, ToolTip.Title)).map('props').first().get('children').value();
		const body = _.chain(findTypes(this.props, ToolTip.Body)).map('props').first().get('children').value();
		const getAlignmentOffset = n => alignment === ContextMenu.CENTER
			? 0
			: alignment === ContextMenu.START
				? n / 2 - 22.5
				: -(n / 2 - 22.5);
 
		return (
			<ContextMenu
				className={cx('&', className)}
				alignment={ContextMenu.CENTER}
				direction={direction}
				directonOffset={15}
				getAlignmentOffset={getAlignmentOffset}
				isExpanded={isExpanded}
				style={style}
				portalId={portalId}
				{...omitProps(passThroughs, ToolTip)}
				onMouseOver={this.handleMouseOverTarget}
				onMouseOut={this.handleMouseOutTarget}
			>
				<Target {...targetProps} className={cx(_.get(targetProps, 'className'), '&-Target')}>
					{_.get(targetProps, 'children')}
				</Target>
				<FlyOut
					style={{
						...flyOutStyle,
						maxWidth: flyOutMaxWidth || flyOutStyle.maxWidth || 200,
					}}
					className={flyOutCx(className, '&', `&-${direction}`, `&-${alignment}`, `&-${kind}`)}
					onMouseOver={this.handleMouseOverFlyout}
					onMouseOut={this.handleMouseOutFlyout}
				>
					{isCloseable ? <CrossIcon onClick={this.handleClose} className={flyOutCx('&-close')}/> : null}
					{!_.isNil(title) ?
						<h2 className={flyOutCx('&-Title')}>{title}</h2>
					: null}
					{body}
				</FlyOut>
			</ContextMenu>
		);
	},
});
 
export default ToolTip;