All files / Expander Expander.tsx

92.86% Statements 26/28
100% Branches 6/6
50% Functions 2/4
96.3% Lines 26/27

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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                              176x   176x                   176x 176x 176x         176x 176x             176x   176x 176x         176x 176x                                                                                         176x                     176x 176x                                                                             176x   176x   176x 176x   176x                 27x 10x                           27x   27x       27x       27x                                                                                                    
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import {
	findTypes,
	omitProps,
	StandardProps,
} from '../../util/component-types';
import { buildModernHybridComponent } from '../../util/state-management';
import ChevronIcon from '../Icon/ChevronIcon/ChevronIcon';
import Collapsible from '../Collapsible/Collapsible';
import * as reducers from './Expander.reducers';
import Button from '../Button/Button';
 
const cx = lucidClassNames.bind('&-Expander');
 
const { any, bool, func, node, object, oneOf, string } = PropTypes;
 
interface IExpanderLabelProps extends StandardProps {
	description?: string;
}
 
interface IExpanderAdditionalLabelProps extends StandardProps {
	description?: string;
}
 
const Label = (_props: IExpanderLabelProps): null => null;
Label.displayName = 'Expander.Label';
Label.peek = {
	description: `
						Renders a \`<span>\` to be shown next to the expander icon.
					`,
};
Label.propName = 'Label';
Label.propTypes = {
	children: node`
					Used to identify the purpose of this switch to the user -- can be any
					renderable content.
				`,
};
 
const AdditionalLabelContent = (_props: IExpanderAdditionalLabelProps): null =>
	null;
AdditionalLabelContent.displayName = 'Expander.AdditionalLabelContent';
AdditionalLabelContent.peek = {
	description: `
						Renders a \`<span>\` to be shown next to the expander label.
					`,
};
AdditionalLabelContent.propName = 'AdditionalLabelContent';
AdditionalLabelContent.propTypes = {
	children: node`
					Used to display additional information or/and actions next to expander label.
				`,
};
 
export interface IExpanderProps
	extends StandardProps,
		React.DetailedHTMLProps<
			React.HTMLAttributes<HTMLDivElement>,
			HTMLDivElement
		> {
	/**
	 * Indicates that the component is in the "expanded" state when true and in
	 * the "unexpanded" state when false.
	 * */
	isExpanded: boolean;
 
	/**
	 * Called when the user clicks on the component's header.
	 * */
	onToggle: (
		isExpanded: boolean,
		{ event, props }: { event: React.MouseEvent; props: IExpanderProps }
	) => void;
 
	/** Passed through to the root element. */
	style?: React.CSSProperties;
 
	/** Child element whose children represents content to be shown next to the
	 * expander icon.
	 * */
	Label?: React.ReactNode;
 
	/** Child element whose children respresent content to be shown inside
	 * Expander.Label and to the right of it
	 * */
	AdditionalLabelContent?: React.ReactNode;
 
	/** Renders different variants of Expander. 'simple' is default.
	 * 'highlighted' is more prominant.
	 * */
	kind: 'simple' | 'highlighted';
}
 
const defaultProps = {
	isExpanded: false,
	onToggle: _.noop,
	kind: 'simple' as const,
};
 
export interface IExpanderState {
	isExpanded: boolean;
}
 
class Expander extends React.Component<IExpanderProps, IExpanderState> {
	static displayName = 'Expander';
	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.
		`,
 
		onToggle: func`
			Called when the user clicks on the component's header.  Signature:
			\`(isExpanded, { event, props }) => {}\`
		`,
 
		style: object`
			Passed through to the root element.
		`,
 
		Label: any`
			Child element whose children represents content to be shown next to the
			expander icon.
		`,
 
		AdditionalLabelContent: node`
			Child element whose children respresent content to be shown inside
			Expander.Label and to the right of it
		`,
 
		kind: oneOf(['simple', 'highlighted'])`
			Renders different variants of Expander. 'simple' is default.
			'highlighted' is more prominant.
		`,
	};
 
	static defaultProps = defaultProps;
 
	static reducers = reducers;
 
	static Label = Label;
	static AdditionalLabelContent = AdditionalLabelContent;
 
	static peek = {
		description: `
				This is a container that provides a toggle that controls when the
				content is shown.
			`,
		categories: ['layout'],
		madeFrom: ['ChevronIcon'],
	};
 
	handleToggle = (event: React.MouseEvent): void => {
		this.props.onToggle(!this.props.isExpanded, {
			event,
			props: this.props,
		});
	};
 
	render(): React.ReactNode {
		const {
			children,
			className,
			isExpanded,
			style,
			kind,
			...passThroughs
		} = this.props;
 
		const labelChildProp = _.first(
			_.map(findTypes(this.props, Expander.Label), 'props')
		);
 
		const additionalLabelContentChildProp = _.first(
			_.map(findTypes(this.props, Expander.AdditionalLabelContent), 'props')
		);
 
		return (
			<div
				{...omitProps(passThroughs, undefined, _.keys(Expander.propTypes))}
				className={cx(
					'&',
					{
						'&-is-expanded': isExpanded,
						'&-kind-highlighted': kind === 'highlighted',
					},
					className
				)}
				style={style}
			>
				<header className={cx('&-header')}>
					<div className={cx('&-header-toggle')} onClick={this.handleToggle}>
						<Button
							className={cx('&-icon')}
							kind='invisible'
							hasOnlyIcon={true}
						>
							<ChevronIcon direction={isExpanded ? 'up' : 'down'} />
						</Button>
						{labelChildProp && (
							<span className={cx('&-text')}>{labelChildProp.children}</span>
						)}
					</div>
					{additionalLabelContentChildProp && (
						<div className={cx('&-additional-content')}>
							{additionalLabelContentChildProp.children}
						</div>
					)}
				</header>
				<Collapsible
					isExpanded={isExpanded}
					rootType='section'
					className={cx('&-content')}
				>
					{children}
				</Collapsible>
			</div>
		);
	}
}
 
export default buildModernHybridComponent<
	IExpanderProps,
	IExpanderState,
	typeof Expander
>(Expander, { reducers });
export { Expander as ExpanderDumb };