UNPKG

@danilandreev/material-docs

Version:

material-docs - react framework for easy creating documentation site in material design style.

88 lines (72 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = getContainerByType; var _react = _interopRequireDefault(require("react")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _createForOfIteratorHelper(o) { if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) { var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var it, normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } /** * getContainerByType - function designed to get containers from components list by types. * @function * @param {JSX.Element} children * @param {string | string[]} types * @param {boolean} invert * @param {boolean} multiple * @return {JSX.Element | JSX.Element[] | null} */ function getContainerByType(children, types, invert) { var multiple = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; /** * checkType - function, designed to check if candidate match type in types. * @function * @param {JSX.Element} candidate * @return {boolean} */ function checkType(candidate) { /** * typeMatch - returns true is candidate component match input type. * @function * @param {JSX.Element} candidate * @param {string} type * @return boolean */ function typeMatch(candidate, type) { if (typeof type !== "string") console.error("MaterialDocs: getChildrenFromContainer: incorrect type of variable 'type', expected \"string | object\", got ".concat(_typeof(type), "!")); if (!invert && candidate.type && candidate.type.displayName === type) return true; if (invert && candidate.type && candidate.type.displayName !== type) return true; return false; } if (Array.isArray(types)) { var _iterator = _createForOfIteratorHelper(types), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var type = _step.value; if (typeMatch(candidate, type)) return true; } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } else if (typeof types === "string" || _typeof(types) === "object") { return typeMatch(candidate, types); } return false; } var candidates = _react.default.Children.map(children, function (candidate) { return /*#__PURE__*/_react.default.isValidElement(candidate) && checkType(candidate) ? candidate : undefined; }); if (!multiple && candidates.length > 1) console.error("DocsLayout: Component can contain only one child with type ".concat(types)); var result = null; if (candidates.length === 1 || candidates.length > 1 && !multiple) { result = candidates[0]; } else if (candidates.length > 1) { result = candidates; } return result; }