All files / Selection Selection.tsx

96.96% Statements 32/33
96.66% Branches 29/30
83.33% Functions 5/6
100% Lines 31/31

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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306                        4x   4x   4x             100x                           4x 4x         4x 4x         20x 4x     4x 4x                                                                                                                                           4x                 4x                         101x   101x   101x 101x 101x 101x             101x                                                                     1x               95x 67x                         28x                 4x 4x 4x 4x       4x 4x                                                                                                                                              
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 CloseIcon from '../Icon/CloseIcon/CloseIcon';
import InfoIcon from '../Icon/InfoIcon/InfoIcon';
import WarningIcon from '../Icon/WarningIcon/WarningIcon';
import { lucidClassNames } from '../../util/style-helpers';
import { getFirst, FC, StandardProps } from '../../util/component-types';
 
const { createElement } = React;
 
const { bool, func, string, node, oneOf } = PropTypes;
 
const cx = lucidClassNames.bind('&-Selection');
 
/** SELECTION ICON */
function defaultIcon(
	kind: SelectionKind,
	responsiveMode?: SelectionResponsiveMode
) {
	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;
}
 
export interface ISelectionIconProps extends StandardProps {}
 
const SelectionIcon: FC<ISelectionIconProps> = (): null => null;
SelectionIcon.peek = {
	description: `
        Icon that is displayed within the Selection. Any of the lucid \`*Icon\` components should work.
    `,
};
SelectionIcon.displayName = 'Selection.Icon';
SelectionIcon.propName = 'Icon';
 
/** SELECTION LABEL */
export interface ISelectionLabelProps extends StandardProps {}
 
const SelectionLabel: FC<ISelectionLabelProps> = (): null => null;
SelectionLabel.peek = {
	description: `\`Label\` for the \`Selection\`.`,
};
SelectionLabel.displayName = 'Selection.Label';
SelectionLabel.propName = 'Label';
 
/** SELECTION */
type SelectionKind =
	| 'default'
	| 'container'
	| 'success'
	| 'danger'
	| 'info'
	| 'warning';
 
type SelectionResponsiveMode = 'small' | 'medium' | 'large';
 
export interface ISelectionProps
	extends StandardProps,
		React.DetailedHTMLProps<
			React.HTMLAttributes<HTMLDivElement>,
			HTMLDivElement
		> {
	/** Applies an icon and styles for the kind of selection. */
	kind: SelectionKind;
 
	/** Apply to the top of a nested sequence of Selection components.
	 * Adds some spacing for a list of top level Selections with nested Selctions inside each.
	 * */
	isTop?: boolean;
 
	/** Only applies to \`container\` Selection components.
	 * Fills with a darker gray background.
	 * Defaults to false.
	 * */
	isFilled?: boolean;
 
	/** Shows or hides the little "x" for a given item. */
	isRemovable: boolean;
 
	/** Called when the close button is clicked. */
	onRemove: ({
		props,
		event,
	}: {
		props: ISelectionProps;
		event: React.MouseEvent;
	}) => void;
 
	/** Gives the selection a background. This is desirable when you only have
	 * one level of nested selections.
	 * */
	hasBackground: boolean;
 
	/** Make the content text bold. This is desirable when you only have one
	 * level of nested selections.
	 * */
	isBold: boolean;
 
	/** Label of the component. */
	Label?: React.ReactNode;
 
	/** 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?: React.ReactNode;
 
	/** 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: SelectionResponsiveMode;
}
 
const defaultProps = {
	isRemovable: true,
	onRemove: _.noop,
	hasBackground: false,
	isBold: false,
	kind: 'default' as const,
	responsiveMode: 'large' as const,
};
 
const Selection = (props: ISelectionProps) => {
	const {
		className,
		isRemovable,
		children,
		hasBackground,
		isBold,
		isFilled,
		isTop,
		kind,
		onRemove,
		responsiveMode,
		...passThroughs
	} = props;
 
	const isSmall = responsiveMode === 'small';
 
	const labelProps = _.get(getFirst(props, Selection.Label), 'props', {});
	const iconElement = getFirst(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
			{..._.omit(passThroughs, ['callbackId', 'Label'])}
			className={cx(
				'&',
				`&-is-${responsiveMode}`,
				kind && `&-${kind}`,
				{
					'&-has-background': hasBackground,
					'&-is-bold': isBold,
					'&-is-filled': isFilled,
					'&-is-top': isTop,
					'&-no-title': _.isEmpty(labelProps),
				},
				className
			)}
		>
			{icon}
 
			<div className={cx('&-content')}>
				<div className={cx('&-label-container')}>
					<span
						{...labelProps}
						className={cx('&-label', isSmall && '&-label-is-small')}
					/>
 
					{isRemovable ? (
						<CloseIcon
							isClickable
							size={!isSmall ? 8 : 16}
							className={cx(
								'&-close-button',
								isSmall && '&-close-button-is-small'
							)}
							onClick={({ event }) => {
								onRemove({ event, props });
							}}
						/>
					) : null}
				</div>
				{!_.isEmpty(children) && (
					<div className={cx('&-children-container')}>
						{_.map(React.Children.toArray(children), (child, i) => {
							if (React.isValidElement(child) && child.type === Selection) {
								return (
									<Selection
										key={
											_.get(
												getFirst(child.props, Selection.Label),
												['props', 'children'],
												{}
											) + i
										}
										{...child.props}
									/>
								);
							}
							return child;
						})}
					</div>
				)}
			</div>
		</div>
	);
};
 
Selection.displayName = 'Selection';
Selection.Icon = SelectionIcon;
Selection.Label = SelectionLabel;
Selection.peek = {
	description: `Used to indicate selections. \`Selection\` is very similar to \`Tag\` but is meant to be used in areas of the UI that have more space available to them.`,
	categories: ['communication'],
};
Selection.defaultProps = defaultProps;
Selection.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']),
 
	/**
			Apply to the top of a nested sequence of Selection components.
			Adds some spacing for a list of top level Selections with nested Selctions inside each.
		*/
	isTop: bool,
 
	/**
			Only applies to \`container\` Selection components.
			Fills with a darker gray background.
			Defaults to false.
		*/
	isFilled: bool,
 
	/**
			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.
		*/
	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']),
};
 
export default Selection;