UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

45 lines (44 loc) 1.63 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.countChildren = void 0; exports.useVerifyChildren = useVerifyChildren; var _react = require("react"); var _helpers = require("../../../../shared/helpers.js"); function useVerifyChildren({ children, message, messageInfo, ignoreTypes }) { const verifyCount = (0, _react.useRef)(0); verifyCount.current = 0; const verifyChild = (0, _react.useCallback)(() => { verifyCount.current += 1; }, []); (0, _react.useEffect)(() => { if (process.env.NODE_ENV !== 'production') { const count = countChildren(children, ignoreTypes); if (count > 0 && count > verifyCount.current) { (0, _helpers.warn)(message, messageInfo); } } }, [children, message]); return { verifyChild }; } const countChildren = (children, ignoreTypes, count = 0) => { return _react.Children.toArray(children).reduce((count, child) => { var _child$type; if ((child === null || child === void 0 ? void 0 : child['type']) === _react.Fragment) { var _child$props; return countChildren((_child$props = child['props']) === null || _child$props === void 0 ? void 0 : _child$props.children, ignoreTypes, count); } return count + ((0, _react.isValidElement)(child) && !(ignoreTypes !== null && ignoreTypes !== void 0 && ignoreTypes.includes(child === null || child === void 0 || (_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type['name'])) ? 1 : 0); }, count); }; exports.countChildren = countChildren; //# sourceMappingURL=useVerifyChildren.js.map