/** * @fileOverview Default Tooltip Content */ import * as React from 'react'; import { CSSProperties, HTMLAttributes, ReactNode, SVGProps } from 'react'; import sortBy from 'es-toolkit/compat/sortBy'; import { clsx } from 'clsx'; import { isNullish, isNumOrStr } from '../util/DataUtils'; import { DataKey } from '../util/types'; function defaultFormatter(value: TValue) { return Array.isArray(value) && isNumOrStr(value[0]) && isNumOrStr(value[1]) ? (value.join(' ~ ') as TValue) : value; } export type TooltipType = 'none'; export type ValueType = number | string | Array; export type NameType = number | string; export type Formatter = ( value: TValue, name: TName, item: Payload, index: number, payload: ReadonlyArray>, ) => [React.ReactNode, TName] | React.ReactNode; export interface Payload extends Omit, 'name'> { type?: TooltipType; color?: string; formatter?: Formatter; name?: TName; value?: TValue; unit?: ReactNode; fill?: string; dataKey?: DataKey; nameKey?: DataKey; payload?: any; chartType?: string; stroke?: string; strokeDasharray?: string | number; strokeWidth?: number | string; className?: string; hide?: boolean; } export interface Props { separator?: string; wrapperClassName?: string; labelClassName?: string; formatter?: Formatter; contentStyle?: CSSProperties; itemStyle?: CSSProperties; labelStyle?: CSSProperties; labelFormatter?: (label: any, payload: ReadonlyArray>) => ReactNode; label?: any; payload?: ReadonlyArray>; itemSorter?: 'dataKey' | 'value' | 'name' | ((item: Payload) => number | string); accessibilityLayer: boolean; } export const DefaultTooltipContent = ( props: Props, ) => { const { separator = ' : ', contentStyle = {}, itemStyle = {}, labelStyle = {}, payload, formatter, itemSorter, wrapperClassName, labelClassName, label, labelFormatter, accessibilityLayer = false, } = props; const renderContent = () => { if (payload && payload.length) { const listStyle = { padding: 0, margin: 0 }; const items = (itemSorter ? sortBy(payload, itemSorter) : payload).map( (entry: { type?: any; formatter?: any; color?: any; unit?: any; value?: any; name?: any }, i: any) => { if (entry.type === 'none') { return null; } const finalFormatter = entry.formatter || formatter || defaultFormatter; const { value, name } = entry; let finalValue: React.ReactNode = value; let finalName: React.ReactNode = name; if (finalFormatter) { const formatted = finalFormatter(value, name, entry, i, payload); if (Array.isArray(formatted)) { [finalValue, finalName] = formatted; } else if (formatted != null) { finalValue = formatted; } else { return null; } } const finalItemStyle = { display: 'block', paddingTop: 4, paddingBottom: 4, color: entry.color || '#000', ...itemStyle, }; return ( // eslint-disable-next-line react/no-array-index-key
  • {isNumOrStr(finalName) ? {finalName} : null} {isNumOrStr(finalName) ? {separator} : null} {finalValue} {entry.unit || ''}
  • ); }, ); return (
      {items}
    ); } return null; }; const finalStyle: React.CSSProperties = { margin: 0, padding: 10, backgroundColor: '#fff', border: '1px solid #ccc', whiteSpace: 'nowrap', ...contentStyle, }; const finalLabelStyle = { margin: 0, ...labelStyle, }; const hasLabel = !isNullish(label); let finalLabel = hasLabel ? label : ''; const wrapperCN = clsx('recharts-default-tooltip', wrapperClassName); const labelCN = clsx('recharts-tooltip-label', labelClassName); if (hasLabel && labelFormatter && payload !== undefined && payload !== null) { finalLabel = labelFormatter(label, payload); } const accessibilityAttributes = accessibilityLayer ? ({ role: 'status', 'aria-live': 'assertive', } as HTMLAttributes) : {}; return (

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

    {renderContent()}
    ); };