UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

40 lines (39 loc) 1.54 kB
"use client"; import { Children, Fragment, isValidElement, useCallback, useEffect, useRef } from 'react'; import { warn } from '../../../../shared/helpers'; export function useVerifyChildren(_ref) { let { children, message, messageInfo, ignoreTypes } = _ref; const verifyCount = useRef(0); verifyCount.current = 0; const verifyChild = useCallback(() => { verifyCount.current += 1; }, []); useEffect(() => { if (process.env.NODE_ENV !== 'production') { const count = countChildren(children, ignoreTypes); if (count > 0 && count > verifyCount.current) { warn(message, messageInfo); } } }, [children, message]); return { verifyChild }; } export const countChildren = function (children, ignoreTypes) { let count = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; return Children.toArray(children).reduce((count, child) => { var _child$type; if ((child === null || child === void 0 ? void 0 : child['type']) === Fragment) { var _child$props; return countChildren((_child$props = child['props']) === null || _child$props === void 0 ? void 0 : _child$props.children, ignoreTypes, count); } return count + (isValidElement(child) && !(ignoreTypes !== null && ignoreTypes !== void 0 && ignoreTypes.includes(child === null || child === void 0 ? void 0 : (_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type['name'])) ? 1 : 0); }, count); }; //# sourceMappingURL=useVerifyChildren.js.map