UNPKG

react-accessible-tabs

Version:

Accessible React tabs component

60 lines (46 loc) 2.64 kB
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; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; var Panel = function (_Component) { _inherits(Panel, _Component); function Panel() { _classCallCheck(this, Panel); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } Panel.prototype.createMarkup = function createMarkup(str) { return { __html: str }; }; Panel.prototype.renderContents = function renderContents(child) { return React.isValidElement(child) ? child : React.createElement('div', { dangerouslySetInnerHTML: this.createMarkup(child) }); }; Panel.prototype.render = function render() { var _props = this.props; var children = _props.children; var id = _props.id; var index = _props.index; var selectedIndex = _props.selectedIndex; var isSelected = index === selectedIndex; var className = classNames('tabs__panel', { 'is-hidden': !isSelected }); return React.createElement( 'section', { 'aria-hidden': !isSelected, className: className, id: id, role: 'tabpanel', tabIndex: isSelected ? 0 : -1 }, React.Children.map(children, this.renderContents, this) ); }; return Panel; }(Component); process.env.NODE_ENV !== "production" ? Panel.propTypes = { children: PropTypes.oneOfType([PropTypes.element, PropTypes.string, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.element, PropTypes.string]))]), id: PropTypes.string, index: PropTypes.number, selectedIndex: PropTypes.number } : void 0; export default Panel;