2023-02-28 04:16:22 -05:00
|
|
|
import { cloneElement } from 'react'
|
2022-04-22 09:49:26 -04:00
|
|
|
import {
|
|
|
|
OverlayTrigger,
|
|
|
|
OverlayTriggerProps,
|
|
|
|
Tooltip as BSTooltip,
|
|
|
|
} from 'react-bootstrap'
|
2023-02-28 04:16:22 -05:00
|
|
|
import { callFnsInSequence } from '../../utils/functions'
|
2022-04-22 09:49:26 -04:00
|
|
|
|
2023-01-05 05:17:57 -05:00
|
|
|
type OverlayProps = Omit<OverlayTriggerProps, 'overlay'> & {
|
2022-05-18 09:46:10 -04:00
|
|
|
shouldUpdatePosition?: boolean // Not officially documented https://stackoverflow.com/a/43138470
|
2023-01-05 05:17:57 -05:00
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2023-02-21 09:34:35 -05:00
|
|
|
export type TooltipProps = {
|
2023-02-28 04:16:22 -05:00
|
|
|
description: React.ReactNode
|
2022-04-22 09:49:26 -04:00
|
|
|
id: string
|
2023-01-05 05:17:57 -05:00
|
|
|
overlayProps?: OverlayProps
|
2022-04-22 09:49:26 -04:00
|
|
|
tooltipProps?: BSTooltip.TooltipProps
|
2023-05-31 04:04:42 -04:00
|
|
|
hidden?: boolean
|
2023-02-28 04:16:22 -05:00
|
|
|
children: React.ReactElement
|
2023-02-21 09:34:35 -05:00
|
|
|
}
|
|
|
|
|
2023-02-28 04:16:22 -05:00
|
|
|
function Tooltip({
|
2023-02-21 09:34:35 -05:00
|
|
|
id,
|
|
|
|
description,
|
|
|
|
children,
|
|
|
|
tooltipProps,
|
|
|
|
overlayProps,
|
2023-05-31 04:04:42 -04:00
|
|
|
hidden,
|
2023-02-28 04:16:22 -05:00
|
|
|
}: TooltipProps) {
|
|
|
|
const hideTooltip = (e: React.MouseEvent) => {
|
|
|
|
if (e.currentTarget instanceof HTMLElement) {
|
|
|
|
e.currentTarget.blur()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
return (
|
|
|
|
<OverlayTrigger
|
|
|
|
overlay={
|
2023-05-31 04:04:42 -04:00
|
|
|
<BSTooltip
|
|
|
|
id={`${id}-tooltip`}
|
|
|
|
{...tooltipProps}
|
|
|
|
style={{ display: hidden ? 'none' : 'block' }}
|
|
|
|
>
|
2022-04-22 09:49:26 -04:00
|
|
|
{description}
|
|
|
|
</BSTooltip>
|
|
|
|
}
|
|
|
|
{...overlayProps}
|
|
|
|
placement={overlayProps?.placement || 'top'}
|
|
|
|
>
|
2023-02-28 04:16:22 -05:00
|
|
|
{cloneElement(children, {
|
|
|
|
onClick: callFnsInSequence(children.props.onClick, hideTooltip),
|
|
|
|
})}
|
2022-04-22 09:49:26 -04:00
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Tooltip
|