cpui-components
Version:
110 lines (84 loc) • 4.49 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Tabs = undefined;
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);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; } // Based on code from https://medium.com/trisfera/a-simple-react-tabs-component-47cac2cfbb5
var Tabs = exports.Tabs = function (_Component) {
_inherits(Tabs, _Component);
function Tabs(props, context) {
_classCallCheck(this, Tabs);
var _this = _possibleConstructorReturn(this, (Tabs.__proto__ || Object.getPrototypeOf(Tabs)).call(this, props, context));
_this.state = {
activeTabIndex: _this.props.defaultActiveTabIndex
};
_this.handleTabClick = _this.handleTabClick.bind(_this);
return _this;
}
_createClass(Tabs, [{
key: 'handleTabClick',
value: function handleTabClick(tabIndex) {
this.setState({
activeTabIndex: tabIndex === this.state.activeTabIndex ? this.props.defaultActiveTabIndex : tabIndex
});
}
// Encapsulate <Tabs/> component API as props for <Tab/> children
}, {
key: 'renderChildrenWithTabsApiAsProps',
value: function renderChildrenWithTabsApiAsProps() {
var _this2 = this;
return _react2.default.Children.map(this.props.children, function (child, index) {
return _react2.default.cloneElement(child, {
onClick: _this2.handleTabClick,
tabIndex: index,
isActive: index === _this2.state.activeTabIndex
});
});
}
// Render current active tab content
}, {
key: 'renderActiveTabContent',
value: function renderActiveTabContent() {
var children = this.props.children;
var activeTabIndex = this.state.activeTabIndex;
if (children[activeTabIndex]) {
return children[activeTabIndex].props.children;
}
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement(
'div',
{ className: 'cp-Tabs' },
_react2.default.createElement(
'ul',
{ className: 'cp-Tabs-list' },
this.renderChildrenWithTabsApiAsProps()
),
_react2.default.createElement(
'div',
{ className: 'cp-Tabs-panel is-active' },
this.renderActiveTabContent()
)
);
}
}]);
return Tabs;
}(_react.Component);
;
Tabs.propTypes = {
defaultActiveTabIndex: _propTypes2.default.number
};
Tabs.defaultProps = {
defaultActiveTabIndex: 0
};
exports.default = Tabs;
;