UNPKG

accessible-react-tabs

Version:

Accessible tab component for React

60 lines (47 loc) 2.53 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 from 'react'; var Tab = function (_React$Component) { _inherits(Tab, _React$Component); function Tab(props) { _classCallCheck(this, Tab); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.state = { expanded: _this.props.index === _this.props.selectedIndex, selected: _this.props.index === _this.props.selectedIndex }; if (_this.props.index === _this.props.selectedIndex && _this.props.callback) { _this.props.callback(); } return _this; } Tab.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { this.setState({ expanded: this.props.index === nextProps.selectedIndex, selected: this.props.index === nextProps.selectedIndex }); }; Tab.prototype.render = function render() { var _this2 = this; return React.createElement( "button", { role: "tab", id: "tab-" + this.props.index, className: this.props.index === this.props.selectedIndex ? 'dz0-tab is-selected' : 'dz0-tab', "aria-selected": this.state.selected, "aria-expanded": this.state.expanded, "aria-controls": "tabpanel-" + this.props.index, tabIndex: this.props.selectedIndex === this.props.index ? '0' : -1, onClick: function onClick(event) { return _this2.props.onClick(event, _this2.props.index); }, onKeyDown: function onKeyDown(event) { return _this2.props.onKeyDown(event, _this2.props.index); } }, this.props.label ); }; return Tab; }(React.Component); export default Tab;