All files / components/ExpanderPanel ExpanderPanel.jsx

100% Statements 9/9
100% Branches 4/4
100% Functions 0/0
100% Lines 9/9
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                          134x   134x               134x                                                                                                                                             268x               11x 9x                               29x   29x         29x                                                                                    
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, getFirst, omitProps } from '../../util/component-types';
import { buildHybridComponent } from '../../util/state-management';
 
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import Collapsible from '../Collapsible/Collapsible';
import Panel from '../Panel/Panel';
 
import * as reducers from '../Expander/Expander.reducers';
 
const cx = lucidClassNames.bind('&-ExpanderPanel');
 
const { any, bool, func, node, object, string } = PropTypes;
 
/**
 * {"categories": ["layout"], "madeFrom": ["ChevronIcon", "Expander", "Panel"]}
 *
 * This is a container that provides a toggle that controls when the content is
 * shown.
 */
const ExpanderPanel = createClass({
	displayName: 'ExpanderPanel',
 
	components: {
		/**
		 * Renders a `<span>` of content next to the `ChevronIcon` in the `Panel.Header`
		 */
		Header: createClass({
			displayName: 'ExpanderPanel.Header',
			propName: 'Header',
			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,
 
		/**
		 * Indicates that the component is in the "disabled" state when true
		 * and in the "enabled" state when false.
		 */
		isDisabled: bool,
 
		/**
		 * Controls the presence of padding on the inner content.
		 */
		hasPadding: 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,
 
		/**
		 * prop alternative to Header child component
		 * passed through to the underlying ExpanderPanel
		 */
		Header: any,
	},
 
	getDefaultProps() {
		return {
			isExpanded: false,
			onToggle: _.noop,
			hasPadding: true,
		};
	},
 
	handleToggle(event) {
		if (!this.props.isDisabled) {
			this.props.onToggle(!this.props.isExpanded, {
				event,
				props: this.props,
			});
		}
	},
 
	render() {
		const {
			children,
			className,
			isExpanded,
			isDisabled,
			hasPadding,
			style,
			...passThroughs
		} = this.props;
 
		const headerChildProps = _.get(
			getFirst(this.props, ExpanderPanel.Header),
			'props'
		);
 
		return (
			<Panel
				{...omitProps(passThroughs, ExpanderPanel, [], false)}
				className={cx(
					'&',
					{
						'&-is-collapsed': !isExpanded,
						'&-is-disabled': isDisabled,
					},
					className
				)}
				style={style}
			>
				<Panel.Header className={cx('&-header')} onClick={this.handleToggle}>
					<span className={cx('&-icon')}>
						<ChevronIcon direction={isExpanded ? 'up' : 'down'} />
					</span>
 
					<span {...headerChildProps} />
				</Panel.Header>
 
				<Collapsible
					isExpanded={isExpanded}
					className={cx('&-content', {
						'&-content-is-expanded': isExpanded,
					})}
				>
					<div
						className={cx('&-content-inner', {
							'&-content-inner-has-padding': hasPadding,
						})}
					>
						{children}
					</div>
				</Collapsible>
			</Panel>
		);
	},
});
 
export default buildHybridComponent(ExpanderPanel);
export { ExpanderPanel as ExpanderPanelDumb };