UNPKG

react-gosuslugi

Version:

react-gosuslugi collection of common React UI components

165 lines (126 loc) 6.45 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes3 = require('./helpers/propTypes'); var _UncontrolledTabs = require('./UncontrolledTabs'); var _UncontrolledTabs2 = _interopRequireDefault(_UncontrolledTabs); var _count = require('./helpers/count'); require('./Tabs.sass'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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; } {/* eslint-disable */} var Tabs = function (_PureComponent) { _inherits(Tabs, _PureComponent); function Tabs(props) { _classCallCheck(this, Tabs); var _this = _possibleConstructorReturn(this, (Tabs.__proto__ || Object.getPrototypeOf(Tabs)).call(this, props)); _this.handleSelected = function (index, last, event) { // Call change event handler if (typeof _this.props.onSelect === 'function') { // Check if the change event handler cancels the tab change if (_this.props.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, {}, _this.props.defaultFocus); return _this; } _createClass(Tabs, [{ key: 'componentWillReceiveProps', value: 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); }); } }, { key: 'render', value: function render() { var _props = this.props, children = _props.children, defaultIndex = _props.defaultIndex, defaultFocus = _props.defaultFocus, props = _objectWithoutProperties(_props, ['children', 'defaultIndex', 'defaultFocus']); props.focus = this.state.focus; props.onSelect = this.handleSelected; if (this.state.selectedIndex != null) { props.selectedIndex = this.state.selectedIndex; } return _react2.default.createElement( _UncontrolledTabs2.default, props, children ); } }], [{ key: 'inUncontrolledMode', value: function inUncontrolledMode(props) { return props.selectedIndex === null; } }, { key: 'copyPropsToState', // preserve the existing selectedIndex from state. // If the state has not selectedIndex, default to the defaultIndex or 0 value: function copyPropsToState(props, state) { var focus = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 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; } }]); return Tabs; }(_react.PureComponent); Tabs.defaultProps = { defaultFocus: false, forceRenderTabPanel: false, selectedIndex: null, defaultIndex: null }; Tabs.propTypes = { children: _propTypes3.childrenPropType, className: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.array, _propTypes2.default.object]), defaultFocus: _propTypes2.default.bool, defaultIndex: _propTypes2.default.number, disabledTabClassName: _propTypes2.default.string, domRef: _propTypes2.default.func, forceRenderTabPanel: _propTypes2.default.bool, onSelect: _propTypes3.onSelectPropType, selectedIndex: _propTypes3.selectedIndexPropType, selectedTabClassName: _propTypes2.default.string, selectedTabPanelClassName: _propTypes2.default.string }; exports.default = Tabs; Tabs.tabsRole = 'Tabs';