accessible-react-tabs
Version:
Accessible tab component for React
69 lines (52 loc) • 2.78 kB
JavaScript
exports.__esModule = true;
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
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; }
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 _react2.default.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;
}(_react2.default.Component);
exports.default = Tab;
module.exports = exports["default"];
;