All files / components/Expander Expander.jsx

69.23% Statements 9/13
66.67% Branches 4/6
100% Functions 0/0
69.23% Lines 9/13
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                                134x   134x               134x                                                                                                                                 268x                                                 26x                     26x   26x       26x                                                                               10x                  
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import ReactTransitionGroup from 'react-transition-group/CSSTransitionGroup';
import { lucidClassNames } from '../../util/style-helpers';
import {
	createClass,
	getFirst,
	findTypes,
	omitProps,
} from '../../util/component-types';
import { buildHybridComponent } from '../../util/state-management';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import Collapsible from '../Collapsible/Collapsible';
import * as reducers from './Expander.reducers';
 
const cx = lucidClassNames.bind('&-Expander');
 
const { any, bool, func, node, object, oneOf, string } = PropTypes;
 
/**
 * {"categories": ["layout"], "madeFrom": ["ChevronIcon"]}
 *
 * This is a container that provides a toggle that controls when the content is
 * shown.
 */
const Expander = createClass({
	displayName: 'Expander',
 
	components: {
		/**
		 * Renders a `<span>` to be shown next to the expander icon.
		 */
		Label: createClass({
			displayName: 'Expander.Label',
			propName: 'Label',
			propTypes: {
				/**
				 * Used to identify the purpose of this switch to the user -- can be
				 * any renderable content.
				 */
				children: node,
			},
		}),
	},
 
	reducers,
 
	propTypes: {
		/**
		 * Expandable content.
		 */
		children: node,
 
		/**
		 * Appended to the component-specific class names set on the root
		 * element.
		 */
		className: string,
 
		/**
		 * Indicates that the component is in the "expanded" state when true
		 * and in the "unexpanded" state when false.
		 */
		isExpanded: bool,
 
		/**
		 * Called when the user clicks on the component's header.
		 *
		 * Signature: `(isExpanded, { event, props }) => {}`
		 */
		onToggle: func,
 
		/**
		 * Passed through to the root element.
		 */
		style: object,
 
		/**
		 * Child element whose children represents content to be shown next to
		 * the expander icon.
		 */
		Label: any,
 
		/**
		 * Renders different variants of Expander. 'simple' is default. 'highlighted' is more prominant.
		 */
		kind: oneOf(['simple', 'highlighted']),
	},
 
	getDefaultProps() {
		return {
			isExpanded: false,
			onToggle: _.noop,
			kind: 'simple',
		};
	},
 
	componentWillReceiveProps(nextProps) {
		const currentLabel = _.get(
			getFirst(this.props, Expander.Label),
			'props.children',
			null
		);
		const nextLabel = _.get(
			getFirst(nextProps, Expander.Label),
			'props.children',
			null
		);
 
		if (currentLabel !== nextLabel) {
			this._labelKey++;
		}
	},
 
	componentWillMount() {
		this._labelKey = 0;
	},
 
	render() {
		const {
			children,
			className,
			isExpanded,
			style,
			kind,
			...passThroughs
		} = this.props;
 
		const labelChildProp = _.first(
			_.map(findTypes(this.props, Expander.Label), 'props')
		);
 
		return (
			<div
				{...omitProps(passThroughs, Expander)}
				className={cx(
					'&',
					{
						'&-is-expanded': isExpanded,
						'&-kind-highlighted': kind === 'highlighted',
					},
					className
				)}
				style={style}
			>
				<header className={cx('&-header')} onClick={this.handleToggle}>
					<span className={cx('&-icon')}>
						<ChevronIcon direction={isExpanded ? 'up' : 'down'} />
					</span>
					<ReactTransitionGroup
						transitionName={cx('&-text')}
						transitionEnterTimeout={100}
						transitionLeaveTimeout={100}
						className={cx('&-text')}
					>
						{labelChildProp
							? <span key={this._labelKey}>{labelChildProp.children}</span>
							: null}
					</ReactTransitionGroup>
				</header>
				<Collapsible
					isExpanded={isExpanded}
					rootType="section"
					className={cx('&-content')}
				>
					{children}
				</Collapsible>
			</div>
		);
	},
 
	handleToggle(event) {
		this.props.onToggle(!this.props.isExpanded, {
			event,
			props: this.props,
		});
	},
});
 
export default buildHybridComponent(Expander);
export { Expander as ExpanderDumb };