Skip to content

Commit

Permalink
feat(annotation): simplify custom tooltip function (#247)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this changes the type signature of `RectAnnotation.renderTooltip?` from `(position, details) => JSX.Element` to `(details) => JSX.Element`.  This allows the user to pass in a custom element without having to do the heavy lifting of writing the container positioning styles themselves.
  • Loading branch information
emmacunningham authored Jun 24, 2019
1 parent 98a87c8 commit 982bc63
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
13 changes: 5 additions & 8 deletions src/components/annotation_tooltips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ class AnnotationTooltipComponent extends React.Component<AnnotationTooltipProps>
return <LineAnnotationTooltip {...props} />;
}
case 'rectangle': {
const props = { details, position };

if (tooltipState.renderTooltip) {
return tooltipState.renderTooltip(position, details);
}

const props = { details, position, customTooltip: tooltipState.renderTooltip };
return <RectAnnotationTooltip {...props} />;
}
default:
Expand Down Expand Up @@ -119,12 +114,14 @@ export const AnnotationTooltip = inject('chartStore')(observer(AnnotationTooltip
function RectAnnotationTooltip(props: {
details?: string;
position: { transform: string; top: number; left: number };
customTooltip?: (details?: string) => JSX.Element;
}) {
const { details, position } = props;
const { details, position, customTooltip } = props;
const tooltipContent = customTooltip ? customTooltip(details) : details;
return (
<div className="echAnnotation__tooltip" style={{ ...position }}>
<div className="echAnnotation__details">
<div className="echAnnotation__detailsText">{details}</div>
<div className="echAnnotation__detailsText">{tooltipContent}</div>
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/series/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export interface RectAnnotationDatum {
export type RectAnnotationSpec = BaseAnnotationSpec & {
annotationType: 'rectangle';
/** Custom rendering function for tooltip */
renderTooltip?: (position: { transform: string; top: number; left: number }, details?: string) => JSX.Element;
renderTooltip?: (details?: string) => JSX.Element;
/** Data values defined with coordinates and details */
dataValues: RectAnnotationDatum[];
/** Custom annotation style */
Expand Down
4 changes: 2 additions & 2 deletions src/state/annotation_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface AnnotationTooltipState {
transform: string;
top?: number;
left?: number;
renderTooltip?: (position: { transform: string; top: number; left: number }, details?: string) => JSX.Element;
renderTooltip?: (details?: string) => JSX.Element;
}
export interface AnnotationDetails {
headerText?: string;
Expand Down Expand Up @@ -925,7 +925,7 @@ export function computeRectAnnotationTooltipState(
annotationRects: AnnotationRectProps[],
chartRotation: Rotation,
chartDimensions: Dimensions,
renderTooltip?: (position: { transform: string; top: number; left: number }, details?: string) => JSX.Element,
renderTooltip?: (details?: string) => JSX.Element,
): AnnotationTooltipState {
const cursorPosition = getRotatedCursor(rawCursorPosition, chartDimensions, chartRotation);

Expand Down
15 changes: 4 additions & 11 deletions stories/annotations.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { array, boolean, color, number, select } from '@storybook/addon-knobs';
import { storiesOf } from '@storybook/react';
import React, { CSSProperties } from 'react';
import React from 'react';
import {
AnnotationDomainTypes,
Axis,
Expand Down Expand Up @@ -508,16 +508,9 @@ storiesOf('Annotations', module)
};

const hasCustomTooltip = boolean('has custom tooltip render', false);
const tooltipStyle: CSSProperties = {
position: 'absolute',
backgroundColor: '#e76f6f',
color: '#e6e6e6',
overflow: 'hidden',
overflowWrap: 'break-word',
width: '120px',
};
const customTooltip = (position: { transform: string; top: number; left: number }, details?: string) => (
<div style={{ ...tooltipStyle, ...position }}>

const customTooltip = (details?: string) => (
<div>
<Icon type="alert" />
{details}
</div>
Expand Down

0 comments on commit 982bc63

Please sign in to comment.