import { ReactElement, ReactNode, isValidElement } from 'react'; import _ from 'lodash'; /** * This file contains utility functions that can be reused across the project. */ /** * Originally made for LongForm component to find sources. * Walks the children of a React element and calls a callback function for each child. * * @param {ReactNode} children - The children of a React element. * @param {Function} callback - The callback function to call for each child. */ function walkChildren(children: ReactNode = [], callback: (el: ReactElement) => void) { const _children = _.isArray(children) ? children : [children]; const validChildren = _.filter(_children, (child) => isValidElement(child)); _.each(validChildren, (child) => { callback(child); // @ts-ignore @ts-expect-error Property 'children' does not exist on type '{}'. walkChildren(child?.props?.children ?? [], callback); }); } export { walkChildren };