forked from GenSpectrum/cov-spectrum-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RechartsTooltip.tsx
36 lines (34 loc) · 1.01 KB
/
RechartsTooltip.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { TooltipProps } from 'recharts';
import { NameType, ValueType } from 'recharts/types/component/DefaultTooltipContent';
import React, { useEffect } from 'react';
import { Payload } from 'recharts/types/component/DefaultTooltipContent';
export function SetCurrentDataSideEffect<Data>({
tooltipProps,
setCurrentData,
}: {
tooltipProps: TooltipProps<ValueType, NameType>;
setCurrentData: (data: Data) => void;
}) {
return (
<TooltipSideEffect
tooltipProps={tooltipProps}
sideEffect={tooltipProps => {
if (tooltipProps?.payload?.length === undefined || tooltipProps.payload.length > 0) {
setCurrentData((tooltipProps.payload as Payload<any, any>[])[0].payload);
}
}}
/>
);
}
export function TooltipSideEffect({
tooltipProps,
sideEffect,
}: {
tooltipProps: TooltipProps<ValueType, NameType>;
sideEffect: (data: TooltipProps<ValueType, NameType>) => void;
}) {
useEffect(() => {
sideEffect(tooltipProps);
}, [tooltipProps, sideEffect]);
return null;
}