UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

38 lines (37 loc) 1.43 kB
"use client"; import { Children, Fragment, isValidElement, useCallback, useEffect, useRef } from 'react'; import { warn } from "../../../../shared/helpers.js"; export function useVerifyChildren({ children, message, messageInfo, ignoreTypes }) { 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 = (children, ignoreTypes, count = 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 || (_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type['name'])) ? 1 : 0); }, count); }; //# sourceMappingURL=useVerifyChildren.js.map