feat: enhance chart tooltip and legend components with improved type definitions and payload handling

This commit is contained in:
Julien Froidefond
2025-11-27 10:02:27 +01:00
parent 66c4ead350
commit c6299de8b2
5 changed files with 2405 additions and 18 deletions

View File

@@ -104,6 +104,15 @@ ${colorConfig
const ChartTooltip = RechartsPrimitive.Tooltip
type TooltipPayloadItem = {
dataKey?: string | number
name?: string
value?: number | string
color?: string
payload?: Record<string, unknown> & { fill?: string }
fill?: string
}
function ChartTooltipContent({
active,
payload,
@@ -118,13 +127,15 @@ function ChartTooltipContent({
color,
nameKey,
labelKey,
}: React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
}: Omit<React.ComponentProps<typeof RechartsPrimitive.Tooltip>, 'payload' | 'label'> &
React.ComponentProps<'div'> & {
hideLabel?: boolean
hideIndicator?: boolean
indicator?: 'line' | 'dot' | 'dashed'
nameKey?: string
labelKey?: string
payload?: TooltipPayloadItem[]
label?: string | number
}) {
const { config } = useChart()
@@ -179,10 +190,10 @@ function ChartTooltipContent({
>
{!nestLabel ? tooltipLabel : null}
<div className="grid gap-1.5">
{payload.map((item, index) => {
{payload.map((item: TooltipPayloadItem, index: number) => {
const key = `${nameKey || item.name || item.dataKey || 'value'}`
const itemConfig = getPayloadConfigFromPayload(config, item, key)
const indicatorColor = color || item.payload.fill || item.color
const indicatorColor = color || item.payload?.fill || item.color
return (
<div
@@ -193,7 +204,7 @@ function ChartTooltipContent({
)}
>
{formatter && item?.value !== undefined && item.name ? (
formatter(item.value, item.name, item, index, item.payload)
formatter(item.value, item.name, item as never, index, item.payload as never)
) : (
<>
{itemConfig?.icon ? (
@@ -250,16 +261,23 @@ function ChartTooltipContent({
const ChartLegend = RechartsPrimitive.Legend
type LegendPayloadItem = {
value?: string
dataKey?: string | number
color?: string
}
function ChartLegendContent({
className,
hideIcon = false,
payload,
verticalAlign = 'bottom',
nameKey,
}: React.ComponentProps<'div'> &
Pick<RechartsPrimitive.LegendProps, 'payload' | 'verticalAlign'> & {
}: React.ComponentProps<'div'> & {
hideIcon?: boolean
nameKey?: string
payload?: LegendPayloadItem[]
verticalAlign?: 'top' | 'bottom' | 'middle'
}) {
const { config } = useChart()
@@ -275,7 +293,7 @@ function ChartLegendContent({
className,
)}
>
{payload.map((item) => {
{payload.map((item: LegendPayloadItem) => {
const key = `${nameKey || item.dataKey || 'value'}`
const itemConfig = getPayloadConfigFromPayload(config, item, key)