diff --git a/src/component/DefaultTooltipContent.tsx b/src/component/DefaultTooltipContent.tsx index 4b7c381269..0ec1289527 100644 --- a/src/component/DefaultTooltipContent.tsx +++ b/src/component/DefaultTooltipContent.tsx @@ -2,7 +2,7 @@ * @fileOverview Default Tooltip Content */ import _ from 'lodash'; -import React, { PureComponent, CSSProperties, ReactNode } from 'react'; +import React, { CSSProperties, ReactNode } from 'react'; import classNames from 'classnames'; import { isNumOrStr } from '../util/DataUtils'; @@ -19,7 +19,7 @@ export type Formatter = ( item: Payload, index: number, payload: Array>, -) => [TValue, TName] | TValue; +) => [React.ReactNode, TName] | React.ReactNode; export interface Payload { type?: TooltipType; @@ -50,21 +50,24 @@ export interface Props { itemSorter?: (item: Payload) => number | string; } -export class DefaultTooltipContent extends PureComponent< - Props -> { - static displayName = 'DefaultTooltipContent'; - - static defaultProps = { - separator: ' : ', - contentStyle: {}, - itemStyle: {}, - labelStyle: {}, - }; - - renderContent() { - const { payload, separator, formatter, itemStyle, itemSorter } = this.props; +export const DefaultTooltipContent = ( + props: Props, +) => { + const { + separator = ' : ', + contentStyle = {}, + itemStyle = {}, + labelStyle = {}, + payload, + formatter, + itemSorter, + wrapperClassName, + labelClassName, + label, + labelFormatter, + } = props; + const renderContent = () => { if (payload && payload.length) { const listStyle = { padding: 0, margin: 0 }; @@ -81,21 +84,24 @@ export class DefaultTooltipContent - {isNumOrStr(name) ? {name} : null} - {isNumOrStr(name) ? {separator} : null} - {value} + {isNumOrStr(finalName) ? {finalName} : null} + {isNumOrStr(finalName) ? {separator} : null} + {finalValue} {entry.unit || ''} ); @@ -109,38 +115,35 @@ export class DefaultTooltipContent -

- {React.isValidElement(finalLabel) ? finalLabel : `${finalLabel}`} -

- {this.renderContent()} - - ); + if (hasLabel && labelFormatter && payload !== undefined && payload !== null) { + finalLabel = labelFormatter(label, payload); } -} + + return ( +
+

+ {React.isValidElement(finalLabel) ? finalLabel : `${finalLabel}`} +

+ {renderContent()} +
+ ); +}; diff --git a/storybook/stories/API/tooltip/Tooltip.stories.tsx b/storybook/stories/API/tooltip/Tooltip.stories.tsx index ec0bd557d5..8088ace2d3 100644 --- a/storybook/stories/API/tooltip/Tooltip.stories.tsx +++ b/storybook/stories/API/tooltip/Tooltip.stories.tsx @@ -88,6 +88,23 @@ export const MultipleValuedTooltip = { }, }; +export const TriggerTooltipByClick = { + render: (tooltipArgs: Record) => { + return ( + + + + + + {Marks.map(({ marks, fill }) => ( + + ))} + + + ); + }, +}; + const CustomMultipleValueTooltip = ({ active, payload }: CustomTooltipProps) => { if (active && payload && payload.length > 0) { return ( diff --git a/storybook/stories/Examples/Tooltip.stories.tsx b/storybook/stories/Examples/Tooltip.stories.tsx index 89c7ac4d27..c5794d919c 100644 --- a/storybook/stories/Examples/Tooltip.stories.tsx +++ b/storybook/stories/Examples/Tooltip.stories.tsx @@ -15,9 +15,11 @@ export const LockedByClick = { // Their update is interrupted by the click event, so we need to store them in a state. const [tooltipData, setTooltipData] = React.useState<{ payload?: unknown[]; label?: string; x?: number }>({}); - // A custom Tooltip that updates the payload of the tooltip if the chart is locked, and either way always renders using the normal Tooltip. + // A custom Tooltip that updates the payload of the tooltip if the + // chart is locked, and either way always renders using the normal Tooltip. const CustomTooltip = (props: any) => { - // If the chart is locked, and the payload is not empty, and the x position of the tooltip has changed, update the tooltipData. + // If the chart is locked, and the payload is not empty, and the + // x position of the tooltip has changed, update the tooltipData. if (!isLocked && props.payload && props.payload.length > 0 && props.coordinate.x !== tooltipData.x) { setTooltipData({ payload: props.payload, x: props.coordinate.x, label: props.label }); }