'use strict'; exports.__esModule = true; var _extends3 = require('babel-runtime/helpers/extends'); var _extends4 = _interopRequireDefault(_extends3); var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _keycode = require('keycode'); var _keycode2 = _interopRequireDefault(_keycode); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _all = require('react-prop-types/lib/all'); var _all2 = _interopRequireDefault(_all); var _warning = require('warning'); var _warning2 = _interopRequireDefault(_warning); var _bootstrapUtils = require('./utils/bootstrapUtils'); var _createChainedFunction = require('./utils/createChainedFunction'); var _createChainedFunction2 = _interopRequireDefault(_createChainedFunction); var _ValidComponentChildren = require('./utils/ValidComponentChildren'); var _ValidComponentChildren2 = _interopRequireDefault(_ValidComponentChildren); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } // TODO: Should we expose `` as ``? // TODO: This `bsStyle` is very unlike the others. Should we rename it? // TODO: `pullRight` and `pullLeft` don't render right outside of `navbar`. // Consider renaming or replacing them. var propTypes = { /** * Marks the NavItem with a matching `eventKey` as active. Has a * higher precedence over `activeHref`. */ activeKey: _react2['default'].PropTypes.any, /** * Marks the child NavItem with a matching `href` prop as active. */ activeHref: _react2['default'].PropTypes.string, /** * NavItems are be positioned vertically. */ stacked: _react2['default'].PropTypes.bool, justified: (0, _all2['default'])(_react2['default'].PropTypes.bool, function (_ref) { var justified = _ref.justified, navbar = _ref.navbar; return justified && navbar ? Error('justified navbar `Nav`s are not supported') : null; }), /** * A callback fired when a NavItem is selected. * * ```js * function ( * Any eventKey, * SyntheticEvent event? * ) * ``` */ onSelect: _react2['default'].PropTypes.func, /** * ARIA role for the Nav, in the context of a TabContainer, the default will * be set to "tablist", but can be overridden by the Nav when set explicitly. * * When the role is set to "tablist" NavItem focus is managed according to * the ARIA authoring practices for tabs: * https://www.w3.org/TR/2013/WD-wai-aria-practices-20130307/#tabpanel */ role: _react2['default'].PropTypes.string, /** * Apply styling an alignment for use in a Navbar. This prop will be set * automatically when the Nav is used inside a Navbar. */ navbar: _react2['default'].PropTypes.bool, /** * Float the Nav to the right. When `navbar` is `true` the appropriate * contextual classes are added as well. */ pullRight: _react2['default'].PropTypes.bool, /** * Float the Nav to the left. When `navbar` is `true` the appropriate * contextual classes are added as well. */ pullLeft: _react2['default'].PropTypes.bool }; var defaultProps = { justified: false, pullRight: false, pullLeft: false, stacked: false }; var contextTypes = { $bs_navbar: _react2['default'].PropTypes.shape({ bsClass: _react2['default'].PropTypes.string, onSelect: _react2['default'].PropTypes.func }), $bs_tabContainer: _react2['default'].PropTypes.shape({ activeKey: _react2['default'].PropTypes.any, onSelect: _react2['default'].PropTypes.func.isRequired, getTabId: _react2['default'].PropTypes.func.isRequired, getPaneId: _react2['default'].PropTypes.func.isRequired }) }; var Nav = function (_React$Component) { (0, _inherits3['default'])(Nav, _React$Component); function Nav() { (0, _classCallCheck3['default'])(this, Nav); return (0, _possibleConstructorReturn3['default'])(this, _React$Component.apply(this, arguments)); } Nav.prototype.componentDidUpdate = function componentDidUpdate() { var _this2 = this; if (!this._needsRefocus) { return; } this._needsRefocus = false; var children = this.props.children; var _getActiveProps = this.getActiveProps(), activeKey = _getActiveProps.activeKey, activeHref = _getActiveProps.activeHref; var activeChild = _ValidComponentChildren2['default'].find(children, function (child) { return _this2.isActive(child, activeKey, activeHref); }); var childrenArray = _ValidComponentChildren2['default'].toArray(children); var activeChildIndex = childrenArray.indexOf(activeChild); var childNodes = _reactDom2['default'].findDOMNode(this).children; var activeNode = childNodes && childNodes[activeChildIndex]; if (!activeNode || !activeNode.firstChild) { return; } activeNode.firstChild.focus(); }; Nav.prototype.handleTabKeyDown = function handleTabKeyDown(onSelect, event) { var nextActiveChild = void 0; switch (event.keyCode) { case _keycode2['default'].codes.left: case _keycode2['default'].codes.up: nextActiveChild = this.getNextActiveChild(-1); break; case _keycode2['default'].codes.right: case _keycode2['default'].codes.down: nextActiveChild = this.getNextActiveChild(1); break; default: // It was a different key; don't handle this keypress. return; } event.preventDefault(); if (onSelect && nextActiveChild && nextActiveChild.props.eventKey) { onSelect(nextActiveChild.props.eventKey); } this._needsRefocus = true; }; Nav.prototype.getNextActiveChild = function getNextActiveChild(offset) { var _this3 = this; var children = this.props.children; var validChildren = children.filter(function (child) { return child.props.eventKey && !child.props.disabled; }); var _getActiveProps2 = this.getActiveProps(), activeKey = _getActiveProps2.activeKey, activeHref = _getActiveProps2.activeHref; var activeChild = _ValidComponentChildren2['default'].find(children, function (child) { return _this3.isActive(child, activeKey, activeHref); }); // This assumes the active child is not disabled. var activeChildIndex = validChildren.indexOf(activeChild); if (activeChildIndex === -1) { // Something has gone wrong. Select the first valid child we can find. return validChildren[0]; } var nextIndex = activeChildIndex + offset; var numValidChildren = validChildren.length; if (nextIndex >= numValidChildren) { nextIndex = 0; } else if (nextIndex < 0) { nextIndex = numValidChildren - 1; } return validChildren[nextIndex]; }; Nav.prototype.getActiveProps = function getActiveProps() { var tabContainer = this.context.$bs_tabContainer; if (tabContainer) { process.env.NODE_ENV !== 'production' ? (0, _warning2['default'])(this.props.activeKey == null && !this.props.activeHref, 'Specifying a `