// @flow import * as React from 'react'; import { useOnClickBody } from '../utils'; import { type TargetedComponentProps } from '../types'; function withTargetedClickThrough( WrappedComponent: React.ComponentType, ): React.ComponentType<{| ...Config, ...$Exact, |}> { const WrapperComponent = ({ children, closeOnClickOutside, shouldTarget, useTargetingApi, ...rest }: {| ...Config, ...$Exact, |}) => { const { canShow, onComplete, onClose, onShow } = useTargetingApi(); const handleOnComplete = () => { if (shouldTarget && canShow) { onComplete(); } }; const shouldShow = shouldTarget && canShow; useOnClickBody(onClose, !!(shouldShow && closeOnClickOutside)); if (shouldShow) { onShow(); } return ( {children} ); }; WrapperComponent.displayName = `withTargetedClickThrough(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`; return WrapperComponent; } export default withTargetedClickThrough;