Skip to content

Commit

Permalink
Add isTooltipPresent
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelVanecek committed Oct 30, 2023
1 parent 831cef9 commit 41ef4ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/component/Tooltip.tsx
Expand Up @@ -44,6 +44,7 @@ function ContextUpdater(props: TooltipProps<any, any>) {
const [, setContext] = useContext(ChartContext);
useEffect(() => {
const newContext: ChartContextType = {
isTooltipPresent: true,
content: props.content,
cursor: props.cursor,
isAnimationActive: props.isAnimationActive,
Expand All @@ -63,6 +64,9 @@ function ContextUpdater(props: TooltipProps<any, any>) {
position: props.position,
};
setContext(newContext);
return function cleanup() {
setContext({ isTooltipPresent: false });
};
}, [props, setContext]);

return <></>;
Expand Down
4 changes: 3 additions & 1 deletion src/context/chartContext.tsx
Expand Up @@ -12,6 +12,7 @@ export type AllowInDimension = {
};

export type ChartContextType = {
isTooltipPresent: boolean;
allowEscapeViewBox: AllowInDimension;
animationDuration: AnimationDuration;
animationEasing: AnimationTiming;
Expand All @@ -32,6 +33,7 @@ function defaultUniqBy(entry: Payload<any, any>) {
}

const defaultValue: ChartContextType = {
isTooltipPresent: false,
content: DefaultTooltipContent,
cursor: true,
isAnimationActive: !Global.isSsr,
Expand All @@ -54,7 +56,7 @@ const defaultValue: ChartContextType = {
};

export const ChartContext = createContext<
[context: ChartContextType, setContext: (newContext: ChartContextType) => void]
[context: ChartContextType, setContext: (newContext: Partial<ChartContextType>) => void]
>([defaultValue, () => undefined]);

export function ChartContextContainer(props: { children: ReactNode }) {
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/TooltipRenderer.tsx
Expand Up @@ -11,7 +11,8 @@ import React, {
useState,
} from 'react';
import { translateStyle } from 'react-smooth';
import _ from 'lodash';
import isNil from 'lodash/isNil';
import isFunction from 'lodash/isFunction';
import { ChartContext } from '../context/chartContext';
import {
CartesianViewBox,
Expand Down Expand Up @@ -122,7 +123,7 @@ function useUniqPayload(payload: Array<Payload<any, any>>) {
const [context] = useContext(ChartContext);
const { payloadUniqBy, filterNull } = context;
const finalPayload = getUniqPayload(
filterNull && payload && payload.length ? payload.filter(entry => !_.isNil(entry.value)) : payload,
filterNull && payload && payload.length ? payload.filter(entry => !isNil(entry.value)) : payload,
payloadUniqBy,
defaultUniqBy,
);
Expand All @@ -138,7 +139,7 @@ function renderContent<TValue extends ValueType, TName extends NameType>(
if (React.isValidElement(content)) {
return React.cloneElement(content, props);
}
if (_.isFunction(content)) {
if (isFunction(content)) {
return React.createElement(content as any, props);
}

Expand Down Expand Up @@ -284,7 +285,12 @@ function BoundingBoxSizeWrapper({
}

export function TooltipRenderer(props: Props) {
const [context] = useContext(ChartContext);
const { finalPayload, hasPayload } = useUniqPayload(props.activePayload);
const { isTooltipPresent } = context;
if (!isTooltipPresent) {
return null;
}
return (
<>
<CursorRenderer {...props} />
Expand Down

0 comments on commit 41ef4ed

Please sign in to comment.