UNPKG

react-tabs

Version:

An accessible and easy tab component for ReactJS

148 lines (112 loc) 5.44 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireWildcard(require("react")); var _propTypes2 = require("../helpers/propTypes"); var _UncontrolledTabs = _interopRequireDefault(require("./UncontrolledTabs")); var _count = require("../helpers/count"); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } var Tabs = /*#__PURE__*/ function (_Component) { _inheritsLoose(Tabs, _Component); function Tabs(props) { var _this; _this = _Component.call(this, props) || this; _this.handleSelected = function (index, last, event) { var onSelect = _this.props.onSelect; // Call change event handler if (typeof onSelect === 'function') { // Check if the change event handler cancels the tab change if (onSelect(index, last, event) === false) return; } var state = { // Set focus if the change was triggered from the keyboard focus: event.type === 'keydown' }; if (Tabs.inUncontrolledMode(_this.props)) { // Update selected index state.selectedIndex = index; } _this.setState(state); }; _this.state = Tabs.copyPropsToState(_this.props, {}, props.defaultFocus); return _this; } var _proto = Tabs.prototype; _proto.componentWillReceiveProps = function componentWillReceiveProps(newProps) { if (process.env.NODE_ENV !== 'production' && Tabs.inUncontrolledMode(newProps) !== Tabs.inUncontrolledMode(this.props)) { throw new Error("Switching between controlled mode (by using `selectedIndex`) and uncontrolled mode is not supported in `Tabs`.\nFor more information about controlled and uncontrolled mode of react-tabs see the README."); } // Use a transactional update to prevent race conditions // when reading the state in copyPropsToState // See https://github.com/reactjs/react-tabs/issues/51 this.setState(function (state) { return Tabs.copyPropsToState(newProps, state); }); }; Tabs.inUncontrolledMode = function inUncontrolledMode(props) { return props.selectedIndex === null; }; // preserve the existing selectedIndex from state. // If the state has not selectedIndex, default to the defaultIndex or 0 Tabs.copyPropsToState = function copyPropsToState(props, state, focus) { if (focus === void 0) { focus = false; } var newState = { focus: focus }; if (Tabs.inUncontrolledMode(props)) { var maxTabIndex = (0, _count.getTabsCount)(props.children) - 1; var selectedIndex = null; if (state.selectedIndex != null) { selectedIndex = Math.min(state.selectedIndex, maxTabIndex); } else { selectedIndex = props.defaultIndex || 0; } newState.selectedIndex = selectedIndex; } return newState; }; _proto.render = function render() { var _this$props = this.props, children = _this$props.children, defaultIndex = _this$props.defaultIndex, defaultFocus = _this$props.defaultFocus, props = _objectWithoutPropertiesLoose(_this$props, ["children", "defaultIndex", "defaultFocus"]); var _this$state = this.state, focus = _this$state.focus, selectedIndex = _this$state.selectedIndex; props.focus = focus; props.onSelect = this.handleSelected; if (selectedIndex != null) { props.selectedIndex = selectedIndex; } return _react.default.createElement(_UncontrolledTabs.default, props, children); }; return Tabs; }(_react.Component); exports.default = Tabs; Tabs.defaultProps = { defaultFocus: false, forceRenderTabPanel: false, selectedIndex: null, defaultIndex: null }; Tabs.propTypes = process.env.NODE_ENV !== "production" ? { children: _propTypes2.childrenPropType, className: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.array, _propTypes.default.object]), defaultFocus: _propTypes.default.bool, defaultIndex: _propTypes.default.number, disabledTabClassName: _propTypes.default.string, domRef: _propTypes.default.func, forceRenderTabPanel: _propTypes.default.bool, onSelect: _propTypes2.onSelectPropType, selectedIndex: _propTypes2.selectedIndexPropType, selectedTabClassName: _propTypes.default.string, selectedTabPanelClassName: _propTypes.default.string } : {}; Tabs.tabsRole = 'Tabs';