UNPKG

react-accessible-tabs

Version:

Accessible React tabs component

109 lines (86 loc) 4.31 kB
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; }; 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 TabList from './TabList'; import Panels from './Panels'; var Tabs = function (_Component) { _inherits(Tabs, _Component); function Tabs(props) { _classCallCheck(this, Tabs); var _this = _possibleConstructorReturn(this, _Component.call(this, props)); _this.handleClick = function (e, index) { e.preventDefault(); if (_this.state.selectedIndex === index) { return; } _this.setState({ userInvokedSelection: true, selectedIndex: index }); }; _this.handleKeyDown = function (e) { if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') { e.preventDefault(); } else { return; } var targetIndex = void 0; if (e.key === 'ArrowLeft' && _this.state.selectedIndex > 0) { targetIndex = _this.state.selectedIndex - 1; } else if (e.key === 'ArrowRight' && _this.state.selectedIndex < _this.tabTriggersLength - 1) { targetIndex = _this.state.selectedIndex + 1; } else { return; } _this.setState({ userInvokedSelection: true, selectedIndex: targetIndex }); }; _this.resetUserInvokedSelection = function () { _this.setState({ userInvokedSelection: false }); }; _this.tabTriggersLength = _this.props.data.length; _this.state = { userInvokedSelection: false, selectedIndex: _this.props.initialSelectedIndex < 0 || _this.props.initialSelectedIndex > _this.tabTriggersLength - 1 ? 0 : _this.props.initialSelectedIndex }; return _this; } Tabs.prototype.render = function render() { if (!this.tabTriggersLength) { return null; } return React.createElement( 'div', { className: 'tabs' }, React.createElement(TabList, _extends({}, this.props, { userInvokedSelection: this.state.userInvokedSelection, selectedIndex: this.state.selectedIndex, onClick: this.handleClick, onKeyDown: this.handleKeyDown, resetUserInvokedSelection: this.resetUserInvokedSelection })), React.createElement(Panels, _extends({}, this.props, { selectedIndex: this.state.selectedIndex })) ); }; return Tabs; }(Component); Tabs.defaultProps = { data: [], initialSelectedIndex: 0 }; process.env.NODE_ENV !== "production" ? Tabs.propTypes = { data: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, content: PropTypes.oneOfType([PropTypes.element, PropTypes.string, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.element, PropTypes.string]))]) })), selectedIndex: PropTypes.number, initialSelectedIndex: PropTypes.number } : void 0; export default Tabs;