All files / components/Collapsible Collapsible.jsx

80% Statements 20/25
57.14% Branches 16/28
75% Functions 3/4
80% Lines 20/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                134x   134x             134x                                                                                         304x                   35x           32x 37x         35x 35x 35x       25x 1x     1x         1x 1x                           10x                       36x   36x   36x                             32x                                                                        
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { Motion, spring } from 'react-motion';
import { QUICK_SLIDE_MOTION } from '../../constants/motion-spring';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, omitProps } from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-Collapsible');
 
const { any, bool, node, number, string } = PropTypes;
 
/**
 * {"categories": ["utility"]}
 *
 * This is a simple container that can render content as expanded or collapsed.
 */
const Collapsible = createClass({
	displayName: 'Collapsible',
	_isPrivate: true,
 
	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,
 
		/**
		 * Show an animated transition for alternating values of `isExpanded`.
		 */
		isAnimated: bool,
 
		/**
		 * If true, do not render children when fully collapsed.
		 */
		isMountControlled: bool,
 
		/**
		 * If `isMountControlled` is true, this value sets is the minimum height
		 * the container needs to reach to not render any children.
		 */
		mountControlThreshold: number,
 
		/**
		 * Pass in a custom root element type.
		 */
		rootType: any,
	},
 
	getDefaultProps() {
		return {
			isExpanded: true,
			isAnimated: true,
			isMountControlled: true,
			mountControlThreshold: 4,
			rootType: 'div',
		};
	},
 
	getInitialState() {
		return {
			maxHeight: null,
		};
	},
 
	storeRef(name) {
		return ref => {
			this.Refs[name] = ref;
		};
	},
 
	componentWillMount() {
		this.Refs = {};
		this.isAnimated = false;
		this.delayTimer = null;
	},
 
	componentDidMount() {
		_.delay(() => {
			this.setState({
				maxHeight: _.get(this.Refs, 'root.scrollHeight'),
			});
			this.isAnimated = this.props.isAnimated;
		}, 32);
	},
 
	componentDidUpdate() {
		this.isAnimated = false;
		this.delayTimer = _.delay(() => {
			if (this.props.isExpanded) {
				const maxHeight = _.get(this.Refs, 'root.scrollHeight');
				if (maxHeight !== this.state.maxHeight) {
					this.setState({
						maxHeight,
					});
				}
			}
			this.isAnimated = this.props.isAnimated;
		}, 32);
	},
 
	componentWillUnmount() {
		this.delayTimer && clearTimeout(this.delayTimer);
	},
 
	render() {
		const {
			children,
			className,
			isExpanded,
			isMountControlled,
			mountControlThreshold,
			rootType,
			...passThroughs
		} = this.props;
 
		const { maxHeight } = this.state;
 
		return (
			<Motion
				style={
					this.isAnimated
						? {
								height: isExpanded && !_.isNull(maxHeight)
									? spring(maxHeight, QUICK_SLIDE_MOTION)
									: spring(0, QUICK_SLIDE_MOTION),
							}
						: {
								height: isExpanded && !_.isNull(maxHeight) ? maxHeight : 0,
							}
				}
			>
				{tween =>
					React.createElement(
						rootType,
						{
							...omitProps(passThroughs, Collapsible),
							ref: this.storeRef('root'),
							className: cx('&', className),
							style: {
								height: tween.height !== maxHeight
									? tween.height < 0 ? 0 : tween.height
									: null,
								overflow: 'hidden',
								padding: 0,
								...passThroughs.style,
							},
						},
						[
							<div
								key="content"
								className={cx('&-content')}
								style={{ margin: 0 }}
							>
								{isMountControlled && !isExpanded
									? _.isNull(maxHeight) ||
											Math.abs(tween.height) > mountControlThreshold
											? children
											: null
									: children}
							</div>,
						]
					)}
			</Motion>
		);
	},
});
 
export default Collapsible;