UNPKG

react-gosuslugi

Version:

react-gosuslugi collection of common React UI components

81 lines (65 loc) 3.68 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.childrenPropType = childrenPropType; exports.onSelectPropType = onSelectPropType; exports.selectedIndexPropType = selectedIndexPropType; var _childrenDeepMap = require('./childrenDeepMap'); var _elementTypes = require('./elementTypes'); function childrenPropType(props, propName, componentName) { var error = void 0; var tabsCount = 0; var panelsCount = 0; var tabListFound = false; var listTabs = []; var children = props[propName]; (0, _childrenDeepMap.deepForEach)(children, function (child) { if ((0, _elementTypes.isTabList)(child)) { if (child.props && child.props.children && _typeof(child.props.children) === 'object') { (0, _childrenDeepMap.deepForEach)(child.props.children, function (listChild) { return listTabs.push(listChild); }); } if (tabListFound) { error = new Error("Found multiple 'TabList' components inside 'Tabs'. Only one is allowed."); } tabListFound = true; } if ((0, _elementTypes.isTab)(child)) { if (!tabListFound || listTabs.indexOf(child) === -1) { error = new Error("Found a 'Tab' component outside of the 'TabList' component. 'Tab' components have to be inside the 'TabList' component."); } tabsCount++; } else if ((0, _elementTypes.isTabPanel)(child)) { panelsCount++; } }); if (!error && tabsCount !== panelsCount) { error = new Error('There should be an equal number of \'Tab\' and \'TabPanel\' in `' + componentName + '`.' + ('Received ' + tabsCount + ' \'Tab\' and ' + panelsCount + ' \'TabPanel\'.')); } return error; } function onSelectPropType(props, propName, componentName, location, propFullName) { var prop = props[propName]; var name = propFullName || propName; var error = null; if (prop && typeof prop !== 'function') { error = new Error('Invalid ' + location + ' `' + name + '` of type `' + (typeof prop === 'undefined' ? 'undefined' : _typeof(prop)) + '` supplied to `' + componentName + '`, expected `function`.'); } else if (props.selectedIndex != null && prop == null) { error = new Error('The ' + location + ' `' + name + '` is marked as required in `' + componentName + '`, but its value is `undefined` or `null`.\n`onSelect` is required when `selectedIndex` is also set. Not doing so will make the tabs not do anything, as `selectedIndex` indicates that you want to handle the selected tab yourself.\nIf you only want to set the inital tab replace `selectedIndex` with `defaultIndex`.'); } return error; } function selectedIndexPropType(props, propName, componentName, location, propFullName) { var prop = props[propName]; var name = propFullName || propName; var error = null; if (prop != null && typeof prop !== 'number') { error = new Error('Invalid ' + location + ' `' + name + '` of type `' + (typeof prop === 'undefined' ? 'undefined' : _typeof(prop)) + '` supplied to `' + componentName + '`, expected `number`.'); } else if (props.defaultIndex != null && prop != null) { return new Error('The ' + location + ' `' + name + '` cannot be used together with `defaultIndex` in `' + componentName + '`.\nEither remove `' + name + '` to let `' + componentName + '` handle the selected tab internally or remove `defaultIndex` to handle it yourself.'); } return error; }