All files / components/Selection Selection.jsx

100% Statements 19/19
96.15% Branches 25/26
100% Functions 2/2
100% Lines 19/19
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 240 241                                  134x   134x   134x   134x             13x                                                                 134x                                                                                                                                                 268x                     1x                         14x   14x 14x   14x 14x 14x         14x 14x 14x             14x                                                                           1x                                          
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import MinusCircleIcon from '../Icon/MinusCircleIcon/MinusCircleIcon';
import SuccessIcon from '../Icon/SuccessIcon/SuccessIcon';
import CrossIcon from '../Icon/CrossIcon/CrossIcon';
import InfoIcon from '../Icon/InfoIcon/InfoIcon';
import WarningIcon from '../Icon/WarningIcon/WarningIcon';
import { lucidClassNames } from '../../util/style-helpers';
import {
	createClass,
	omitProps,
	getFirst,
	filterTypes,
	rejectTypes,
} from '../../util/component-types';
 
const { createElement } = React;
 
const { bool, func, string, node, oneOf } = PropTypes;
 
const cx = lucidClassNames.bind('&-Selection');
 
const responsiveMap = {
	small: 'small',
	medium: 'small',
	large: 'large',
};
 
function defaultIcon(kind, responsiveMode) {
	return kind === 'default'
		? null
		: kind === 'container'
				? null
				: kind === 'success'
						? <SuccessIcon
								className={cx('&-Icon', `&-Icon-is-${responsiveMode}`)}
							/>
						: kind === 'danger'
								? <MinusCircleIcon
										className={cx('&-Icon', `&-Icon-is-${responsiveMode}`)}
									/>
								: kind === 'info'
										? <InfoIcon
												className={cx('&-Icon', `&-Icon-is-${responsiveMode}`)}
											/>
										: kind === 'warning'
												? <WarningIcon
														className={cx(
															'&-Icon',
															`&-Icon-is-${responsiveMode}`
														)}
													/>
												: null;
}
 
/**
 *
 * {"categories": ["communication"]}
 *
 * Used to indicate selections. It's very similar to `Tag` but is meant to be
 * used in areas of the UI that have more space available to them.
 */
const Selection = createClass({
	displayName: 'Selection',
 
	components: {
		Label: createClass({
			displayName: 'Selection.Label',
			propName: 'Label',
		}),
 
		Icon: createClass({
			displayName: 'Selection.Icon',
			propName: 'Icon',
		}),
	},
 
	propTypes: {
		/**
		 * Appended to the component-specific class names set on the root element.
		 */
		className: string,
		/**
		 * Applies an icon and styles for the kind of selection.
		 */
		kind: oneOf([
			'default',
			'container',
			'success',
			'danger',
			'info',
			'warning',
		]),
		/**
		 * Shows or hides the little "x" for a given item.
		 */
		isRemovable: bool,
		/**
		 * Gives the selection a background. This is desirable when you only have
		 * one level of nested selections.
		 */
		hasBackground: bool,
		/**
		 * Make the content text bold. This is desirable when you only have
		 * one level of nested selections.
		 */
		isBold: bool,
		/**
		 * Called when the close button is clicked.
		 *
		 * Signature: `({ props, event }) => {}`
		 */
		onRemove: func,
		/**
		 * Label of the component.
		 */
		Label: node,
		/**
		 * Display a custom icon for the selection. Generally you shouldn't need
		 * this prop since the `kind` prop will pick the correct icon for you.
		 */
		Icon: node,
		/**
		 * Arbitrary children.
		 */
		children: node,
		/**
		 * Adjusts the display of this component. This should typically be driven
		 * by screen size. Currently `small` and `large` are explicitly handled
		 * by this component.
		 */
		responsiveMode: oneOf(['small', 'medium', 'large']),
	},
 
	getDefaultProps() {
		return {
			kind: 'default',
			isRemovable: true,
			onRemove: _.noop,
			hasBackground: false,
			isBold: false,
			responsiveMode: 'large',
		};
	},
 
	handleRemove(event) {
		this.props.onRemove({ props: this.props, event });
	},
 
	render() {
		const {
			className,
			kind,
			isRemovable,
			children,
			hasBackground,
			isBold,
			responsiveMode: responsiveModeInput,
			...passThroughs
		} = this.props;
 
		const responsiveMode = responsiveMap[responsiveModeInput];
		const isSmall = responsiveMode === 'small';
 
		const selectionChildren = filterTypes(children, Selection);
		const otherChildren = rejectTypes(children, Selection);
		const labelProps = _.get(
			getFirst(this.props, Selection.Label),
			'props',
			{}
		);
		const iconElement = getFirst(this.props, Selection.Icon);
		const iconChildren = _.get(iconElement, 'props.children');
		const icon = iconChildren
			? createElement(iconChildren.type, {
					...iconChildren.props,
					className: cx('&-Icon', iconChildren.props.className),
				})
			: defaultIcon(kind, responsiveMode);
 
		return (
			<div
				{...omitProps(passThroughs, Selection)}
				className={cx(
					'&',
					`&-is-${responsiveMode}`,
					kind && `&-${kind}`,
					{
						'&-has-background': hasBackground,
						'&-is-bold': isBold,
					},
					className
				)}
			>
				{icon}
 
				<div className={cx('&-content')}>
					<div className={cx('&-label-container')}>
						<span
							{...labelProps}
							className={cx('&-label', isSmall && '&-label-is-small')}
						/>
 
						{isRemovable
							? <CrossIcon
									isClickable
									size={isSmall ? 44 : 26}
									viewBox={isSmall ? '-6 -6 28 28' : '-3 -2 20 20'}
									className={cx('&-close-button')}
									onClick={this.handleRemove}
								/>
							: null}
					</div>
 
					{_.isEmpty(selectionChildren)
						? null
						: <div className={cx('&-children-container')}>
								{_.map(selectionChildren, ({ props }, i) => (
									<Selection
										key={
											_.get(
												getFirst(props, Selection.Label),
												['props', 'children'],
												{}
											) + i
										}
										{...props}
									/>
								))}
							</div>}
					{otherChildren}
 
				</div>
			</div>
		);
	},
});
 
export default Selection;