All files / components/Submarine Submarine.jsx

100% Statements 20/20
100% Branches 8/8
100% Functions 0/0
100% Lines 20/20
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                  120x                     120x             120x                                                                                                                                                                                                     120x                             1x   1x           1x   1x           1x   1x                         28x   28x 28x 28x             28x 1x 1x   27x 27x     28x                                                                        
import _ from 'lodash';
import React from 'react';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, filterTypes, findTypes, omitProps } from '../../util/component-types';
import * as reducers from './Submarine.reducers';
import SplitHorizontal from '../SplitHorizontal/SplitHorizontal';
import ChevronIcon  from '../Icon/ChevronIcon/ChevronIcon';
import GripperHorizontalIcon  from '../Icon/GripperHorizontalIcon/GripperHorizontalIcon';
 
const cx = lucidClassNames.bind('&-Submarine');
 
const {
	any,
	bool,
	func,
	node,
	number,
	string,
	oneOf,
	oneOfType,
} = React.PropTypes;
 
/**
 * {"categories": ["layout"], "madeFrom": ["SplitHorizontal", "ChevronIcon", "GripperHorizontalIcon"]}
 *
 * `Submarine` renders a collapsible, resizeable side bar panel next to primary content.
 */
const Submarine = createClass({
	displayName: 'Submarine',
 
	reducers,
 
	propTypes: {
		/**
		 * Appended to the component-specific class names set on the root
		 * element. Value is run through the `classnames` library.
		 */
		className: string,
		/**
		 * Direct children must be types {Submarine.Primary, Submarine.Bar, Submarine.Title}.
		 * All content is composed as children of these respective elements.
		 */
		children: node,
		/**
		 * Sets the starting height of the Bar.
		 */
		height: oneOfType([number, string]),
		/**
		 * Force the Submarine to be expanded or collapsed.
		 */
		isExpanded: bool,
		/**
		 * Allows animated expand and collapse behavior.
		 */
		isAnimated: bool,
		/**
		 * Render the Submarine to the top or bottom of primary content.
		 */
		position: oneOf(['top', 'bottom']),
		/**
		 * Disable user resizing of the Submarine.
		 */
		isResizeDisabled: bool,
		/**
		 * Set the title of the Submarine.
		 */
		Title: any,
		/**
		 * Called when the user is currently resizing the Submarine.
		 *
		 * Signature: `(height, { event, props }) => {}`
		 */
		onResizing: func,
		/**
		 * Called when the user resizes the Submarine.
		 *
		 * Signature: `(height, { event, props }) => {}`
		 */
		onResize: func,
		/**
		 * Called when the user expands or collapses the Submarine.
		 *
		 * Signature: `({ event, props }) => {}`
		 */
		onToggle: func,
	},
 
	components: {
		Bar: createClass({
			displayName: 'Submarine.Bar',
			propTypes: {
				/**
				 * Submarine content. Also can define <Submarine.Title> here as well.
				 */
				children: node,
				/**
				 * Set the title of the Submarine. (alias for `Submarine.Title`)
				 */
				Title: any,
			},
		}),
 
		Primary: createClass({
			displayName: 'SplitHorizontal.Primary',
			propTypes: {
				/**
				 * Primary content rendered beside the Submarine.
				 */
				children: node,
			},
		}),
 
		Title: createClass({
			displayName: 'Submarine.Title',
			propName: ['Title'],
			propTypes: {
				/**
				 * Submarine title.
				 */
				children: node,
			},
		}),
 
	},
 
	getDefaultProps() {
		return {
			isExpanded: true,
			isAnimated: true,
			height: 250,
			position: 'bottom',
			isResizeDisabled: false,
			onResizing: _.noop,
			onResize: _.noop,
			onToggle: _.noop,
		};
	},
 
	handleExpanderClick(event) {
		const {
			onToggle,
		} = this.props;
 
		onToggle({ props: this.props, event });
	},
 
	handleResizing(height, { event }) {
		const {
			onResizing,
		} = this.props;
 
		onResizing(height, { props: this.props, event });
	},
 
	handleResize(height, { event }) {
		const {
			onResize,
		} = this.props;
 
		onResize(height, { props: this.props, event });
	},
 
	render() {
		const {
			children,
			className,
			isExpanded,
			isAnimated,
			position,
			isResizeDisabled,
			height,
			...passThroughs,
		} = this.props;
 
		const primaryProps = _.get(_.first(filterTypes(children, Submarine.Primary)), 'props', {}); // props from first Primary
		const barProps = _.get(_.first(filterTypes(children, Submarine.Bar)), 'props', {}); // props from first Bar
		const titleProps = _.get(
			findTypes(barProps, Submarine.Title).concat(findTypes(this.props, Submarine.Title)), // get titles from Bar and parent Submarine
			'[0].props', // select props from the first title element
			(<Submarine.Title />).props // default props
		);
 
		let PrimaryPane, BarPane; // using Left/Right Pane as primary depends on position
		if (position !== 'bottom') {
			PrimaryPane = SplitHorizontal.BottomPane;
			BarPane = SplitHorizontal.TopPane;
		} else {
			PrimaryPane = SplitHorizontal.TopPane;
			BarPane = SplitHorizontal.BottomPane;
		}
 
		return (
			<SplitHorizontal
				{...omitProps(passThroughs, Submarine)}
				className={cx('&', {
					'&-is-resize-disabled': isResizeDisabled,
					'&-is-position-bottom': position === 'bottom',
					'&-is-position-top': position !== 'bottom',
				}, className)}
				isAnimated={isAnimated}
				isExpanded={isExpanded}
				collapseShift={33} // leave 33px of sidebar to stick out when collapsed
				onResizing={this.handleResizing}
				onResize={this.handleResize}
			>
				<BarPane {...omitProps(barProps, Submarine.Bar)} className={cx('&-Bar', barProps.className)} height={height}>
					<div className={cx('&-Bar-overlay')} />
					<div className={cx('&-Bar-header')}>
						<div {...titleProps} className={cx('&-Bar-Title', titleProps.className)} />
						<div className={cx('&-expander')} onMouseDown={this.handleExpanderClick}>
							<ChevronIcon direction={isExpanded && position === 'bottom' || !isExpanded && position !== 'bottom' ? 'down' : 'up'} />
						</div>
					</div>
					<div className={cx('&-Bar-content')}>
						{barProps.children}
					</div>
				</BarPane>
				<SplitHorizontal.Divider className={cx('&-Divider')}>
					<GripperHorizontalIcon className={cx('&-Divider-gripper')} />
				</SplitHorizontal.Divider>
				<PrimaryPane {...primaryProps} className={cx('&-Primary', primaryProps.className)} isPrimary />
			</SplitHorizontal>
		);
	},
});
 
export default Submarine;