All files / ExpanderPanel ExpanderPanel.tsx

95% Statements 19/20
100% Branches 8/8
66.67% Functions 2/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                            176x   176x           176x 176x 176x           176x 176x                                                                                     176x 176x 176x                                                                                 176x                 176x             30x 11x 9x                                   30x   30x           30x   30x                                                                                              
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { getFirst, omitProps, StandardProps } from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
import { IExpanderState } from '../Expander/Expander';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import Collapsible from '../Collapsible/Collapsible';
import Button from '../Button/Button';
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;
 
export interface IExpanderPanelHeaderProps extends StandardProps {
	description?: string;
}
 
const Header = (_props: IExpanderPanelHeaderProps): null => null;
Header.displayName = 'ExpanderPanel.Header';
Header.peek = {
	description: `
		Renders a \`<span>\` of content next to the \`ChevronIcon\` in the
		\`Panel.Header\`
	`,
};
Header.propName = 'Header';
Header.propTypes = {
	children: node`
		Used to identify the purpose of this switch to the user -- can be any
		renderable content.
	`,
};
 
export interface IExpanderPanelProps extends StandardProps {
	/** Indicates that the component is in the "expanded" state when true and in
			the "unexpanded" state when false. */
	isExpanded: boolean;
 
	/** Indicates that the component is in the "disabled" state when true and in
			the "enabled" state when false. */
	isDisabled: boolean;
 
	/** Controls the presence of padding on the inner content. */
	hasPadding: boolean;
 
	onRest?: () => void;
	onRestAppliedOnCollapse?: boolean;
 
	/** Called when the user clicks on the component's header. */
	onToggle: (
		isExpanded: boolean,
		{
			event,
			props,
		}: {
			event: React.MouseEvent;
			props: IExpanderPanelProps;
		}
	) => void;
 
	/** prop alternative to Header child component passed through to the
			underlying ExpanderPanel */
	Header?: React.ReactNode;
}
 
class ExpanderPanel extends React.Component<
	IExpanderPanelProps,
	IExpanderState
> {
	static displayName = 'ExpanderPanel';
	static Header = Header;
	static propTypes = {
		children: node`
			Expandable content.
		`,
 
		className: string`
			Appended to the component-specific class names set on the root element.
		`,
 
		isExpanded: bool`
			Indicates that the component is in the "expanded" state when true and in
			the "unexpanded" state when false.
		`,
 
		isDisabled: bool`
			Indicates that the component is in the "disabled" state when true and in
			the "enabled" state when false.
		`,
 
		hasPadding: bool`
			Controls the presence of padding on the inner content.
		`,
 
		onToggle: func`
			Called when the user clicks on the component's header.
			Signature: \`(isExpanded, { event, props }) => {}\`
		`,
 
		style: object`
			Passed through to the root element.
		`,
 
		onRest: func`Optional. The callback that fires when the animation comes to a rest.`,
		onRestAppliedOnCollapse: bool`Applies on onRest callback when rest state is closed.`,
 
		Header: any`
			prop alternative to Header child component passed through to the
			underlying ExpanderPanel
		`,
	};
 
	static peek = {
		description: `
					This is a container that provides a toggle that controls when the
					content is shown.
				`,
		categories: ['layout'],
		madeFrom: ['ChevronIcon', 'Expander', 'Panel'],
	};
 
	static defaultProps = {
		isExpanded: false,
		onToggle: _.noop,
		hasPadding: true,
		isDisabled: false,
	};
 
	handleToggle = (event: React.MouseEvent) => {
		if (!this.props.isDisabled) {
			this.props.onToggle(!this.props.isExpanded, {
				event,
				props: this.props,
			});
		}
	};
 
	render() {
		const {
			children,
			className,
			isExpanded,
			isDisabled,
			hasPadding,
			onRest,
			onRestAppliedOnCollapse,
			style,
			...passThroughs
		} = this.props;
 
		const headerChildProps = _.get(
			getFirst(this.props, ExpanderPanel.Header),
			'props'
		);
 
		const cleanedOnRest =
			onRestAppliedOnCollapse || isExpanded ? onRest : undefined;
 
		return (
			<Panel
				{...omitProps(
					passThroughs,
					undefined,
					_.keys(ExpanderPanel.propTypes),
					false
				)}
				className={cx(
					'&',
					{
						'&-is-collapsed': !isExpanded,
						'&-is-disabled': isDisabled,
					},
					className
				)}
				style={style}
				isGutterless={!hasPadding}
			>
				<Panel.Header className={cx('&-header')} onClick={this.handleToggle}>
					<Button className={cx('&-icon')} kind='invisible' hasOnlyIcon={true}>
						<ChevronIcon direction={isExpanded ? 'up' : 'down'} />
					</Button>
 
					<span {...headerChildProps} />
				</Panel.Header>
 
				<Collapsible
					isExpanded={isExpanded}
					className={cx('&-content', {
						'&-content-is-expanded': isExpanded,
					})}
					onRest={cleanedOnRest}
				>
					<div className={cx('&-content-inner')}>{children}</div>
				</Collapsible>
			</Panel>
		);
	}
}
 
export default buildModernHybridComponent<
	IExpanderPanelProps,
	IExpanderState,
	typeof ExpanderPanel
>(ExpanderPanel, { reducers });
export { ExpanderPanel as ExpanderPanelDumb };