import { Ref, MutableRefObject, Children, isValidElement, ComponentType, Fragment, ReactElement as OriginReactElement, ReactText as OriginReactText, ReactNode as OriginReactNode, ReactChild as OriginReactChild, ReactInstance as OriginReactInstance, } from 'react' import {isUndefined} from 'lodash-es' import { hasOwnProperty, } from './help' export type ReactNodeOrRenderer = ReactNode | (() => ReactNode) export type ReactElement = OriginReactElement export type ReactText = OriginReactText export type ReactNode = OriginReactNode export type ReactChild = OriginReactChild export type ReactInstance = OriginReactInstance export function refNode(ref: Ref | MutableRefObject | undefined, node: ReactInstance) { // https://github.com/facebook/react/issues/8873 if (ref) { if (typeof ref === 'function') { ref(node) } else if (hasOwnProperty(ref, 'current')) { (ref as MutableRefObject).current = node } } } export function getElementChildren(children: ReactNode | ReactNode[]) { return Children.toArray(children).filter(child => isValidElement(child)) as ReactElement[] } export function findElementChild

(children: ReactNode | ReactNode[], type: ComponentType

) { return getElementChildren(children).find(child => child.type === type) as ReactElement | null } export function pickDerivedStateFromProps>( nextProps: Props, keys: Array ) { const state: Partial = {} keys.forEach(key => { if (!isUndefined(nextProps[key])) { state[key] = nextProps[key] } }) return Object.keys(state).length ? state : null } export function getChildrenArray(children: ReactNode): ReactNode[] { if (isValidElement(children) && children.type === Fragment) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return getChildrenArray(children.props.children) } return Children.toArray(children) } export function getTextFromReactNode(children: ReactNode): string { if (typeof children === 'string') { return children } if (typeof children === 'number') { return `${children}` } if (isValidElement(children)) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access return getTextFromReactNode(children.props.children) } return getChildrenArray(children).map(child => getTextFromReactNode(child)).join(' ').trim() }