plouc / nivo

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
https://nivo.rocks
MIT License
13.06k stars 1.02k forks source link

Wrong `PointMouseHandler` type on event handlers like `onMouseMove` #2615

Open madebyfabian opened 2 months ago

madebyfabian commented 2 months ago

Describe/explain the bug When using event handlers like onMouseMove, onMouseEnter and so on with for example the <ResponsiveLine>, the return type of the callback parameters is (point: Point, event: React.MouseEvent) => void in TypeScript. But these types are wrong, since in runtime, the actual type you get is (slice: Slice, event: React.MouseEvent) => void. So instead of getting just one point, you get the whole slice object, including the points: Point[] array.

Not sure if it's only for the line chart or others also.

To Reproduce Steps to reproduce the behavior:

  1. Use Nivo@0.87.0
  2. Create a <ResponsiveLine data={...} onMouseMove={(...params) => { console.log(params) }} />
  3. See the type mismatch in your editor versus the console logs real values

Expected behavior Types match

madebyfabian commented 2 months ago

I also discovered that onTouchMove always returns the same slice, no matter where the finger is touching the chart. onMouseMove works perfectly fine and returns the exact slice the user hovers over. Is this by design?

https://github.com/plouc/nivo/blob/839ff6d4f272690c792d67cc143496bd29543b55/packages/line/src/SlicesItem.js#L64-L89

And is there a better way to get the current slice/points that is being hovered/touched over?

Not sure if this could be related with the type mismatch, if not, I can open a seperate issue.