import { createElement, MouseEvent, useCallback, useMemo } from 'react' import { animated, SpringValue } from '@react-spring/web' import { useTooltip } from '@nivo/tooltip' import { BoxPlotDatum, BoxPlotItemProps } from './types' const BoxPlotItemWhisker = ({ distStart, distEnd, whiskerEndSize, whiskerColor, whiskerWidth, }: { distStart: SpringValue distEnd: SpringValue whiskerEndSize: number whiskerColor: SpringValue whiskerWidth: number }) => { return ( <> {whiskerEndSize > 0 ? ( ) : null} ) } export const BoxPlotItem = ({ boxPlot, layout, animatedProps: { borderColor, medianColor, whiskerColor, color, opacity, transform, valueInterval, valueDistance0, valueDistance1, valueDistance3, valueDistance4, }, borderRadius, borderWidth, medianWidth, whiskerWidth, whiskerEndSize, isInteractive, onClick, onMouseEnter, onMouseLeave, setActiveItem, tooltip, isFocusable, ariaLabel, ariaLabelledBy, ariaDescribedBy, }: BoxPlotItemProps) => { const { showTooltipFromEvent, hideTooltip } = useTooltip() const vertical = layout === 'vertical' const bandwidth = vertical ? boxPlot.width : boxPlot.height const renderTooltip = useMemo(() => () => createElement(tooltip, boxPlot), [tooltip, boxPlot]) const handleClick = useCallback( (event: MouseEvent) => { onClick?.(boxPlot, event) }, [boxPlot, onClick] ) const handleTooltip = useCallback( (event: MouseEvent) => showTooltipFromEvent(renderTooltip(), event), [showTooltipFromEvent, renderTooltip] ) const handleMouseEnter = useCallback( (event: MouseEvent) => { onMouseEnter?.(boxPlot, event) showTooltipFromEvent(renderTooltip(), event) setActiveItem(boxPlot) }, [boxPlot, onMouseEnter, showTooltipFromEvent, renderTooltip, setActiveItem] ) const handleMouseLeave = useCallback( (event: MouseEvent) => { onMouseLeave?.(boxPlot, event) hideTooltip() setActiveItem(null) }, [boxPlot, hideTooltip, onMouseLeave, setActiveItem] ) const handleBlur = useCallback(() => { hideTooltip() }, [hideTooltip]) return ( ) }