All files / components/Breadcrumb Breadcrumb.jsx

100% Statements 9/9
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9
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              134x   134x                   134x                                                         6x   6x 6x 6x   6x               5x                                            
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, findTypes, omitProps } from '../../util/component-types';
import SeparatorIcon from '../Icon/SeparatorIcon/SeparatorIcon';
 
const cx = lucidClassNames.bind('&-Breadcrumb');
 
const { any, node } = PropTypes;
 
/**
 *
 * {"categories": ["navigation"], "madeFrom": ["SeparatorIcon"]}
 *
 * Navigation component to show a user's place in a navigation hierarchy and
 * provide links to return to higher points in the hierarchy
 *
 */
const Breadcrumb = createClass({
	displayName: 'Breadcrumb',
 
	components: {
		SeparatorIcon,
		/*
		 * Renders a `li`
		 */
		Item: createClass({
			displayName: 'Breadcrumb.Item',
			propTypes: {
				children: node,
			},
		}),
	},
 
	propTypes: {
		/**
		 * All children should be `Breadcrumb.Item`s. Others are ignored.
		 */
		children: node,
		/**
		 * Appended to the component-specific class names set on the root
		 * element. Value is run through the `classnames` library.
		 */
		className: any,
	},
 
	render() {
		const { className, ...passThroughs } = this.props;
 
		const items = findTypes(this.props, Breadcrumb.Item);
		const initialItems = _.initial(items);
		const lastItem = _.last(items);
 
		return (
			<nav
				{...omitProps(passThroughs, Breadcrumb)}
				className={cx('&', className)}
			>
				{!_.isEmpty(items)
					? <ul className={cx('&-List')}>
							{_.map(initialItems, ({ props, key }) => (
								<li
									{...props}
									key={key}
									className={cx('&-Item', props.className)}
								>
									{props.children}
									<SeparatorIcon className={cx('&-SeparatorIcon')} />
								</li>
							))}
							<li
								{...lastItem.props}
								key={lastItem.key}
								className={cx('&-Item', lastItem.props.className)}
							/>
						</ul>
					: null}
			</nav>
		);
	},
});
 
export default Breadcrumb;