UNPKG

sophia-components

Version:
148 lines (110 loc) 6.22 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _compose = require('recompose/compose'); var _compose2 = _interopRequireDefault(_compose); var _withIsHas = require('../../base/withIsHas'); var _withIsHas2 = _interopRequireDefault(_withIsHas); var _withAttrs = require('../../base/withAttrs'); var _withAttrs2 = _interopRequireDefault(_withAttrs); var _helpers = require('../../utils/helpers'); var _BreadcrumbItem = require('./components/BreadcrumbItem'); var _BreadcrumbItem2 = _interopRequireDefault(_BreadcrumbItem); var _Icon = require('../../elements/Icon'); var _Icon2 = _interopRequireDefault(_Icon); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint-disable no-nested-ternary */ var Breadcrumb = function (_Component) { _inherits(Breadcrumb, _Component); function Breadcrumb() { _classCallCheck(this, Breadcrumb); var _this = _possibleConstructorReturn(this, (Breadcrumb.__proto__ || Object.getPrototypeOf(Breadcrumb)).call(this)); _this.onClick = _this.onClick.bind(_this); _this.buildItems = _this.buildItems.bind(_this); return _this; } _createClass(Breadcrumb, [{ key: 'onClick', value: function onClick(index, event) { event.preventDefault(); var _props = this.props, items = _props.items, onItemClick = _props.onItemClick; if (onItemClick) { onItemClick(index, items[index], event); } } }, { key: 'buildItems', value: function buildItems() { var _this2 = this; var _props2 = this.props, items = _props2.items, activeIndex = _props2.activeIndex; if (!items || !items.length) { return null; } return items.map(function (item, index) { var active = activeIndex || activeIndex === 0 ? activeIndex === index : index === items.length - 1; var label = typeof item === 'string' ? item : item.label; var icon = typeof item === 'string' ? null : typeof item.icon === 'string' ? _react2.default.createElement(_Icon2.default, { small: true, icon: item.icon }) : item.icon; return _react2.default.createElement( _BreadcrumbItem2.default, { key: label, index: index, onClick: _this2.onClick, active: active }, icon, label ); }); } }, { key: 'render', value: function render() { var _props3 = this.props, _props3$attrs = _props3.attrs, className = _props3$attrs.className, restAttrs = _objectWithoutProperties(_props3$attrs, ['className']), children = _props3.children; var itemsComponents = children || this.buildItems(); return _react2.default.createElement( 'nav', _extends({ className: (0, _helpers.classNameJoiner)('breadcrumb', className), 'aria-label': 'breadcrumbs' }, restAttrs), _react2.default.createElement( 'ul', null, itemsComponents ) ); } }]); return Breadcrumb; }(_react.Component); Breadcrumb.propTypes = { children: _propTypes2.default.node, index: _propTypes2.default.number, attrs: _propTypes2.default.shape().isRequired, onItemClick: _propTypes2.default.func, items: _propTypes2.default.arrayOf(_propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.shape({ icon: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.node]), label: _propTypes2.default.string.isRequired })])), activeIndex: _propTypes2.default.number }; Breadcrumb.defaultProps = { onItemClick: null, index: null, items: null, children: null, activeIndex: null }; var hasKeys = (0, _helpers.combineSets)(_withIsHas.helpersHasKeys, ['arrowSeparator', 'bulletSeparator', 'dotSeparator', 'succeedsSeparator']); exports.default = (0, _compose2.default)((0, _withIsHas2.default)((0, _helpers.combineSets)(_withIsHas.helpersIsKeys, _withIsHas.aligmentKeys, _withIsHas.sizeKeys), hasKeys), (0, _withAttrs2.default)())(Breadcrumb);