All files / components/Banner Banner.jsx

85.71% Statements 12/14
90% Branches 9/10
100% Functions 0/0
85.71% Lines 12/14
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                    120x                 120x   120x                                         120x                                                                                                                 120x                                                             50x   50x   50x 3x 47x 17x     50x                                                                                    
import _ from 'lodash';
import React from 'react';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, omitProps } from '../../util/component-types';
import DangerIcon from '../Icon/DangerIcon/DangerIcon';
import InfoIcon from '../Icon/InfoIcon/InfoIcon';
import SuccessIcon from '../Icon/SuccessIcon/SuccessIcon';
import WarningIcon from '../Icon/WarningIcon/WarningIcon';
 
const cx = lucidClassNames.bind('&-Banner');
 
const {
	bool,
	element,
	func,
	node,
	oneOf,
	string,
} = React.PropTypes;
 
const defaultIcons = {
	'success': <SuccessIcon />,
	'danger': <DangerIcon />,
	'info': <InfoIcon isBadge />,
	'warning': <WarningIcon />,
	'primary': null,
	'default': null,
};
 
/**
 *
 * {"categories": ["communication"], "madeFrom": ["DangerIcon", "InfoIcon", "SuccessIcon", "WarningIcon"]}
 *
 * A basic Banner. Any props that are not explicitly called out below will be
 * passed through to the native `Banner` component.
 *
 * Short single line content can be passed in as a simple string. Multi line
 * messages should be passed wrapped in a `<p>` tag.
 *
 * It is valid to use `strong` or `em` within a `Banner` message.
 */
const Banner = createClass({
	displayName: 'Banner',
	propTypes: {
		/**
		 * Pass in a bool to display predefined icon based on `kind`.
		 */
		hasIcon: bool,
		/**
		 * Pass in a icon component for custom icons within `Banner`.
		 */
		icon: element,
		/**
		 * Set this to `true` if you want to have a `x` close icon.
		 */
		isCloseable: bool,
		/**
		 * Set this value to `false` if you want to remove the rounded corners on
		 * the `Banner`.  **default is `true`**
		 */
		hasRoundedCorners: bool,
		/**
		 * Class names that are appended to the defaults.
		 */
		className: string,
		/**
		 * Any valid React children.
		 */
		children: node,
		/**
		 * Style variations of the `Banner`.
		 */
		kind: oneOf([
			'primary',
			'success',
			'warning',
			'danger',
			'info',
			'default',
		]),
		/**
		 * If set to `true` the banner have smaller padding on the inside.
		 */
		isSmall: bool,
		/**
		 * Called when the user closes the `Banner`.
		 *
		 * Signature: `({ event, props }) => {}`
		 */
		onClose: func,
		/**
		 * Controls the visibility of the `Banner`.
		 */
		isClosed: bool,
 
	},
 
	getDefaultProps() {
		return {
			hasIcon: false,
			icon: null,
			isCloseable: true,
			hasRoundedCorners: true,
			kind: 'default',
			isSmall: false,
			onClose: _.noop,
		};
	},
 
	handleClose(event) {
		const {
			onClose,
		} = this.props;
 
		onClose({ event, props: this.props });
	},
 
	render() {
		const {
			hasIcon,
			icon,
			kind,
			isSmall,
			className,
			children,
			isCloseable,
			hasRoundedCorners,
			isClosed,
			...passThroughs,
		} = this.props;
 
		let displayedIcon = null;
 
		if (icon) {
			displayedIcon = icon;
		} else if (hasIcon) {
			displayedIcon = defaultIcons[kind];
		}
 
		return (
			<ReactCSSTransitionGroup
				transitionName={cx('&')}
				transitionEnterTimeout={300}
				transitionLeaveTimeout={300}
			>
				{!isClosed ?
					<section
						{...omitProps(passThroughs, Banner)}
						className={cx('&', {
							'&-has-icon': displayedIcon,
							'&-has-close': isCloseable,
							'&-has-no-roundedCorners': !hasRoundedCorners,
							'&-primary': kind === 'primary',
							'&-success': kind === 'success',
							'&-warning': kind === 'warning',
							'&-danger': kind === 'danger',
							'&-info': kind === 'info',
							'&-small': isSmall,
						}, className)}
					>
						{displayedIcon ?
							<span className={cx('&-icon')}>{displayedIcon}</span>
						: null}
 
						<span className={cx('&-content')}>
							{children}
						</span>
 
						{isCloseable ?
							<span className={cx('&-close')} onClick={this.handleClose}>
								{String.fromCharCode(0x00d7)}
							</span>
						: null}
					</section>
				: null}
			</ReactCSSTransitionGroup>
		);
	},
});
 
export default Banner;