From 69a744699892cb30861572931a98b2f7b1e2a0c8 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 28 Dec 2022 14:46:32 -0500 Subject: [PATCH] Fix after update --- packages/d3tooltip/src/index.ts | 51 +++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/d3tooltip/src/index.ts b/packages/d3tooltip/src/index.ts index bf8e2795..bdba4d7b 100644 --- a/packages/d3tooltip/src/index.ts +++ b/packages/d3tooltip/src/index.ts @@ -1,27 +1,42 @@ -import d3Package, { Primitive, Selection } from 'd3'; +import d3Package, { + BaseType, + ContainerElement, + Primitive, + Selection, +} from 'd3'; import { is } from 'ramda'; import utils from './utils'; const { prependClass, functor } = utils; -interface Options { +interface Options< + GElement extends ContainerElement, + Datum, + PElement extends BaseType, + PDatum +> { left: number | undefined; top: number | undefined; offset: { left: number; top: number; }; - root: Selection | undefined; + root: Selection | undefined; } -const defaultOptions: Options = { +const defaultOptions: Options = { left: undefined, // mouseX top: undefined, // mouseY offset: { left: 0, top: 0 }, root: undefined, }; -interface Tip { - (selection: Selection): void; +interface Tip< + GElement extends ContainerElement, + Datum, + PElement extends BaseType, + PDatum +> { + (selection: Selection): void; attr: ( this: this, d: @@ -49,15 +64,20 @@ interface Tip { ) => this; } -export function tooltip( +export function tooltip< + GElement extends ContainerElement, + Datum, + PElement extends BaseType, + PDatum +>( d3: typeof d3Package, className = 'tooltip', - options: Partial> = {} -): Tip { + options: Partial> = {} +): Tip { const { left, top, offset, root } = { ...defaultOptions, ...options, - } as Options; + } as Options; let attrs = { class: className }; let text: (datum: Datum, index?: number, outerIndex?: number) => string = ( @@ -65,11 +85,12 @@ export function tooltip( ) => ''; let styles = {}; - let el: Selection; - const anchor = root || d3.select('body'); - const rootNode = anchor.node(); + let el: Selection; + const anchor: Selection = + root || d3.select('body'); + const rootNode = anchor.node()!; - function tip(selection: Selection) { + function tip(selection: Selection) { selection.on('mouseover.tip', (node) => { const [mouseX, mouseY] = d3.mouse(rootNode); const [x, y] = [left || mouseX + offset.left, top || mouseY - offset.top]; @@ -86,7 +107,7 @@ export function tooltip( top: `${y}px`, ...styles, }) - .html(() => text(node)) as Selection; + .html(() => text(node)) as Selection; }); selection.on('mousemove.tip', (node) => {