optimizely-oui
Version:
Optimizely's Component Library.
68 lines (56 loc) • 3.14 kB
JavaScript
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from "react";
import classNames from "classnames";
import PropTypes from "prop-types";
import Tab from "./Tab";
/**
* Wrapper component for a set of tabs
* @param {Object} props - Properties passed to component
* @returns {ReactElement}
*/
var TabNav = function TabNav(_ref) {
var activeTab = _ref.activeTab,
children = _ref.children,
className = _ref.className,
style = _ref.style,
testSection = _ref.testSection,
props = _objectWithoutProperties(_ref, ["activeTab", "children", "className", "style", "testSection"]);
var tabStyleClasses = style ? style.map(function (styleName) {
return "oui-tabs--".concat(styleName);
}) : "";
var classes = classNames(tabStyleClasses, "oui-tabs", className); // Determine if the child is an active tab.
// From http://stackoverflow.com/questions/32370994/how-to-pass-props-to-this-props-children
var childrenWithProps = React.Children.map(children, function (child) {
// guard against conditionally rendered children
if (child === null) {
return null;
}
return React.cloneElement(child, {
isActive: activeTab === child.props.tabId
});
});
return React.createElement("div", _extends({
"data-oui-component": true,
"data-test-section": testSection,
className: classes
}, props), React.createElement("ul", {
className: "oui-tabs-nav",
"data-test-section": "tabs-menu"
}, childrenWithProps));
};
TabNav.propTypes = {
/** Id corresponding to which tab should be given the active class */
activeTab: PropTypes.string.isRequired,
/** Set of Tab components */
children: PropTypes.node.isRequired,
/** CSS class names. */
className: PropTypes.string,
/** Various styles that can be given to the navigation */
style: PropTypes.arrayOf(PropTypes.oneOf(["center", "dashboard", "header", "left-pad", "plain", "small", "sub"])),
/** Hook for automated JavaScript tests */
testSection: PropTypes.string
};
TabNav.Tab = Tab;
export default TabNav;