All files / Panel Panel.tsx

96.15% Statements 25/26
100% Branches 4/4
66.67% Functions 2/3
100% Lines 24/24

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                    187x   187x               187x 187x 187x         187x       187x             187x   187x 187x         187x       187x                                                 187x           187x                 41x   41x     41x       41x                                                                 187x 187x 187x           187x                                                       187x 187x      
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';
 
const cx = lucidClassNames.bind('&-Panel');
 
const { bool, node, object, string } = PropTypes;
 
interface IPanelHeaderProps
	extends StandardProps,
		React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> {
	description?: string;
}
 
const PanelHeader = (_props: IPanelHeaderProps): null => null;
PanelHeader.displayName = 'Panel.Header';
PanelHeader.peek = {
	description: `
		Content displayed at the top of the panel.
	`,
};
PanelHeader.propTypes = {
	description: string,
	children: node,
};
PanelHeader.propName = 'Header';
 
interface IPanelFooterProps
	extends StandardProps,
		React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> {
	description?: string;
}
const PanelFooter = (_props: IPanelFooterProps): null => null;
 
PanelFooter.displayName = 'Panel.Footer';
PanelFooter.peek = {
	description: `
		Content displayed at the bottom of the panel.
	`,
};
PanelFooter.propTypes = {
	description: string,
	children: node,
};
PanelFooter.propName = 'Footer';
 
export interface IPanelProps
	extends StandardProps,
		React.DetailedHTMLProps<
			React.HTMLAttributes<HTMLDivElement>,
			HTMLDivElement
		> {
	/** *Child Element* - Header contents. Only one \`Header\` is used. */
	Header?: React.ReactNode & { props: IPanelHeaderProps };
 
	/** *Child Element* - Footer contents. Only one \`Footer\` is used. */
	Footer?: React.ReactNode & { props: IPanelFooterProps };
 
	/** If set to true, creates a content section with no padding. */
	isGutterless: boolean;
 
	/** If set to false, removes margin around the Panel */
	hasMargin: boolean;
 
	/** If set to true, makes content overflow scrollable, when Panel has a set
	 * height. */
	isScrollable: boolean;
}
 
const defaultProps = {
	isGutterless: false,
	hasMargin: true,
	isScrollable: true,
};
 
export const Panel = (props: IPanelProps): React.ReactElement => {
	const {
		children,
		className,
		isGutterless,
		hasMargin,
		style,
		isScrollable,
		...passThroughs
	} = props;
 
	const headerChildProp = _.first(
		_.map(findTypes(props, Panel.Header), 'props')
	);
	const footerChildProp = _.first(
		_.map(findTypes(props, Panel.Footer), 'props')
	);
 
	return (
		<div
			{...omitProps<IPanelProps>(
				passThroughs,
				undefined,
				_.keys(Panel.propTypes)
			)}
			className={cx('&', className, {
				'&-is-not-gutterless': !isGutterless,
				'&-has-margin': hasMargin,
				'&-is-scrollable': isScrollable,
			})}
			style={style}
		>
			{headerChildProp ? (
				<header
					{...headerChildProp}
					className={cx('&-Header', headerChildProp.className)}
				/>
			) : null}
 
			<section className={cx('&-content')}>{children}</section>
 
			{footerChildProp ? (
				<footer
					{...footerChildProp}
					className={cx('&-Footer', footerChildProp.className)}
				/>
			) : null}
		</div>
	);
};
 
Panel.defaultProps = defaultProps;
Panel.displayName = 'Panel';
Panel.peek = {
	description: `
		Panel is used to wrap content to better organize elements in window.
	`,
	categories: ['layout'],
};
Panel.propTypes = {
	className: string`
		Appended to the component-specific class names set on the root element.
	`,
	Header: node`
		*Child Element* - Header contents. Only one \`Header\` is used.
	`,
	Footer: node`
		*Child Element* - Footer contents. Only one \`Footer\` is used.
	`,
	children: node`
		Generally you should only have a single child element so the centering
		works correctly.
	`,
	isGutterless: bool`
		If set to true, creates a content section with no padding.
	`,
	hasMargin: bool`
		If set to false, removes margin around the Panel
	`,
	style: object`
		Styles that are passed through to root element.
	`,
	isScrollable: bool`
		If set to true, makes content overflow scrollable, when Panel has a set
		height.
	`,
};
Panel.Header = PanelHeader;
Panel.Footer = PanelFooter;
 
export default Panel;