react-accessible-tabs
Version:
Accessible React tabs component
85 lines (70 loc) • 3.35 kB
JavaScript
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 Tab = function (_Component) {
_inherits(Tab, _Component);
function Tab() {
_classCallCheck(this, Tab);
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
}
Tab.prototype.componentDidUpdate = function componentDidUpdate() {
var _props = this.props;
var id = _props.id;
var index = _props.index;
var userInvokedSelection = _props.userInvokedSelection;
var selectedIndex = _props.selectedIndex;
var resetUserInvokedSelection = _props.resetUserInvokedSelection;
var isSelected = index === selectedIndex;
if (userInvokedSelection && isSelected && this.refs[id]) {
this.refs[id].focus();
resetUserInvokedSelection();
}
};
Tab.prototype.render = function render() {
var _props2 = this.props;
var id = _props2.id;
var index = _props2.index;
var label = _props2.label;
var selectedIndex = _props2.selectedIndex;
var _onClick = _props2.onClick;
var _onKeyDown = _props2.onKeyDown;
var isSelected = index === selectedIndex;
var className = classNames('tabs__trigger', { 'is-selected': isSelected });
return React.createElement(
'li',
{ className: 'tabs__tab-list-item', role: 'presentation' },
React.createElement(
'a',
{
ref: id,
'aria-controls': id,
'aria-selected': isSelected,
className: className,
href: '#' + id,
role: 'tab',
tabIndex: isSelected ? 0 : -1,
onClick: function onClick(e) {
return _onClick(e, index);
},
onKeyDown: function onKeyDown(e) {
return _onKeyDown(e);
}
},
label
)
);
};
return Tab;
}(Component);
process.env.NODE_ENV !== "production" ? Tab.propTypes = {
id: PropTypes.string,
index: PropTypes.number,
label: PropTypes.string,
selectedIndex: PropTypes.number,
onClick: PropTypes.func,
onKeyDown: PropTypes.func
} : void 0;
export default Tab;