Skip to content

Commit 7ecfb12

Browse files
committed
feat: Adds details renderer to core tooltip API
1 parent e58d1cc commit 7ecfb12

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/core/components/core-tooltip.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,15 @@ function getTooltipContentPie(
215215
</Box>
216216
</div>
217217
),
218-
body: renderers.body?.(tooltipDetails) ?? (
219-
// We expect all pie chart segments to have defined y values. We use y=0 as fallback
220-
// because the property is optional in Highcharts types.
221-
<ChartSeriesDetails details={[{ key: point.series.name, value: point.y ?? 0 }]} />
222-
),
218+
body:
219+
renderers.body?.(tooltipDetails) ??
220+
(renderers.details ? (
221+
<ChartSeriesDetails details={renderers.details({ point })} compactList={true} />
222+
) : (
223+
// We expect all pie chart segments to have defined y values. We use y=0 as fallback
224+
// because the property is optional in Highcharts types.
225+
<ChartSeriesDetails details={[{ key: point.series.name, value: point.y ?? 0 }]} />
226+
)),
223227
footer: renderers.footer?.(tooltipDetails),
224228
};
225229
}

src/core/interfaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ export namespace CoreChartProps {
428428
}
429429
export interface TooltipContentRenderer {
430430
point?: (props: TooltipPointProps) => TooltipPointFormatted;
431+
details?: (props: TooltipDetailsProps) => readonly TooltipDetail[];
431432
header?: (props: TooltipSlotProps) => React.ReactNode;
432433
body?: (props: TooltipSlotProps) => React.ReactNode;
433434
footer?: (props: TooltipSlotProps) => React.ReactNode;
@@ -445,6 +446,15 @@ export namespace CoreChartProps {
445446
items: TooltipContentItem[];
446447
}
447448

449+
export interface TooltipDetailsProps {
450+
point: Highcharts.Point;
451+
}
452+
453+
export interface TooltipDetail {
454+
key: React.ReactNode;
455+
value: React.ReactNode;
456+
}
457+
448458
export interface LegendItemHighlightDetail {
449459
item: LegendItem;
450460
}

src/pie-chart/chart-pie-internal.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { CoreChartProps } from "../core/interfaces";
1212
import { getOptionsId } from "../core/utils";
1313
import { InternalBaseComponentProps } from "../internal/base-component/use-base-component";
1414
import * as Styles from "../internal/chart-styles";
15-
import ChartSeriesDetails from "../internal/components/series-details";
1615
import { fireNonCancelableEvent } from "../internal/events";
1716
import { SomeRequired, Writeable } from "../internal/utils/utils";
1817
import { useInnerArea } from "./chart-inner-area";
@@ -62,29 +61,24 @@ export const InternalPieChart = forwardRef(
6261
// We convert pie tooltip options to the core chart's getTooltipContent callback,
6362
// ensuring no internal types are exposed to the consumer-defined render functions.
6463
const getTooltipContent: CoreChartProps["getTooltipContent"] = () => {
65-
const transformSlotProps = (props: CoreChartProps.TooltipSlotProps): PieChartProps.TooltipDetailsRenderProps => {
66-
const point = props.items[0].point;
64+
const transformDetailsProps = ({
65+
point,
66+
}: CoreChartProps.TooltipDetailsProps): PieChartProps.TooltipDetailsRenderProps => {
6767
return {
6868
totalValue: point.total ?? 0,
6969
segmentValue: point.y ?? 0,
7070
segmentId: getOptionsId(point.options),
7171
segmentName: point.name ?? "",
7272
};
7373
};
74+
const transformSlotProps = (props: CoreChartProps.TooltipSlotProps): PieChartProps.TooltipDetailsRenderProps => {
75+
const point = props.items[0].point;
76+
return transformDetailsProps({ point });
77+
};
7478
return {
7579
header: tooltip?.header ? (props) => tooltip.header!(transformSlotProps(props)) : undefined,
76-
body:
77-
tooltip?.body || tooltip?.details
78-
? (props) =>
79-
tooltip.body ? (
80-
tooltip.body(transformSlotProps(props))
81-
) : (
82-
<ChartSeriesDetails
83-
details={tooltip?.details?.(transformSlotProps(props)) ?? []}
84-
compactList={true}
85-
/>
86-
)
87-
: undefined,
80+
details: tooltip?.details ? (props) => tooltip.details!(transformDetailsProps(props)) : undefined,
81+
body: tooltip?.body ? (props) => tooltip.body!(transformSlotProps(props)) : undefined,
8882
footer: tooltip?.footer ? (props) => tooltip.footer!(transformSlotProps(props)) : undefined,
8983
};
9084
};

0 commit comments

Comments
 (0)