UNPKG

react-tabs

Version:

An accessible and easy tab component for ReactJS

135 lines (107 loc) 4.61 kB
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; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { childrenPropType, onSelectPropType, selectedIndexPropType } from '../helpers/propTypes'; import UncontrolledTabs from './UncontrolledTabs'; import { getTabsCount } from '../helpers/count'; 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 = 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.createElement(UncontrolledTabs, props, children); }; return Tabs; }(Component); Tabs.defaultProps = { defaultFocus: false, forceRenderTabPanel: false, selectedIndex: null, defaultIndex: null }; export { Tabs as default }; Tabs.propTypes = process.env.NODE_ENV !== "production" ? { children: childrenPropType, className: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]), defaultFocus: PropTypes.bool, defaultIndex: PropTypes.number, disabledTabClassName: PropTypes.string, domRef: PropTypes.func, forceRenderTabPanel: PropTypes.bool, onSelect: onSelectPropType, selectedIndex: selectedIndexPropType, selectedTabClassName: PropTypes.string, selectedTabPanelClassName: PropTypes.string } : {}; Tabs.tabsRole = 'Tabs';